common/log.c: minor whitespace change
[nvi.git] / common / db.c
blobfc3e851bebf9428e93ad13e8e790f21bc2408223
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 <string.h>
27 #include "common.h"
28 #include "../vi/vi.h"
30 static int append __P((SCR*, db_recno_t, CHAR_T*, size_t, lnop_t, int));
33 * db_eget --
34 * Front-end to db_get, special case handling for empty files.
36 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
38 int
39 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
41 /* Line number. */
42 /* Pointer store. */
43 /* Length store. */
46 db_recno_t l1;
48 if (isemptyp != NULL)
49 *isemptyp = 0;
51 /* If the line exists, simply return it. */
52 if (!db_get(sp, lno, 0, pp, lenp))
53 return (0);
56 * If the user asked for line 0 or line 1, i.e. the only possible
57 * line in an empty file, find the last line of the file; db_last
58 * fails loudly.
60 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
61 return (1);
63 /* If the file isn't empty, fail loudly. */
64 if ((lno != 0 && lno != 1) || l1 != 0) {
65 db_err(sp, lno);
66 return (1);
69 if (isemptyp != NULL)
70 *isemptyp = 1;
72 return (1);
76 * db_get --
77 * Look in the text buffers for a line, followed by the cache, followed
78 * by the database.
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
82 int
83 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
84 /* Line number. */ /* Pointer store. */ /* Length store. */
86 DBT data, key;
87 EXF *ep;
88 TEXT *tp;
89 db_recno_t l1, l2;
90 CHAR_T *wp;
91 size_t wlen;
92 size_t nlen;
95 * The underlying recno stuff handles zero by returning NULL, but
96 * have to have an OOB condition for the look-aside into the input
97 * buffer anyway.
99 if (lno == 0)
100 goto err1;
102 /* Check for no underlying file. */
103 if ((ep = sp->ep) == NULL) {
104 ex_emsg(sp, NULL, EXM_NOFILEYET);
105 goto err3;
108 if (LF_ISSET(DBG_NOCACHE))
109 goto nocache;
112 * Look-aside into the TEXT buffers and see if the line we want
113 * is there.
115 if (F_ISSET(sp, SC_TINPUT)) {
116 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
117 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
118 if (l1 <= lno && l2 >= lno) {
119 #if defined(DEBUG) && 0
120 vtrace(sp,
121 "retrieve TEXT buffer line %lu\n", (u_long)lno);
122 #endif
123 for (tp = sp->tiq.cqh_first;
124 tp->lno != lno; tp = tp->q.cqe_next);
125 if (lenp != NULL)
126 *lenp = tp->len;
127 if (pp != NULL)
128 *pp = tp->lb;
129 return (0);
132 * Adjust the line number for the number of lines used
133 * by the text input buffers.
135 if (lno > l2)
136 lno -= l2 - l1;
139 /* Look-aside into the cache, and see if the line we want is there. */
140 if (lno == sp->c_lno) {
141 #if defined(DEBUG) && 0
142 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
143 #endif
144 if (lenp != NULL)
145 *lenp = sp->c_len;
146 if (pp != NULL)
147 *pp = sp->c_lp;
148 return (0);
150 sp->c_lno = OOBLNO;
152 nocache:
153 nlen = 1024;
154 retry:
155 /* data.size contains length in bytes */
156 BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
158 /* Get the line from the underlying database. */
159 memset(&key, 0, sizeof(key));
160 key.data = &lno;
161 key.size = sizeof(lno);
162 memset(&data, 0, sizeof(data));
163 data.data = sp->c_lp;
164 data.ulen = sp->c_blen;
165 data.flags = DB_DBT_USERMEM;
166 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
167 case DB_BUFFER_SMALL:
168 nlen = data.size;
169 goto retry;
170 default:
171 goto err2;
172 case DB_NOTFOUND:
173 err1: if (LF_ISSET(DBG_FATAL))
174 err2: db_err(sp, lno);
175 alloc_err:
176 err3: if (lenp != NULL)
177 *lenp = 0;
178 if (pp != NULL)
179 *pp = NULL;
180 return (1);
181 case 0:
185 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
186 if (!F_ISSET(sp, SC_CONV_ERROR)) {
187 F_SET(sp, SC_CONV_ERROR);
188 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
190 goto err3;
193 /* Reset the cache. */
194 if (wp != data.data) {
195 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
196 MEMCPYW(sp->c_lp, wp, wlen);
198 sp->c_lno = lno;
199 sp->c_len = wlen;
201 #if defined(DEBUG) && 0
202 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
203 #endif
204 if (lenp != NULL)
205 *lenp = wlen;
206 if (pp != NULL)
207 *pp = sp->c_lp;
208 return (0);
212 * db_delete --
213 * Delete a line from the file.
215 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
218 db_delete(SCR *sp, db_recno_t lno)
220 DBT key;
221 EXF *ep;
223 #if defined(DEBUG) && 0
224 vtrace(sp, "delete line %lu\n", (u_long)lno);
225 #endif
226 /* Check for no underlying file. */
227 if ((ep = sp->ep) == NULL) {
228 ex_emsg(sp, NULL, EXM_NOFILEYET);
229 return (1);
231 if (ep->l_win && ep->l_win != sp->wp) {
232 ex_emsg(sp, NULL, EXM_LOCKED);
233 return 1;
236 /* Update marks, @ and global commands. */
237 if (line_insdel(sp, LINE_DELETE, lno))
238 return 1;
240 /* Log before change. */
241 log_line(sp, lno, LOG_LINE_DELETE_B);
243 /* Update file. */
244 memset(&key, 0, sizeof(key));
245 key.data = &lno;
246 key.size = sizeof(lno);
247 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
248 msgq(sp, M_DBERR, "003|unable to delete line %lu",
249 (u_long)lno);
250 return (1);
253 /* Flush the cache, update line count, before screen update. */
254 update_cache(sp, LINE_DELETE, lno);
256 /* File now modified. */
257 if (F_ISSET(ep, F_FIRSTMODIFY))
258 (void)rcv_init(sp);
259 F_SET(ep, F_MODIFIED);
261 /* Log after change. */
262 log_line(sp, lno, LOG_LINE_DELETE_F);
264 /* Update screen. */
265 return (scr_update(sp, lno, LINE_DELETE, 1));
268 /* maybe this could be simpler
270 * DB3 behaves differently from DB1
272 * if lno != 0 just go to lno and put the new line after it
273 * if lno == 0 then if there are any record, put in front of the first
274 * otherwise just append to the end thus creating the first
275 * line
277 static int
278 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len, lnop_t op, int update)
280 DBT data, key;
281 DBC *dbcp_put;
282 EXF *ep;
283 char *fp;
284 size_t flen;
285 int rval;
287 /* Check for no underlying file. */
288 if ((ep = sp->ep) == NULL) {
289 ex_emsg(sp, NULL, EXM_NOFILEYET);
290 return (1);
292 if (ep->l_win && ep->l_win != sp->wp) {
293 ex_emsg(sp, NULL, EXM_LOCKED);
294 return 1;
297 /* Log before change. */
298 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
300 /* Update file. */
301 memset(&key, 0, sizeof(key));
302 key.data = &lno;
303 key.size = sizeof(lno);
304 memset(&data, 0, sizeof(data));
306 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
307 return 1;
309 INT2FILE(sp, p, len, fp, flen);
311 if (lno != 0) {
312 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
313 goto err2;
315 data.data = fp;
316 data.size = flen;
317 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
318 err2:
319 (void)dbcp_put->c_close(dbcp_put);
320 msgq(sp, M_DBERR,
321 (op == LINE_APPEND)
322 ? "004|unable to append to line %lu"
323 : "005|unable to insert at line %lu",
324 (u_long)lno);
325 return (1);
327 } else {
328 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
329 if (sp->db_error != DB_NOTFOUND)
330 goto err2;
332 data.data = fp;
333 data.size = flen;
334 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
335 goto err2;
337 } else {
338 key.data = &lno;
339 key.size = sizeof(lno);
340 data.data = fp;
341 data.size = flen;
342 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
343 goto err2;
348 (void)dbcp_put->c_close(dbcp_put);
350 /* Flush the cache, update line count, before screen update. */
351 update_cache(sp, LINE_INSERT, lno);
353 /* File now dirty. */
354 if (F_ISSET(ep, F_FIRSTMODIFY))
355 (void)rcv_init(sp);
356 F_SET(ep, F_MODIFIED);
358 /* Log after change. */
359 log_line(sp, lno + 1, LOG_LINE_APPEND_F);
361 /* Update marks, @ and global commands. */
362 rval = line_insdel(sp, LINE_INSERT, lno + 1);
365 * Update screen.
367 * comment copied from db_append
368 * XXX
369 * Nasty hack. If multiple lines are input by the user, they aren't
370 * committed until an <ESC> is entered. The problem is the screen was
371 * updated/scrolled as each line was entered. So, when this routine
372 * is called to copy the new lines from the cut buffer into the file,
373 * it has to know not to update the screen again.
375 return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
379 * db_append --
380 * Append a line into the file.
382 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
385 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
387 #if defined(DEBUG) && 0
388 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
389 #endif
391 /* Update file. */
392 return append(sp, lno, p, len, LINE_APPEND, update);
396 * db_insert --
397 * Insert a line into the file.
399 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
402 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
404 #if defined(DEBUG) && 0
405 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
406 (u_long)lno, (u_long)len, MIN(len, 20), p);
407 #endif
408 return append(sp, lno - 1, p, len, LINE_INSERT, 1);
412 * db_set --
413 * Store a line in the file.
415 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
418 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
420 DBT data, key;
421 EXF *ep;
422 char *fp;
423 size_t flen;
425 #if defined(DEBUG) && 0
426 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
427 (u_long)lno, (u_long)len, MIN(len, 20), p);
428 #endif
429 /* Check for no underlying file. */
430 if ((ep = sp->ep) == NULL) {
431 ex_emsg(sp, NULL, EXM_NOFILEYET);
432 return (1);
434 if (ep->l_win && ep->l_win != sp->wp) {
435 ex_emsg(sp, NULL, EXM_LOCKED);
436 return 1;
439 /* Log before change. */
440 log_line(sp, lno, LOG_LINE_RESET_B);
442 INT2FILE(sp, p, len, fp, flen);
444 /* Update file. */
445 memset(&key, 0, sizeof(key));
446 key.data = &lno;
447 key.size = sizeof(lno);
448 memset(&data, 0, sizeof(data));
449 data.data = fp;
450 data.size = flen;
451 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
452 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
453 return (1);
456 /* Flush the cache, update line count, before screen update. */
457 update_cache(sp, LINE_RESET, lno);
459 /* File now dirty. */
460 if (F_ISSET(ep, F_FIRSTMODIFY))
461 (void)rcv_init(sp);
462 F_SET(ep, F_MODIFIED);
464 /* Log after change. */
465 log_line(sp, lno, LOG_LINE_RESET_F);
467 /* Update screen. */
468 return (scr_update(sp, lno, LINE_RESET, 1));
472 * db_exist --
473 * Return if a line exists.
475 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
478 db_exist(SCR *sp, db_recno_t lno)
480 EXF *ep;
482 /* Check for no underlying file. */
483 if ((ep = sp->ep) == NULL) {
484 ex_emsg(sp, NULL, EXM_NOFILEYET);
485 return (1);
488 if (lno == OOBLNO)
489 return (0);
492 * Check the last-line number cache. Adjust the cached line
493 * number for the lines used by the text input buffers.
495 if (ep->c_nlines != OOBLNO)
496 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
497 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
498 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
500 /* Go get the line. */
501 return (!db_get(sp, lno, 0, NULL, NULL));
505 * db_last --
506 * Return the number of lines in the file.
508 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
511 db_last(SCR *sp, db_recno_t *lnop)
513 DBT data, key;
514 DBC *dbcp;
515 EXF *ep;
516 db_recno_t lno;
517 CHAR_T *wp;
518 size_t wlen;
520 /* Check for no underlying file. */
521 if ((ep = sp->ep) == NULL) {
522 ex_emsg(sp, NULL, EXM_NOFILEYET);
523 return (1);
527 * Check the last-line number cache. Adjust the cached line
528 * number for the lines used by the text input buffers.
530 if (ep->c_nlines != OOBLNO) {
531 *lnop = ep->c_nlines;
532 if (F_ISSET(sp, SC_TINPUT))
533 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
534 ((TEXT *)sp->tiq.cqh_first)->lno;
535 return (0);
538 memset(&key, 0, sizeof(key));
539 key.data = &lno;
540 key.size = sizeof(lno);
541 memset(&data, 0, sizeof(data));
543 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
544 goto err1;
545 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
546 case DB_NOTFOUND:
547 *lnop = 0;
548 return (0);
549 default:
550 (void)dbcp->c_close(dbcp);
551 alloc_err:
552 err1:
553 msgq(sp, M_DBERR, "007|unable to get last line");
554 *lnop = 0;
555 return (1);
556 case 0:
560 memcpy(&lno, key.data, sizeof(lno));
562 if (lno != sp->c_lno) {
563 FILE2INT(sp, data.data, data.size, wp, wlen);
565 /* Fill the cache. */
566 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
567 MEMCPYW(sp->c_lp, wp, wlen);
568 sp->c_lno = lno;
569 sp->c_len = wlen;
571 ep->c_nlines = lno;
573 (void)dbcp->c_close(dbcp);
575 /* Return the value. */
576 *lnop = (F_ISSET(sp, SC_TINPUT) &&
577 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
578 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
579 return (0);
583 * db_err --
584 * Report a line error.
586 * PUBLIC: void db_err __P((SCR *, db_recno_t));
588 void
589 db_err(SCR *sp, db_recno_t lno)
591 msgq(sp, M_ERR,
592 "008|Error: unable to retrieve line %lu", (u_long)lno);
596 * scr_update --
597 * Update all of the screens that are backed by the file that
598 * just changed.
600 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
601 * PUBLIC: lnop_t op, int current));
604 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
606 EXF *ep;
607 SCR *tsp;
608 WIN *wp;
610 if (F_ISSET(sp, SC_EX))
611 return (0);
613 /* XXXX goes outside of window */
614 ep = sp->ep;
615 if (ep->refcnt != 1)
616 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
617 wp = wp->q.cqe_next)
618 for (tsp = wp->scrq.cqh_first;
619 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
620 if (sp != tsp && tsp->ep == ep)
621 if (vs_change(tsp, lno, op))
622 return (1);
623 return (current ? vs_change(sp, lno, op) : 0);
627 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
629 void
630 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
632 SCR* scrp;
633 EXF *ep;
635 ep = sp->ep;
637 /* Flush the cache, update line count, before screen update. */
638 /* The flushing is probably not needed, since it was incorrect
639 * for db_insert. It might be better to adjust it, like
640 * marks, @ and global
642 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
643 scrp = scrp->eq.cqe_next)
644 switch (op) {
645 case LINE_INSERT:
646 case LINE_DELETE:
647 if (lno <= scrp->c_lno)
648 scrp->c_lno = OOBLNO;
649 break;
650 case LINE_RESET:
651 if (lno == scrp->c_lno)
652 scrp->c_lno = OOBLNO;
653 break;
656 if (ep->c_nlines != OOBLNO)
657 switch (op) {
658 case LINE_INSERT:
659 ++ep->c_nlines;
660 break;
661 case LINE_DELETE:
662 --ep->c_nlines;
663 break;
668 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
671 line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
673 int rval;
675 /* Update marks, @ and global commands. */
676 rval = 0;
677 if (mark_insdel(sp, op, lno))
678 rval = 1;
679 if (ex_g_insdel(sp, op, lno))
680 rval = 1;
682 return rval;
685 #ifdef USE_DB4_LOGGING
686 #define VI_DB_INIT_LOG DB_INIT_LOG
687 #else
688 #define VI_DB_INIT_LOG 0
689 #endif
692 * PUBLIC: int db_setup __P((SCR *, EXF *));
695 db_setup(SCR *sp, EXF *ep)
697 char path[MAXPATHLEN];
698 int fd;
699 DB_ENV *env;
701 (void)snprintf(path, sizeof(path), "%s/vi.XXXXXX", O_STR(sp, O_RECDIR));
702 if ((fd = mkstemp(path)) == -1) {
703 msgq(sp, M_SYSERR, "%s", path);
704 goto err;
706 (void)close(fd);
707 (void)unlink(path);
708 if (mkdir(path, S_IRWXU)) {
709 msgq(sp, M_SYSERR, "%s", path);
710 goto err;
712 if (db_env_create(&env, 0)) {
713 msgq(sp, M_ERR, "env_create");
714 goto err;
716 #ifdef USE_DB4_LOGGING
717 if ((sp->db_error = vi_db_init_recover(env))) {
718 msgq(sp, M_DBERR, "init_recover");
719 goto err;
721 if ((sp->db_error = __vi_init_recover(env))) {
722 msgq(sp, M_DBERR, "init_recover");
723 goto err;
725 #endif
726 if ((sp->db_error = db_env_open(env, path,
727 DB_PRIVATE | DB_CREATE | DB_INIT_MPOOL | VI_DB_THREAD
728 | VI_DB_INIT_LOG, 0)) != 0) {
729 msgq(sp, M_DBERR, "env->open");
730 goto err;
733 if ((ep->env_path = strdup(path)) == NULL) {
734 msgq(sp, M_SYSERR, NULL);
735 (void)rmdir(path);
736 goto err;
738 ep->env = env;
739 return 0;
740 err:
741 return 1;
744 /* Round up v to the nearest power of 2 */
745 static size_t round_up(size_t v)
747 ssize_t old_v = v;
749 while (v) {
750 old_v = v;
751 v ^= v & -v;
753 return old_v << 1;
757 * PUBLIC: int db_msg_open __P((SCR *, char *, DB **));
759 int db_msg_open(SCR *sp, char *file, DB **dbp)
761 return (sp->db_error = db_create(dbp, 0, 0)) != 0 ||
762 (sp->db_error = (*dbp)->set_re_source(*dbp, file)) != 0 ||
763 (sp->db_error = db_open(*dbp, NULL, DB_RECNO, 0, 0)) != 0;
767 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
770 db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
772 if (db_setup(sp, ep))
773 return 1;
775 /* Open a db structure. */
776 if ((sp->db_error = db_create(&ep->db, 0, 0)) != 0) {
777 msgq(sp, M_DBERR, "db_create");
778 return 1;
781 ep->db->set_re_delim(ep->db, '\n'); /* Always set. */
782 ep->db->set_pagesize(ep->db, round_up(psize));
783 ep->db->set_flags(ep->db, DB_RENUMBER | DB_SNAPSHOT);
784 if (rcv_name == NULL)
785 ep->db->set_re_source(ep->db, oname);
788 * Don't let db use mmap when using fcntl for locking
790 #ifdef HAVE_LOCK_FCNTL
791 #define NOMMAPIFFCNTL DB_NOMMAP
792 #else
793 #define NOMMAPIFFCNTL 0
794 #endif
796 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
798 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
799 ((rcv_name == 0) ? DB_TRUNCATE : 0) | VI_DB_THREAD | NOMMAPIFFCNTL,
800 _DB_OPEN_MODE)) != 0) {
801 msgq_str(sp,
802 M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
804 * !!!
805 * Historically, vi permitted users to edit files that couldn't
806 * be read. This isn't useful for single files from a command
807 * line, but it's quite useful for "vi *.c", since you can skip
808 * past files that you can't read.
810 ep->db = NULL; /* Don't close it; it wasn't opened */
812 *open_err = 1;
813 return 1;
816 /* re_source is loaded into the database.
817 * Close it and reopen it in the environment.
819 if ((sp->db_error = ep->db->close(ep->db, 0))) {
820 msgq(sp, M_DBERR, "close");
821 return 1;
823 if ((sp->db_error = db_create(&ep->db, ep->env, 0)) != 0) {
824 msgq(sp, M_DBERR, "db_create 2");
825 return 1;
827 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
828 VI_DB_THREAD | NOMMAPIFFCNTL, _DB_OPEN_MODE)) != 0) {
829 msgq_str(sp,
830 M_DBERR, ep->rcv_path, "%s");
831 return 1;
834 return 0;