common/vi_db.c: include <stdlib.h> for mkstemp()
[nvi.git] / common / vi_db.c
blob52375078fcf3993cd77a6d883d13e766e7ecc763
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: db.c,v 10.48 2002/06/08 19:32:52 skimo Exp $ (Berkeley) $Date: 2002/06/08 19:32:52 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "common.h"
29 #include "../vi/vi.h"
31 static int append __P((SCR*, db_recno_t, CHAR_T*, size_t, lnop_t, int));
34 * db_eget --
35 * Front-end to db_get, special case handling for empty files.
37 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
39 int
40 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
42 /* Line number. */
43 /* Pointer store. */
44 /* Length store. */
47 db_recno_t l1;
49 if (isemptyp != NULL)
50 *isemptyp = 0;
52 /* If the line exists, simply return it. */
53 if (!db_get(sp, lno, 0, pp, lenp))
54 return (0);
57 * If the user asked for line 0 or line 1, i.e. the only possible
58 * line in an empty file, find the last line of the file; db_last
59 * fails loudly.
61 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
62 return (1);
64 /* If the file isn't empty, fail loudly. */
65 if ((lno != 0 && lno != 1) || l1 != 0) {
66 db_err(sp, lno);
67 return (1);
70 if (isemptyp != NULL)
71 *isemptyp = 1;
73 return (1);
77 * db_get --
78 * Look in the text buffers for a line, followed by the cache, followed
79 * by the database.
81 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
83 int
84 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
85 /* Line number. */ /* Pointer store. */ /* Length store. */
87 DBT data, key;
88 EXF *ep;
89 TEXT *tp;
90 db_recno_t l1, l2;
91 CHAR_T *wp;
92 size_t wlen;
93 size_t nlen;
96 * The underlying recno stuff handles zero by returning NULL, but
97 * have to have an OOB condition for the look-aside into the input
98 * buffer anyway.
100 if (lno == 0)
101 goto err1;
103 /* Check for no underlying file. */
104 if ((ep = sp->ep) == NULL) {
105 ex_emsg(sp, NULL, EXM_NOFILEYET);
106 goto err3;
109 if (LF_ISSET(DBG_NOCACHE))
110 goto nocache;
113 * Look-aside into the TEXT buffers and see if the line we want
114 * is there.
116 if (F_ISSET(sp, SC_TINPUT)) {
117 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
118 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
119 if (l1 <= lno && l2 >= lno) {
120 #if defined(DEBUG) && 0
121 vtrace(sp,
122 "retrieve TEXT buffer line %lu\n", (u_long)lno);
123 #endif
124 for (tp = sp->tiq.cqh_first;
125 tp->lno != lno; tp = tp->q.cqe_next);
126 if (lenp != NULL)
127 *lenp = tp->len;
128 if (pp != NULL)
129 *pp = tp->lb;
130 return (0);
133 * Adjust the line number for the number of lines used
134 * by the text input buffers.
136 if (lno > l2)
137 lno -= l2 - l1;
140 /* Look-aside into the cache, and see if the line we want is there. */
141 if (lno == sp->c_lno) {
142 #if defined(DEBUG) && 0
143 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
144 #endif
145 if (lenp != NULL)
146 *lenp = sp->c_len;
147 if (pp != NULL)
148 *pp = sp->c_lp;
149 return (0);
151 sp->c_lno = OOBLNO;
153 nocache:
154 nlen = 1024;
155 retry:
156 /* data.size contains length in bytes */
157 BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
159 /* Get the line from the underlying database. */
160 memset(&key, 0, sizeof(key));
161 key.data = &lno;
162 key.size = sizeof(lno);
163 memset(&data, 0, sizeof(data));
164 data.data = sp->c_lp;
165 data.ulen = sp->c_blen;
166 data.flags = DB_DBT_USERMEM;
167 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
168 case DB_BUFFER_SMALL:
169 nlen = data.size;
170 goto retry;
171 default:
172 goto err2;
173 case DB_NOTFOUND:
174 err1: if (LF_ISSET(DBG_FATAL))
175 err2: db_err(sp, lno);
176 alloc_err:
177 err3: if (lenp != NULL)
178 *lenp = 0;
179 if (pp != NULL)
180 *pp = NULL;
181 return (1);
182 case 0:
186 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
187 if (!F_ISSET(sp, SC_CONV_ERROR)) {
188 F_SET(sp, SC_CONV_ERROR);
189 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
191 goto err3;
194 /* Reset the cache. */
195 if (wp != data.data) {
196 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
197 MEMCPYW(sp->c_lp, wp, wlen);
199 sp->c_lno = lno;
200 sp->c_len = wlen;
202 #if defined(DEBUG) && 0
203 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
204 #endif
205 if (lenp != NULL)
206 *lenp = wlen;
207 if (pp != NULL)
208 *pp = sp->c_lp;
209 return (0);
213 * db_delete --
214 * Delete a line from the file.
216 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
219 db_delete(SCR *sp, db_recno_t lno)
221 DBT key;
222 EXF *ep;
224 #if defined(DEBUG) && 0
225 vtrace(sp, "delete line %lu\n", (u_long)lno);
226 #endif
227 /* Check for no underlying file. */
228 if ((ep = sp->ep) == NULL) {
229 ex_emsg(sp, NULL, EXM_NOFILEYET);
230 return (1);
232 if (ep->l_win && ep->l_win != sp->wp) {
233 ex_emsg(sp, NULL, EXM_LOCKED);
234 return 1;
237 /* Update marks, @ and global commands. */
238 if (line_insdel(sp, LINE_DELETE, lno))
239 return 1;
241 /* Log before change. */
242 log_line(sp, lno, LOG_LINE_DELETE_B);
244 /* Update file. */
245 memset(&key, 0, sizeof(key));
246 key.data = &lno;
247 key.size = sizeof(lno);
248 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
249 msgq(sp, M_DBERR, "003|unable to delete line %lu",
250 (u_long)lno);
251 return (1);
254 /* Flush the cache, update line count, before screen update. */
255 update_cache(sp, LINE_DELETE, lno);
257 /* File now modified. */
258 if (F_ISSET(ep, F_FIRSTMODIFY))
259 (void)rcv_init(sp);
260 F_SET(ep, F_MODIFIED);
262 /* Log after change. */
263 log_line(sp, lno, LOG_LINE_DELETE_F);
265 /* Update screen. */
266 return (scr_update(sp, lno, LINE_DELETE, 1));
269 /* maybe this could be simpler
271 * DB3 behaves differently from DB1
273 * if lno != 0 just go to lno and put the new line after it
274 * if lno == 0 then if there are any record, put in front of the first
275 * otherwise just append to the end thus creating the first
276 * line
278 static int
279 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len, lnop_t op, int update)
281 DBT data, key;
282 DBC *dbcp_put;
283 EXF *ep;
284 char *fp;
285 size_t flen;
286 int rval;
288 /* Check for no underlying file. */
289 if ((ep = sp->ep) == NULL) {
290 ex_emsg(sp, NULL, EXM_NOFILEYET);
291 return (1);
293 if (ep->l_win && ep->l_win != sp->wp) {
294 ex_emsg(sp, NULL, EXM_LOCKED);
295 return 1;
298 /* Log before change. */
299 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
301 /* Update file. */
302 memset(&key, 0, sizeof(key));
303 key.data = &lno;
304 key.size = sizeof(lno);
305 memset(&data, 0, sizeof(data));
307 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
308 return 1;
310 INT2FILE(sp, p, len, fp, flen);
312 if (lno != 0) {
313 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
314 goto err2;
316 data.data = fp;
317 data.size = flen;
318 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
319 err2:
320 (void)dbcp_put->c_close(dbcp_put);
321 msgq(sp, M_DBERR,
322 (op == LINE_APPEND)
323 ? "004|unable to append to line %lu"
324 : "005|unable to insert at line %lu",
325 (u_long)lno);
326 return (1);
328 } else {
329 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
330 if (sp->db_error != DB_NOTFOUND)
331 goto err2;
333 data.data = fp;
334 data.size = flen;
335 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
336 goto err2;
338 } else {
339 key.data = &lno;
340 key.size = sizeof(lno);
341 data.data = fp;
342 data.size = flen;
343 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
344 goto err2;
349 (void)dbcp_put->c_close(dbcp_put);
351 /* Flush the cache, update line count, before screen update. */
352 update_cache(sp, LINE_INSERT, lno);
354 /* File now dirty. */
355 if (F_ISSET(ep, F_FIRSTMODIFY))
356 (void)rcv_init(sp);
357 F_SET(ep, F_MODIFIED);
359 /* Log after change. */
360 log_line(sp, lno + 1, LOG_LINE_APPEND_F);
362 /* Update marks, @ and global commands. */
363 rval = line_insdel(sp, LINE_INSERT, lno + 1);
366 * Update screen.
368 * comment copied from db_append
369 * XXX
370 * Nasty hack. If multiple lines are input by the user, they aren't
371 * committed until an <ESC> is entered. The problem is the screen was
372 * updated/scrolled as each line was entered. So, when this routine
373 * is called to copy the new lines from the cut buffer into the file,
374 * it has to know not to update the screen again.
376 return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
380 * db_append --
381 * Append a line into the file.
383 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
386 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
388 #if defined(DEBUG) && 0
389 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
390 #endif
392 /* Update file. */
393 return append(sp, lno, p, len, LINE_APPEND, update);
397 * db_insert --
398 * Insert a line into the file.
400 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
403 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
405 #if defined(DEBUG) && 0
406 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
407 (u_long)lno, (u_long)len, MIN(len, 20), p);
408 #endif
409 return append(sp, lno - 1, p, len, LINE_INSERT, 1);
413 * db_set --
414 * Store a line in the file.
416 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
419 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
421 DBT data, key;
422 EXF *ep;
423 char *fp;
424 size_t flen;
426 #if defined(DEBUG) && 0
427 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
428 (u_long)lno, (u_long)len, MIN(len, 20), p);
429 #endif
430 /* Check for no underlying file. */
431 if ((ep = sp->ep) == NULL) {
432 ex_emsg(sp, NULL, EXM_NOFILEYET);
433 return (1);
435 if (ep->l_win && ep->l_win != sp->wp) {
436 ex_emsg(sp, NULL, EXM_LOCKED);
437 return 1;
440 /* Log before change. */
441 log_line(sp, lno, LOG_LINE_RESET_B);
443 INT2FILE(sp, p, len, fp, flen);
445 /* Update file. */
446 memset(&key, 0, sizeof(key));
447 key.data = &lno;
448 key.size = sizeof(lno);
449 memset(&data, 0, sizeof(data));
450 data.data = fp;
451 data.size = flen;
452 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
453 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
454 return (1);
457 /* Flush the cache, update line count, before screen update. */
458 update_cache(sp, LINE_RESET, lno);
460 /* File now dirty. */
461 if (F_ISSET(ep, F_FIRSTMODIFY))
462 (void)rcv_init(sp);
463 F_SET(ep, F_MODIFIED);
465 /* Log after change. */
466 log_line(sp, lno, LOG_LINE_RESET_F);
468 /* Update screen. */
469 return (scr_update(sp, lno, LINE_RESET, 1));
473 * db_exist --
474 * Return if a line exists.
476 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
479 db_exist(SCR *sp, db_recno_t lno)
481 EXF *ep;
483 /* Check for no underlying file. */
484 if ((ep = sp->ep) == NULL) {
485 ex_emsg(sp, NULL, EXM_NOFILEYET);
486 return (1);
489 if (lno == OOBLNO)
490 return (0);
493 * Check the last-line number cache. Adjust the cached line
494 * number for the lines used by the text input buffers.
496 if (ep->c_nlines != OOBLNO)
497 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
498 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
499 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
501 /* Go get the line. */
502 return (!db_get(sp, lno, 0, NULL, NULL));
506 * db_last --
507 * Return the number of lines in the file.
509 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
512 db_last(SCR *sp, db_recno_t *lnop)
514 DBT data, key;
515 DBC *dbcp;
516 EXF *ep;
517 db_recno_t lno;
518 CHAR_T *wp;
519 size_t wlen;
521 /* Check for no underlying file. */
522 if ((ep = sp->ep) == NULL) {
523 ex_emsg(sp, NULL, EXM_NOFILEYET);
524 return (1);
528 * Check the last-line number cache. Adjust the cached line
529 * number for the lines used by the text input buffers.
531 if (ep->c_nlines != OOBLNO) {
532 *lnop = ep->c_nlines;
533 if (F_ISSET(sp, SC_TINPUT))
534 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
535 ((TEXT *)sp->tiq.cqh_first)->lno;
536 return (0);
539 memset(&key, 0, sizeof(key));
540 key.data = &lno;
541 key.size = sizeof(lno);
542 memset(&data, 0, sizeof(data));
544 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
545 goto err1;
546 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
547 case DB_NOTFOUND:
548 *lnop = 0;
549 return (0);
550 default:
551 (void)dbcp->c_close(dbcp);
552 alloc_err:
553 err1:
554 msgq(sp, M_DBERR, "007|unable to get last line");
555 *lnop = 0;
556 return (1);
557 case 0:
561 memcpy(&lno, key.data, sizeof(lno));
563 if (lno != sp->c_lno) {
564 FILE2INT(sp, data.data, data.size, wp, wlen);
566 /* Fill the cache. */
567 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
568 MEMCPYW(sp->c_lp, wp, wlen);
569 sp->c_lno = lno;
570 sp->c_len = wlen;
572 ep->c_nlines = lno;
574 (void)dbcp->c_close(dbcp);
576 /* Return the value. */
577 *lnop = (F_ISSET(sp, SC_TINPUT) &&
578 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
579 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
580 return (0);
584 * db_err --
585 * Report a line error.
587 * PUBLIC: void db_err __P((SCR *, db_recno_t));
589 void
590 db_err(SCR *sp, db_recno_t lno)
592 msgq(sp, M_ERR,
593 "008|Error: unable to retrieve line %lu", (u_long)lno);
597 * scr_update --
598 * Update all of the screens that are backed by the file that
599 * just changed.
601 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
602 * PUBLIC: lnop_t op, int current));
605 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
607 EXF *ep;
608 SCR *tsp;
609 WIN *wp;
611 if (F_ISSET(sp, SC_EX))
612 return (0);
614 /* XXXX goes outside of window */
615 ep = sp->ep;
616 if (ep->refcnt != 1)
617 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
618 wp = wp->q.cqe_next)
619 for (tsp = wp->scrq.cqh_first;
620 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
621 if (sp != tsp && tsp->ep == ep)
622 if (vs_change(tsp, lno, op))
623 return (1);
624 return (current ? vs_change(sp, lno, op) : 0);
628 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
630 void
631 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
633 SCR* scrp;
634 EXF *ep;
636 ep = sp->ep;
638 /* Flush the cache, update line count, before screen update. */
639 /* The flushing is probably not needed, since it was incorrect
640 * for db_insert. It might be better to adjust it, like
641 * marks, @ and global
643 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
644 scrp = scrp->eq.cqe_next)
645 switch (op) {
646 case LINE_INSERT:
647 case LINE_DELETE:
648 if (lno <= scrp->c_lno)
649 scrp->c_lno = OOBLNO;
650 break;
651 case LINE_RESET:
652 if (lno == scrp->c_lno)
653 scrp->c_lno = OOBLNO;
654 break;
657 if (ep->c_nlines != OOBLNO)
658 switch (op) {
659 case LINE_INSERT:
660 ++ep->c_nlines;
661 break;
662 case LINE_DELETE:
663 --ep->c_nlines;
664 break;
669 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
672 line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
674 int rval;
676 /* Update marks, @ and global commands. */
677 rval = 0;
678 if (mark_insdel(sp, op, lno))
679 rval = 1;
680 if (ex_g_insdel(sp, op, lno))
681 rval = 1;
683 return rval;
686 #ifdef USE_DB4_LOGGING
687 #define VI_DB_INIT_LOG DB_INIT_LOG
688 #else
689 #define VI_DB_INIT_LOG 0
690 #endif
693 * PUBLIC: int db_setup __P((SCR *, EXF *));
696 db_setup(SCR *sp, EXF *ep)
698 char path[MAXPATHLEN];
699 int fd;
700 DB_ENV *env;
702 (void)snprintf(path, sizeof(path), "%s/vi.XXXXXX", O_STR(sp, O_RECDIR));
703 if ((fd = mkstemp(path)) == -1) {
704 msgq(sp, M_SYSERR, "%s", path);
705 goto err;
707 (void)close(fd);
708 (void)unlink(path);
709 if (mkdir(path, S_IRWXU)) {
710 msgq(sp, M_SYSERR, "%s", path);
711 goto err;
713 if (db_env_create(&env, 0)) {
714 msgq(sp, M_ERR, "env_create");
715 goto err;
717 #ifdef USE_DB4_LOGGING
718 if ((sp->db_error = vi_db_init_recover(env))) {
719 msgq(sp, M_DBERR, "init_recover");
720 goto err;
722 if ((sp->db_error = __vi_init_recover(env))) {
723 msgq(sp, M_DBERR, "init_recover");
724 goto err;
726 #endif
727 if ((sp->db_error = db_env_open(env, path,
728 DB_PRIVATE | DB_CREATE | DB_INIT_MPOOL | VI_DB_THREAD
729 | VI_DB_INIT_LOG, 0)) != 0) {
730 msgq(sp, M_DBERR, "env->open");
731 goto err;
734 if ((ep->env_path = strdup(path)) == NULL) {
735 msgq(sp, M_SYSERR, NULL);
736 (void)rmdir(path);
737 goto err;
739 ep->env = env;
740 return 0;
741 err:
742 return 1;
745 /* Round up v to the nearest power of 2 */
746 static size_t round_up(size_t v)
748 ssize_t old_v = v;
750 while (v) {
751 old_v = v;
752 v ^= v & -v;
754 return old_v << 1;
758 * PUBLIC: int db_msg_open __P((SCR *, char *, DB **));
760 int db_msg_open(SCR *sp, char *file, DB **dbp)
762 return (sp->db_error = db_create(dbp, 0, 0)) != 0 ||
763 (sp->db_error = (*dbp)->set_re_source(*dbp, file)) != 0 ||
764 (sp->db_error = db_open(*dbp, NULL, DB_RECNO, 0, 0)) != 0;
768 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
771 db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
773 if (db_setup(sp, ep))
774 return 1;
776 /* Open a db structure. */
777 if ((sp->db_error = db_create(&ep->db, 0, 0)) != 0) {
778 msgq(sp, M_DBERR, "db_create");
779 return 1;
782 ep->db->set_re_delim(ep->db, '\n'); /* Always set. */
783 ep->db->set_pagesize(ep->db, round_up(psize));
784 ep->db->set_flags(ep->db, DB_RENUMBER | DB_SNAPSHOT);
785 if (rcv_name == NULL)
786 ep->db->set_re_source(ep->db, oname);
789 * Don't let db use mmap when using fcntl for locking
791 #ifdef HAVE_LOCK_FCNTL
792 #define NOMMAPIFFCNTL DB_NOMMAP
793 #else
794 #define NOMMAPIFFCNTL 0
795 #endif
797 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
799 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
800 ((rcv_name == 0) ? DB_TRUNCATE : 0) | VI_DB_THREAD | NOMMAPIFFCNTL,
801 _DB_OPEN_MODE)) != 0) {
802 msgq_str(sp,
803 M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
805 * !!!
806 * Historically, vi permitted users to edit files that couldn't
807 * be read. This isn't useful for single files from a command
808 * line, but it's quite useful for "vi *.c", since you can skip
809 * past files that you can't read.
811 ep->db = NULL; /* Don't close it; it wasn't opened */
813 *open_err = 1;
814 return 1;
817 /* re_source is loaded into the database.
818 * Close it and reopen it in the environment.
820 if ((sp->db_error = ep->db->close(ep->db, 0))) {
821 msgq(sp, M_DBERR, "close");
822 return 1;
824 if ((sp->db_error = db_create(&ep->db, ep->env, 0)) != 0) {
825 msgq(sp, M_DBERR, "db_create 2");
826 return 1;
828 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
829 VI_DB_THREAD | NOMMAPIFFCNTL, _DB_OPEN_MODE)) != 0) {
830 msgq_str(sp,
831 M_DBERR, ep->rcv_path, "%s");
832 return 1;
835 return 0;