2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: exf.c,v 8.62 1993/12/21 11:58:31 bostic Exp $ (Berkeley) $Date: 1993/12/21 11:58:31 $";
12 #include <sys/param.h>
16 * We include <sys/file.h>, because the flock(2) #defines were
17 * found there on historical systems. We also include <fcntl.h>
18 * because the open(2) #defines are found there on newer systems.
30 #include "pathnames.h"
34 * Insert a file name into the FREF list, if it doesn't already
38 * The "if it doesn't already appear" changes vi's semantics slightly. If
39 * you do a "vi foo bar", and then execute "next bar baz", the edit of bar
40 * will reflect the line/column of the previous edit session. Historic nvi
41 * did not do this. The change is a logical extension of the change where
42 * vi now remembers the last location in any file that it has ever edited,
43 * not just the previously edited file.
46 file_add(sp
, frp_append
, name
, ignore
)
56 * Return it if it already exists. Note that we test against the
57 * user's current name, whatever that happens to be, including if
58 * it's a temporary file. If the user is trying to set an argument
59 * list, the ignore argument will be on -- if we're ignoring the
60 * file turn off the ignore bit, so it's back in the argument list.
63 for (frp
= sp
->frefq
.cqh_first
;
64 frp
!= (FREF
*)&sp
->frefq
; frp
= frp
->q
.cqe_next
)
65 if ((p
= FILENAME(frp
)) != NULL
&& !strcmp(p
, name
)) {
67 F_CLR(frp
, FR_IGNORE
);
71 /* Allocate and initialize the FREF structure. */
72 CALLOC(sp
, frp
, FREF
*, 1, sizeof(FREF
));
77 * If no file name specified, or if the file name is a request
78 * for something temporary, file_init() will allocate the file
79 * name. Temporary files are always ignored.
81 #define TEMPORARY_FILE_STRING "/tmp"
82 if (name
!= NULL
&& strcmp(name
, TEMPORARY_FILE_STRING
) &&
83 (frp
->name
= strdup(name
)) == NULL
) {
84 FREE(frp
, sizeof(FREF
));
85 msgq(sp
, M_SYSERR
, NULL
);
89 /* Only the initial argument list is "remembered". */
91 F_SET(frp
, FR_IGNORE
);
93 /* Append into the chain of file names. */
94 if (frp_append
!= NULL
) {
95 CIRCLEQ_INSERT_AFTER(&sp
->frefq
, frp_append
, frp
, q
);
97 CIRCLEQ_INSERT_TAIL(&sp
->frefq
, frp
, q
);
104 * Return the first file name for editing, if any.
112 /* Return the first file name. */
113 for (frp
= sp
->frefq
.cqh_first
;
114 frp
!= (FREF
*)&sp
->frefq
; frp
= frp
->q
.cqe_next
)
115 if (!F_ISSET(frp
, FR_IGNORE
))
122 * Return the next file name, if any.
129 while ((frp
= frp
->q
.cqe_next
) != (FREF
*)&sp
->frefq
)
130 if (!F_ISSET(frp
, FR_IGNORE
))
137 * Return the previous file name, if any.
144 while ((frp
= frp
->q
.cqe_prev
) != (FREF
*)&sp
->frefq
)
145 if (!F_ISSET(frp
, FR_IGNORE
))
152 * Return if there are files that aren't ignored and are unedited.
160 /* Return the next file name. */
161 for (frp
= sp
->frefq
.cqh_first
;
162 frp
!= (FREF
*)&sp
->frefq
; frp
= frp
->q
.cqe_next
)
163 if (!F_ISSET(frp
, FR_EDITED
| FR_IGNORE
))
170 * Start editing a file, based on the FREF structure. If successsful,
171 * let go of any previous file. Don't release the previous file until
172 * absolutely sure we have the new one.
175 file_init(sp
, frp
, rcv_name
, force
)
186 char *p
, *oname
, tname
[MAXPATHLEN
];
189 * Required ep initialization:
190 * Flush the line caches.
191 * Default recover mail file fd to -1.
192 * Set initial EXF flag bits.
194 CALLOC_RET(sp
, ep
, EXF
*, 1, sizeof(EXF
));
195 ep
->c_lno
= ep
->c_nlines
= OOBLNO
;
197 LIST_INIT(&ep
->marks
);
198 F_SET(ep
, F_FIRSTMODIFY
);
201 * If no name or backing file, create a backing temporary file, saving
202 * the temp file name so can later unlink it. Repoint the name to the
203 * temporary name (we display it to the user until they rename it).
204 * There are some games we play with the FR_FREE_TNAME and FR_NONAME
205 * flags (see ex/ex_file.c) to make sure that the temporary memory gets
208 if ((oname
= FILENAME(frp
)) == NULL
|| stat(oname
, &sb
)) {
209 (void)snprintf(tname
, sizeof(tname
),
210 "%s/vi.XXXXXX", O_STR(sp
, O_DIRECTORY
));
211 if ((fd
= mkstemp(tname
)) == -1) {
212 msgq(sp
, M_SYSERR
, "Temporary file");
216 if ((frp
->tname
= strdup(tname
)) == NULL
) {
217 msgq(sp
, M_SYSERR
, NULL
);
223 F_SET(frp
, FR_NEWFILE
);
225 /* Try to keep it at 10 pages or less per file. */
226 if (sb
.st_size
< 40 * 1024)
228 else if (sb
.st_size
< 320 * 1024)
233 frp
->mtime
= sb
.st_mtime
;
236 /* Set up recovery. */
237 memset(&oinfo
, 0, sizeof(RECNOINFO
));
238 oinfo
.bval
= '\n'; /* Always set. */
240 oinfo
.flags
= F_ISSET(sp
->gp
, G_SNAPSHOT
) ? R_SNAPSHOT
: 0;
241 if (rcv_name
== NULL
) {
242 if (rcv_tmp(sp
, ep
, FILENAME(frp
)))
244 "Modifications not recoverable if the system crashes.");
246 oinfo
.bfname
= ep
->rcv_path
;
247 } else if ((ep
->rcv_path
= strdup(rcv_name
)) == NULL
) {
248 msgq(sp
, M_SYSERR
, NULL
);
251 oinfo
.bfname
= ep
->rcv_path
;
252 F_SET(ep
, F_MODIFIED
| F_RCV_ON
);
255 /* Open a db structure. */
256 if ((ep
->db
= dbopen(rcv_name
== NULL
? oname
: NULL
,
257 O_NONBLOCK
| O_RDONLY
, DEFFILEMODE
, DB_RECNO
, &oinfo
)) == NULL
) {
258 msgq(sp
, M_SYSERR
, rcv_name
== NULL
? oname
: rcv_name
);
262 /* Init file marks. */
263 if (mark_init(sp
, ep
))
267 if (log_init(sp
, ep
))
271 * The -R flag, or doing a "set readonly" during a session causes
272 * all files edited during the session (using an edit command, or
273 * even using tags) to be marked read-only. Changing the file name
274 * (see ex/ex_file.c), clears this flag.
276 * Otherwise, try and figure out if a file is readonly. This is a
277 * dangerous thing to do. The kernel is the only arbiter of whether
278 * or not a file is writeable, and the best that a user program can
279 * do is guess. Obvious loopholes are files that are on a file system
280 * mounted readonly (access catches this one on a few systems), or
281 * alternate protection mechanisms, ACL's for example, that we can't
282 * portably check. Lots of fun, and only here because users whined.
285 * Historic vi displayed the readonly message if none of the file
286 * write bits were set, or if an an access(2) call on the path
287 * failed. This seems reasonable. If the file is mode 444, root
288 * users may want to know that the owner of the file did not expect
291 * Historic vi set the readonly bit if no write bits were set for
292 * a file, even if the access call would have succeeded. This makes
293 * the superuser force the write even when vi expects that it will
294 * succeed. I'm less supportive of this semantic, but it's historic
295 * practice and the conservative approach to vi'ing files as root.
297 * It would be nice if there was some way to update this when the user
298 * does a "^Z; chmod ...". The problem is that we'd first have to
299 * distinguish between readonly bits set because of file permissions
300 * and those set for other reasons. That's not too hard, but deciding
301 * when to reevaluate the permissions is trickier. An alternative
302 * might be to turn off the readonly bit if the user forces a write
306 * Access(2) doesn't consider the effective uid/gid values. This
307 * probably isn't a problem for vi when it's running standalone.
309 if (O_ISSET(sp
, O_READONLY
) || !F_ISSET(frp
, FR_NEWFILE
) &&
310 (!(sb
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) ||
311 access(FILENAME(frp
), W_OK
)))
312 F_SET(frp
, FR_RDONLY
);
314 F_CLR(frp
, FR_RDONLY
);
317 * Close the previous file; if that fails, close the new one
318 * and run for the border.
320 if (sp
->ep
!= NULL
&& file_end(sp
, sp
->ep
, force
)) {
321 (void)file_end(sp
, ep
, 1);
326 * 4.4BSD supports locking in the open call, other systems don't.
327 * Since the user can't interrupt us between the open and here,
331 * We need to distinguish a lock not being available for the file
332 * from the file system not supporting locking. Assume that EAGAIN
333 * is the former. There isn't a portable way to do this.
336 * The locking is flock(2) style, not fcntl(2). The latter is known
337 * to fail badly on some systems, and its only advantage is that it
338 * occasionally works over NFS.
340 if (flock(ep
->db
->fd(ep
->db
), LOCK_EX
| LOCK_NB
))
341 if (errno
== EAGAIN
) {
343 "%s already locked, session is read-only", oname
);
344 F_SET(frp
, FR_RDONLY
);
346 msgq(sp
, M_VINFO
, "%s cannot be locked", oname
);
349 * Set the previous file pointer and the alternate file name to be
350 * the file we're about to discard.
353 * If the current file was a temporary file, the call to file_end()
354 * unlinked it and free'd the name. So, there is no previous file,
355 * and there is no alternate file name. This matches historical
356 * practice, although in historical vi it could only happen as the
357 * result of the initial command, i.e. if vi was execute without a
360 if (sp
->frp
!= NULL
) {
361 p
= FILENAME(sp
->frp
);
369 /* The new file has now been officially edited. */
370 F_SET(frp
, FR_EDITED
);
378 err
: if (frp
->tname
!= NULL
) {
379 (void)unlink(frp
->tname
);
383 if (ep
->rcv_path
!= NULL
) {
387 FREE(ep
, sizeof(EXF
));
393 * Stop editing a file.
396 file_end(sp
, ep
, force
)
405 * sp->ep MAY NOT BE THE SAME AS THE ARGUMENT ep, SO DON'T USE IT!
407 * Save the cursor location.
410 * It would be cleaner to do this somewhere else, but by the time
411 * ex or vi knows that we're changing files it's already happened.
416 F_SET(frp
, FR_CURSORSET
);
418 /* If multiply referenced, just decrement the count and return. */
419 if (--ep
->refcnt
!= 0)
422 /* Close the db structure. */
423 if (ep
->db
->close
!= NULL
&& ep
->db
->close(ep
->db
) && !force
) {
425 "%s: close: %s", FILENAME(frp
), strerror(errno
));
429 /* COMMITTED TO THE CLOSE. THERE'S NO GOING BACK... */
432 (void)log_end(sp
, ep
);
434 /* Free up any marks. */
438 * Delete the recovery files, close the open descriptor,
439 * free recovery memory.
441 if (!F_ISSET(ep
, F_RCV_NORM
)) {
442 if (ep
->rcv_path
!= NULL
&& unlink(ep
->rcv_path
))
444 "%s: remove: %s", ep
->rcv_path
, strerror(errno
));
445 if (ep
->rcv_mpath
!= NULL
&& unlink(ep
->rcv_mpath
))
447 "%s: remove: %s", ep
->rcv_mpath
, strerror(errno
));
449 if (ep
->rcv_fd
!= -1)
450 (void)close(ep
->rcv_fd
);
451 if (ep
->rcv_path
!= NULL
)
453 if (ep
->rcv_mpath
!= NULL
)
457 * Unlink any temporary file, file name. We also turn on the
458 * ignore bit at this point, because it was a "created" file,
459 * not an argument file.
461 if (frp
->tname
!= NULL
) {
462 if (unlink(frp
->tname
))
464 "%s: remove: %s", frp
->tname
, strerror(errno
));
468 if (frp
->name
== NULL
&& frp
->cname
== NULL
)
469 F_SET(frp
, FR_IGNORE
);
471 /* Free the EXF structure. */
472 FREE(ep
, sizeof(EXF
));
478 * Write the file to disk. Historic vi had fairly convoluted
479 * semantics for whether or not writes would happen. That's
483 file_write(sp
, ep
, fm
, tm
, name
, flags
)
495 int fd
, oflags
, rval
;
499 * Don't permit writing to temporary files. The problem is that
500 * if it's a temp file, and the user does ":wq", we write and quit,
501 * unlinking the temporary file. Not what the user had in mind
502 * at all. This test cannot be forced.
505 if (name
== NULL
&& frp
->cname
== NULL
&& frp
->name
== NULL
) {
506 msgq(sp
, M_ERR
, "No filename to which to write.");
510 /* Can't write files marked read-only, unless forced. */
511 if (!LF_ISSET(FS_FORCE
) &&
512 name
== NULL
&& F_ISSET(frp
, FR_RDONLY
)) {
513 if (LF_ISSET(FS_POSSIBLE
))
515 "Read-only file, not written; use ! to override.");
518 "Read-only file, not written.");
522 /* If not forced, not appending, and "writeany" not set ... */
523 if (!LF_ISSET(FS_FORCE
| FS_APPEND
) && !O_ISSET(sp
, O_WRITEANY
)) {
524 /* Don't overwrite anything but the original file. */
526 if (!stat(name
, &sb
))
528 } else if (frp
->cname
!= NULL
&&
529 !F_ISSET(frp
, FR_CHANGEWRITE
) && !stat(frp
->cname
, &sb
)) {
531 exists
: if (LF_ISSET(FS_POSSIBLE
))
533 "%s exists, not written; use ! to override.", name
);
536 "%s exists, not written.", name
);
541 * Don't write part of any existing file. Only test for the
542 * original file, the previous test catches anything else.
544 if (!LF_ISSET(FS_ALL
) && name
== NULL
&&
545 frp
->cname
== NULL
&& !stat(frp
->name
, &sb
)) {
546 if (LF_ISSET(FS_POSSIBLE
))
548 "Use ! to write a partial file.");
550 msgq(sp
, M_ERR
, "Partial file, not written.");
556 * Figure out if the file already exists -- if it doesn't, we display
557 * the "new file" message. The stat might not be necessary, but we
558 * just repeat it because it's easier than hacking the previous tests.
559 * The information is only used for the user message and modification
560 * time test, so we can ignore the obvious race condition.
562 * If the user is overwriting a file other than the original file, and
563 * O_WRITEANY was what got us here (neither force nor append was set),
564 * display the "existing file" messsage. Since the FR_CHANGEWRITE flag
565 * is set on a successful write, the message only appears once when the
566 * user changes a file name. This is historic practice.
568 * One final test. If we're not forcing or appending, and we have a
569 * saved modification time, stop the user if it's been written since
570 * we last edited or wrote it, and make them force it.
572 if (stat(name
== NULL
? FILENAME(frp
) : name
, &sb
))
576 if (!LF_ISSET(FS_FORCE
| FS_APPEND
)) {
577 if (frp
->mtime
&& sb
.st_mtime
> frp
->mtime
) {
579 "%s: file modified more recently than this copy%s.",
580 name
== NULL
? frp
->name
: name
,
581 LF_ISSET(FS_POSSIBLE
) ?
582 "; use ! to override" : "");
586 !F_ISSET(frp
, FR_CHANGEWRITE
) && frp
->cname
!= NULL
)
587 msg
= ": existing file";
591 /* We no longer care where the name came from. */
593 name
= FILENAME(frp
);
595 /* Set flags to either append or truncate. */
596 oflags
= O_CREAT
| O_WRONLY
;
597 if (LF_ISSET(FS_APPEND
))
603 if ((fd
= open(name
, oflags
, DEFFILEMODE
)) < 0) {
604 msgq(sp
, M_SYSERR
, name
);
608 /* Use stdio for buffering. */
609 if ((fp
= fdopen(fd
, "w")) == NULL
) {
611 msgq(sp
, M_SYSERR
, name
);
615 /* Build fake addresses, if necessary. */
620 if (file_lline(sp
, ep
, &to
.lno
))
626 /* Write the file. */
627 rval
= ex_writefp(sp
, ep
, name
, fp
, fm
, tm
, &nlno
, &nch
);
630 * Save the new last modification time -- even if the write fails
631 * we re-init the time if we wrote anything. That way the user can
632 * clean up the disk and rewrite without having to force it.
635 frp
->mtime
= stat(name
, &sb
) ? 0 : sb
.st_mtime
;
637 /* If the write failed, complain loudly. */
639 if (!LF_ISSET(FS_APPEND
))
640 msgq(sp
, M_ERR
, "%s: WARNING: file truncated!", name
);
645 * Once we've actually written the file, it doesn't matter that the
646 * file name was changed -- if it was, we've already whacked it.
648 F_SET(frp
, FR_CHANGEWRITE
);
650 /* If wrote the entire file, clear the modified bit. */
651 if (LF_ISSET(FS_ALL
))
652 F_CLR(ep
, F_MODIFIED
);
654 msgq(sp
, M_INFO
, "%s%s: %lu line%s, %lu characters.",
655 name
, msg
, nlno
, nlno
== 1 ? "" : "s", nch
);