* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)exf.c 9.14 (Berkeley) 12/2/94";
* We include <sys/file.h>, because the flock(2) and open(2) #defines
* were found there on historical systems. We also include <fcntl.h>
* because the open(2) #defines are found there on newer systems.
static int file_backup
__P((SCR
*, char *, char *));
* Insert a file name into the FREF list, if it doesn't already
* The "if it doesn't already appear" changes vi's semantics slightly. If
* you do a "vi foo bar", and then execute "next bar baz", the edit of bar
* will reflect the line/column of the previous edit session. Historic nvi
* did not do this. The change is a logical extension of the change where
* vi now remembers the last location in any file that it has ever edited,
* not just the previously edited file.
* Return it if it already exists. Note that we test against the
* user's name, whatever that happens to be, including if it's a
for (frp
= sp
->frefq
.cqh_first
;
frp
!= (FREF
*)&sp
->frefq
; frp
= frp
->q
.cqe_next
)
if (!strcmp(frp
->name
, name
))
/* Allocate and initialize the FREF structure. */
CALLOC(sp
, frp
, FREF
*, 1, sizeof(FREF
));
* If no file name specified, or if the file name is a request
* for something temporary, file_init() will allocate the file
* name. Temporary files are always ignored.
if (name
!= NULL
&& strcmp(name
, TEMPORARY_FILE_STRING
) &&
(frp
->name
= strdup(name
)) == NULL
) {
msgq(sp
, M_SYSERR
, NULL
);
/* Append into the chain of file names. */
CIRCLEQ_INSERT_TAIL(&sp
->frefq
, frp
, q
);
* Start editing a file, based on the FREF structure. If successsful,
* let go of any previous file. Don't release the previous file until
* absolutely sure we have the new one.
file_init(sp
, frp
, rcv_name
, flags
)
char *p
, *oname
, tname
[MAXPATHLEN
];
* If the file is a recovery file, let the recovery code handle it.
* Clear the FR_RECOVER flag first -- the recovery code does set up,
* and then calls us! If the recovery call fails, it's probably
* because the named file doesn't exist. So, move boldly forward,
* presuming that there's an error message the user will get to see.
if (F_ISSET(frp
, FR_RECOVER
)) {
return (rcv_read(sp
, frp
));
* Required FRP initialization; the only flag we keep is the
F_CLR(frp
, ~(FR_CURSORSET
| FR_FNONBLANK
));
* Required EXF initialization:
* Default recover mail file fd to -1.
* Set initial EXF flag bits.
CALLOC_RET(sp
, ep
, EXF
*, 1, sizeof(EXF
));
ep
->c_lno
= ep
->c_nlines
= OOBLNO
;
ep
->rcv_fd
= ep
->fcntl_fd
= -1;
F_SET(ep
, F_FIRSTMODIFY
);
* If no name or backing file, for whatever reason, create a backing
* temporary file, saving the temp file name so we can later unlink
* it. If the user never named this file, copy the temporary file name
* to the real name (we display that until the user renames it).
if (LF_ISSET(FS_OPENERR
) || oname
== NULL
|| stat(oname
, &sb
)) {
(void)snprintf(tname
, sizeof(tname
),
"%s/vi.XXXXXX", O_STR(sp
, O_DIRECTORY
));
if ((fd
= mkstemp(tname
)) == -1) {
"002|Unable to create temporary file");
if ((frp
->tname
= strdup(tname
)) == NULL
||
frp
->name
== NULL
&& (frp
->name
= strdup(tname
)) == NULL
) {
msgq(sp
, M_SYSERR
, NULL
);
if (!LF_ISSET(FS_OPENERR
))
* Try to keep it at 10 pages or less per file. This
* isn't friendly on a loaded machine, btw.
if (sb
.st_size
< 40 * 1024)
else if (sb
.st_size
< 320 * 1024)
if (!S_ISREG(sb
.st_mode
)) {
p
= msg_print(sp
, oname
, &nf
);
"003|Warning: %s is not a regular file", p
);
memset(&oinfo
, 0, sizeof(RECNOINFO
));
oinfo
.bval
= '\n'; /* Always set. */
oinfo
.flags
= F_ISSET(sp
->gp
, G_SNAPSHOT
) ? R_SNAPSHOT
: 0;
if (!rcv_tmp(sp
, ep
, frp
->name
))
oinfo
.bfname
= ep
->rcv_path
;
if ((ep
->rcv_path
= strdup(rcv_name
)) == NULL
) {
msgq(sp
, M_SYSERR
, NULL
);
oinfo
.bfname
= ep
->rcv_path
;
/* Open a db structure. */
if ((ep
->db
= dbopen(rcv_name
== NULL
? oname
: NULL
,
O_NONBLOCK
| O_RDONLY
, DEFFILEMODE
, DB_RECNO
, &oinfo
)) == NULL
) {
p
= msg_print(sp
, rcv_name
== NULL
? oname
: rcv_name
, &nf
);
msgq(sp
, M_SYSERR
, "%s", p
);
* Historically, vi permitted users to edit files that couldn't
* be read. This isn't useful for single files from a command
* line, but it's quite useful for "vi *.c", since you can skip
* past files that you can't read.
* Do the remaining things that can cause failure of the new file,
* mark and logging initialization.
if (mark_init(sp
, ep
) || log_init(sp
, ep
))
* Set the alternate file name to be the file we're discarding.
* Temporary files can't become alternate files, so there's no file
* name. This matches historical practice, although it could only
* happen in historical vi as the result of the initial command, i.e.
* if vi was executed without a file name.
set_alt_name(sp
, sp
->frp
== NULL
||
F_ISSET(sp
->frp
, FR_TMPFILE
) ? NULL
: sp
->frp
->name
);
* Close the previous file; if that fails, close the new one and run
* There's a nasty special case. If the user edits a temporary file,
* and then does an ":e! %", we need to re-initialize the backing
* file, but we can't change the name. (It's worse -- we're dealing
* with *names* here, we can't even detect that it happened.) Set a
* flag so that the file_end routine ignores the backing information
* of the old file if it happens to be the same as the new one.
* Side-effect: after the call to file_end(), sp->frp may be NULL.
F_SET(frp
, FR_DONTDELETE
);
if (sp
->ep
!= NULL
&& file_end(sp
, NULL
, LF_ISSET(FS_FORCE
))) {
(void)file_end(sp
, ep
, 1);
F_CLR(frp
, FR_DONTDELETE
);
* Lock the file; if it's a recovery file, it should already be
* locked. Note, we acquire the lock after the previous file
* has been ended, so that we don't get an "already locked" error
* While the user can't interrupt us between the open and here,
* there's a race between the dbopen() and the lock. Not much
* We don't make a big deal of not being able to lock the file. As
* locking rarely works over NFS, and often fails if the file was
* mmap(2)'d, it's far too common to do anything like print an error
* message, let alone make the file readonly. At some future time,
* when locking is a little more reliable, this should change to be
switch (file_lock(sp
, oname
,
&ep
->fcntl_fd
, ep
->db
->fd(ep
->db
), 0)) {
p
= msg_print(sp
, oname
, &nf
);
"004|%s already locked, session is read-only", p
);
* The -R flag, or doing a "set readonly" during a session causes
* all files edited during the session (using an edit command, or
* even using tags) to be marked read-only. Changing the file name
* (see ex/ex_file.c), clears this flag.
* Otherwise, try and figure out if a file is readonly. This is a
* dangerous thing to do. The kernel is the only arbiter of whether
* or not a file is writeable, and the best that a user program can
* do is guess. Obvious loopholes are files that are on a file system
* mounted readonly (access catches this one on a few systems), or
* alternate protection mechanisms, ACL's for example, that we can't
* portably check. Lots of fun, and only here because users whined.
* Historic vi displayed the readonly message if none of the file
* write bits were set, or if an an access(2) call on the path
* failed. This seems reasonable. If the file is mode 444, root
* users may want to know that the owner of the file did not expect
* Historic vi set the readonly bit if no write bits were set for
* a file, even if the access call would have succeeded. This makes
* the superuser force the write even when vi expects that it will
* succeed. I'm less supportive of this semantic, but it's historic
* practice and the conservative approach to vi'ing files as root.
* It would be nice if there was some way to update this when the user
* does a "^Z; chmod ...". The problem is that we'd first have to
* distinguish between readonly bits set because of file permissions
* and those set for other reasons. That's not too hard, but deciding
* when to reevaluate the permissions is trickier. An alternative
* might be to turn off the readonly bit if the user forces a write
* Access(2) doesn't consider the effective uid/gid values. This
* probably isn't a problem for vi when it's running standalone.
if (O_ISSET(sp
, O_READONLY
) || !F_ISSET(frp
, FR_NEWFILE
) &&
(!(sb
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) ||
access(frp
->name
, W_OK
)))
* Note, because the EXF structure is examined at interrupt time,
* the underlying DB structures have to be consistent as soon as
* it's assigned to an SCR structure.
/* Set the initial cursor position. */
/* Redraw the screen from scratch. */
F_SET(sp
, S_SCR_REFORMAT
);
/* Display file statistics. */
return (msg_status(sp
, sp
->lno
, 0));
err
: if (frp
->name
!= NULL
) {
if (frp
->tname
!= NULL
) {
(void)unlink(frp
->tname
);
oerr
: if (F_ISSET(ep
, F_RCV_ON
))
(void)unlink(ep
->rcv_path
);
if (ep
->rcv_path
!= NULL
) {
(void)ep
->db
->close(ep
->db
);
file_init(sp
, frp
, rcv_name
, flags
| FS_OPENERR
) : 1);
* Set up the initial cursor position.
* If in ex mode, move to the last line, first nonblank character.
* Otherwise, if the file has previously been edited, move to the
* last position, and check it for validity. Otherwise, move to
* the first line, first nonblank. This gets called by some the
* file init code, because we may be in a file of ex commands and
* we want to execute them from the right location in the file. A
* few other places that want special case behavior also call here.
/* XXX: If this fails, we're toast. */
(void)file_lline(sp
, &sp
->lno
);
if (F_ISSET(sp
->frp
, FR_CURSORSET
)) {
if (F_ISSET(sp
->frp
, FR_FNONBLANK
))
/* If returning to a file in vi, center the line. */
if (file_gline(sp
, sp
->lno
, &len
) == NULL
) {
if (!nb
&& sp
->cno
> len
)
(void)nonblank(sp
, sp
->lno
, &sp
->cno
);
* Historically, vi initialized the absolute mark, but ex did not.
* Which meant, that if the first command in ex mode was "visual",
* or if an ex command was executed first (e.g. vi +10 file) vi was
* entered without the mark being initialized. For consistency, if
* the file isn't empty, we initialize it for everyone, believing
* that it can't hurt, and is generally useful. Not initializing it
* if the file is empty is historic practice, although it has always
* been possible to set (and use) marks in empty vi files.
(void)mark_set(sp
, ABSMARK1
, &m
, 0);
* ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
* (If argument ep is NULL, use sp->ep.)
* Clean up the FREF structure.
* Save the cursor location.
* It would be cleaner to do this somewhere else, but by the time
* ex or vi knows that we're changing files it's already happened.
F_SET(frp
, FR_CURSORSET
);
* We may no longer need the temporary backing file, so clean it
* up. We don't need the FREF structure either, if the file was
* never named, so lose it.
* Re: FR_DONTDELETE, see the comment above in file_init().
if (!F_ISSET(frp
, FR_DONTDELETE
) && frp
->tname
!= NULL
) {
if (unlink(frp
->tname
)) {
p
= msg_print(sp
, frp
->tname
, &nf
);
msgq(sp
, M_SYSERR
, "005|%s: remove", p
);
if (F_ISSET(frp
, FR_TMPFILE
)) {
CIRCLEQ_REMOVE(&sp
->frefq
, frp
, q
);
* Clean up the EXF structure.
* If multiply referenced, just decrement the count and return.
/* Close the db structure. */
if (ep
->db
->close
!= NULL
&& ep
->db
->close(ep
->db
) && !force
) {
p
= msg_print(sp
, frp
->name
, &nf
);
msgq(sp
, M_SYSERR
, "006|%s: close", p
);
/* COMMITTED TO THE CLOSE. THERE'S NO GOING BACK... */
* Delete recovery files, close the open descriptor, free recovery
* memory. See recover.c for a description of the protocol.
* Unlink backup file first, we can detect that the recovery file
* doesn't reference anything when the user tries to recover it.
* There's a race, here, obviously, but it's fairly small.
if (!F_ISSET(ep
, F_RCV_NORM
)) {
if (ep
->rcv_path
!= NULL
&& unlink(ep
->rcv_path
)) {
p
= msg_print(sp
, ep
->rcv_path
, &nf
);
msgq(sp
, M_SYSERR
, "007|%s: remove", p
);
if (ep
->rcv_mpath
!= NULL
&& unlink(ep
->rcv_mpath
)) {
p
= msg_print(sp
, ep
->rcv_mpath
, &nf
);
msgq(sp
, M_SYSERR
, "008|%s: remove", p
);
(void)close(ep
->fcntl_fd
);
if (ep
->rcv_path
!= NULL
)
if (ep
->rcv_mpath
!= NULL
)
* Write the file to disk. Historic vi had fairly convoluted
* semantics for whether or not writes would happen. That's
file_write(sp
, fm
, tm
, name
, flags
)
enum { NEWFILE
, NONE
, EXISTING
} mtype
;
int btear
, fd
, nf
, noname
, oflags
, rval
;
* Writing '%', or naming the current file explicitly, has the
* same semantics as writing without a name.
if (name
== NULL
|| !strcmp(name
, frp
->name
)) {
/* Can't write files marked read-only, unless forced. */
if (!LF_ISSET(FS_FORCE
) && noname
&& F_ISSET(frp
, FR_RDONLY
)) {
if (LF_ISSET(FS_POSSIBLE
))
"009|Read-only file, not written; use ! to override");
msgq(sp
, M_ERR
, "010|Read-only file, not written");
/* If not forced, not appending, and "writeany" not set ... */
if (!LF_ISSET(FS_FORCE
| FS_APPEND
) && !O_ISSET(sp
, O_WRITEANY
)) {
/* Don't overwrite anything but the original file. */
if ((!noname
|| F_ISSET(frp
, FR_NAMECHANGE
)) &&
p
= msg_print(sp
, name
, &nf
);
if (LF_ISSET(FS_POSSIBLE
)) {
"011|%s exists, not written; use ! to override", p
);
"012|%s exists, not written", p
);
* Don't write part of any existing file. Only test for the
* original file, the previous test catches anything else.
if (!LF_ISSET(FS_ALL
) && noname
&& !stat(name
, &sb
)) {
if (LF_ISSET(FS_POSSIBLE
))
"013|Use ! to write a partial file");
"014|Partial file, not written");
* Figure out if the file already exists -- if it doesn't, we display
* the "new file" message. The stat might not be necessary, but we
* just repeat it because it's easier than hacking the previous tests.
* The information is only used for the user message and modification
* time test, so we can ignore the obvious race condition.
* If the user is overwriting a file other than the original file, and
* O_WRITEANY was what got us here (neither force nor append was set),
* display the "existing file" messsage. Since the FR_NAMECHANGE flag
* is cleared on a successful write, the message only appears once when
* the user changes a file name. This is historic practice.
* One final test. If we're not forcing or appending, and we have a
* saved modification time, object if the file was changed since we
* last edited or wrote it, and make them force it.
if (!LF_ISSET(FS_FORCE
| FS_APPEND
)) {
(sb
.st_dev
!= sp
->ep
->mdev
||
sb
.st_ino
!= ep
->minode
||
sb
.st_mtime
!= ep
->mtime
)) {
p
= msg_print(sp
, name
, &nf
);
"016|%s: file modified more recently than this copy; use ! to override" :
"017|%s: file modified more recently than this copy",
if (!noname
|| F_ISSET(frp
, FR_NAMECHANGE
))
/* Set flags to either append or truncate. */
oflags
= O_CREAT
| O_WRONLY
;
/* Backup the file if requested. */
if (p
[0] != '\0' && file_backup(sp
, name
, p
) && !LF_ISSET(FS_FORCE
))
if ((fd
= open(name
, oflags
, DEFFILEMODE
)) < 0) {
p
= msg_print(sp
, name
, &nf
);
msgq(sp
, M_SYSERR
, "%s", p
);
/* Try and get a lock. */
if (!noname
&& file_lock(sp
, NULL
, NULL
, fd
, 0) == LOCK_UNAVAIL
)
msgq(sp
, M_ERR
, "265|%s: write lock was unavailable", name
);
* In libc 4.5.x, fdopen(fd, "w") clears the O_APPEND flag (if set).
* This bug is fixed in libc 4.6.x.
* This code works around this problem for libc 4.5.x users.
* Note that this code is harmless if you're using libc 4.6.x.
if (LF_ISSET(FS_APPEND
) && lseek(fd
, (off_t
)0, SEEK_END
) < 0) {
msgq(sp
, M_SYSERR
, name
);
/* Use stdio for buffering. */
if ((fp
= fdopen(fd
, "w")) == NULL
) {
p
= msg_print(sp
, name
, &nf
);
msgq(sp
, M_SYSERR
, "%s", p
);
/* Build fake addresses, if necessary. */
if (file_lline(sp
, &to
.lno
))
/* Turn on the busy message. */
btear
= F_ISSET(sp
, S_EXSILENT
) ? 0 : !busy_on(sp
, "Writing...");
rval
= ex_writefp(sp
, name
, fp
, fm
, tm
, &nlno
, &nch
);
* Save the new last modification time -- even if the write fails
* we re-init the time. That way the user can clean up the disk
* and rewrite without having to force it.
/* If the write failed, complain loudly. */
if (!LF_ISSET(FS_APPEND
)) {
p
= msg_print(sp
, name
, &nf
);
"019|%s: WARNING: FILE TRUNCATED", p
);
* Once we've actually written the file, it doesn't matter that the
* file name was changed -- if it was, we've already whacked it.
F_CLR(frp
, FR_NAMECHANGE
);
* If wrote the entire file clear the modified bit. If the file was
* written back to the original file name and the file is a temporary,
* set the "no exit" bit. This permits the user to write the file and
* use it in the context of the file system, but still keeps them from
* losing their changes by exiting.
F_CLR(sp
->ep
, F_MODIFIED
);
if (F_ISSET(frp
, FR_TMPFILE
))
p
= msg_print(sp
, name
, &nf
);
"015|Interrupted write: %s: existing file: %lu lines, %lu characters",
"018|Interrupted write: %s: new file: %lu lines, %lu characters",
"020|Interrupted write: %s: %lu lines, %lu characters",
"025|%s: existing file: %lu lines, %lu characters",
"092|%s: new file: %lu lines, %lu characters",
"093|%s: %lu lines, %lu characters", p
, nlno
, nch
);
* Backup the about-to-be-written file.
* We do the backup by copying the entire file. It would be nice to do
* a rename instead, but: (1) both files may not fit and we want to fail
* before doing the rename; (2) the backup file may not be on the same
* disk partition as the file being written; (3) there may be optional
* file information (MACs, DACs, whatever) that we won't get right if we
* recreate the file. So, let's not risk it.
file_backup(sp
, name
, bname
)
int flags
, maxnum
, nf
, nr
, num
, nw
, rfd
, wfd
, version
;
char *bp
, *estr
, *p
, *pct
, *slash
, *t
, *wfname
, buf
[8192];
bp
= estr
= wfname
= NULL
;
* Open the current file for reading. Do this first, so that
* we don't exec a shell before the most likely failure point.
* If it doesn't exist, it's okay, there's just nothing to back
if ((rfd
= open(name
, O_RDONLY
, 0)) < 0) {
* If the name starts with an 'N' character, add a version number
* to the name. Strip the leading N from the string passed to the
* expansion routines, for no particular reason. It would be nice
* to permit users to put the version number anywhere in the backup
* name, but there isn't a special character that we can use in the
* name, and giving a new character a special meaning leads to ugly
* hacks both here and in the supporting ex routines.
* Shell and file name expand the option's value.
ex_cbuild(&cmd
, 0, 0, 0, 0, 0, ap
, &a
, NULL
);
if (argv_exp2(sp
, &cmd
, bname
, strlen(bname
)))
* >1 args: object, too many args.
p
= msg_print(sp
, bname
, &nf
);
"256|%s expanded into too many file names", p
);
* If appending a version number, read through the directory, looking
* for file names that match the name followed by a number. Make all
* of the other % characters in name literal, so the user doesn't get
* surprised and sscanf doesn't drop core indirecting through pointers
* that don't exist. If any such files are found, increment its number
GET_SPACE_GOTO(sp
, bp
, blen
, cmd
.argv
[0]->len
* 2 + 50);
for (t
= bp
, slash
= NULL
,
p
= cmd
.argv
[0]->bp
; p
[0] != '\0'; *t
++ = *p
++)
for (maxnum
= 0; (dp
= readdir(dirp
)) != NULL
;)
if (sscanf(dp
->d_name
, p
, &num
) == 1 && num
> maxnum
)
/* Format the backup file name. */
(void)snprintf(pct
, blen
- (pct
- bp
), "%d", maxnum
+ 1);
wfname
= cmd
.argv
[0]->bp
;
/* Open the backup file, avoiding lurkers. */
if (stat(wfname
, &sb
) == 0) {
if (!S_ISREG(sb
.st_mode
)) {
t
= "257|%s: not a regular file";
if (sb
.st_uid
!= getuid()) {
t
= "258|%s: not owned by you";
if (sb
.st_mode
& (S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
)) {
t
= "259|%s: accessible by a user other than the owner";
perm
: p
= msg_print(sp
, bname
, &nf
);
flags
= O_CREAT
| O_EXCL
;
if ((wfd
= open(wfname
, flags
| O_WRONLY
, S_IRUSR
| S_IWUSR
)) < 0) {
/* Copy the file's current contents to its backup value. */
while ((nr
= read(rfd
, buf
, sizeof(buf
))) > 0)
for (off
= 0; nr
!= 0; nr
-= nw
, off
+= nw
)
if ((nw
= write(wfd
, buf
+ off
, nr
)) < 0) {
FREE_SPACE(sp
, bp
, blen
);
p
= msg_print(sp
, estr
, &nf
);
msgq(sp
, M_SYSERR
, "%s", p
);
FREE_SPACE(sp
, bp
, blen
);
* First modification check routine. The :next, :prev, :rewind, :tag,
* :tagpush, :tagpop, ^^ modifications check.
file_m1(sp
, force
, flags
)
/* If no file loaded, return no modifications. */
* If the file has been modified, we'll want to write it back or
* fail. If autowrite is set, we'll write it back automatically,
* unless force is also set. Otherwise, we fail unless forced or
* there's another open screen on this file.
if (F_ISSET(sp
->ep
, F_MODIFIED
))
if (O_ISSET(sp
, O_AUTOWRITE
)) {
if (!force
&& file_aw(sp
, flags
))
} else if (sp
->ep
->refcnt
<= 1 && !force
) {
msgq(sp
, M_ERR
, LF_ISSET(FS_POSSIBLE
) ?
"021|File modified since last complete write; write or use ! to override" :
"022|File modified since last complete write; write or use :edit! to override");
return (file_m3(sp
, force
));
* Second modification check routine. The :edit, :quit, :recover
/* If no file loaded, return no modifications. */
* If the file has been modified, we'll want to fail, unless forced
* or there's another open screen on this file.
if (F_ISSET(sp
->ep
, F_MODIFIED
) && sp
->ep
->refcnt
<= 1 && !force
) {
"023|File modified since last complete write; write or use ! to override");
return (file_m3(sp
, force
));
* Third modification check routine.
/* If no file loaded, return no modifications. */
* Don't exit while in a temporary files if the file was ever modified.
* The problem is that if the user does a ":wq", we write and quit,
* unlinking the temporary file. Not what the user had in mind at all.
* We permit writing to temporary files, so that user maps using file
* system names work with temporary files.
if (F_ISSET(sp
->frp
, FR_TMPEXIT
) && sp
->ep
->refcnt
<= 1 && !force
) {
"024|File is a temporary; exit will discard modifications");
* Autowrite routine. If modified, autowrite is set and the readonly bit
* is not set, write the file. A routine so there's a place to put the
if (!F_ISSET(sp
->ep
, F_MODIFIED
))
if (!O_ISSET(sp
, O_AUTOWRITE
))
* Historic 4BSD vi attempted to write the file if autowrite was set,
* regardless of the writeability of the file (as defined by the file
* readonly flag). System V changed this as some point, not attempting
* autowrite if the file was readonly. This feels like a bug fix to
* me (e.g. the principle of least surprise is violated if readonly is
* set and vi writes the file), so I'm compatible with System V.
if (F_ISSET(sp
->frp
, FR_RDONLY
)) {
"268|File readonly, modifications not auto-written");
return (file_write(sp
, NULL
, NULL
, NULL
, flags
));
* Get an exclusive lock on a file.
* The default locking is flock(2) style, not fcntl(2). The latter is
* known to fail badly on some systems, and its only advantage is that
* it occasionally works over NFS.
* Furthermore, the semantics of fcntl(2) are wrong. The problems are
* two-fold: you can't close any file descriptor associated with the file
* without losing all of the locks, and you can't get an exclusive lock
* unless you have the file open for writing. Someone ought to be shot,
* but it's probably too late, they may already have reproduced. To get
* around these problems, nvi opens the files for writing when it can and
* acquires a second file descriptor when it can't. The recovery files
* are examples of the former, they're always opened for writing. The DB
* files can't be opened for writing because the semantics of DB are that
* files opened for writing are flushed back to disk when the DB session
* is ended. So, in that case we have to acquire an extra file descriptor.
file_lock(sp
, name
, fdp
, fd
, iswrite
)
if (!O_ISSET(sp
, O_LOCK
))
#if !defined(USE_FCNTL) && defined(LOCK_EX)
/* Hurrah! We've got flock(2). */
* We need to distinguish a lock not being available for the file
* from the file system not supporting locking. Flock is documented
* as returning EWOULDBLOCK; add EAGAIN for good measure, and assume
* they are the former. There's no portable way to do this.
return (flock(fd
, LOCK_EX
| LOCK_NB
) ?
errno
== EAGAIN
|| errno
== EWOULDBLOCK
?
LOCK_UNAVAIL
: LOCK_FAILED
: LOCK_SUCCESS
);
#else /* Gag me. We've got fcntl(2). */
arg
.l_whence
= 0; /* SEEK_SET */
arg
.l_start
= arg
.l_len
= 0;
* If the file descriptor isn't opened for writing, it must fail.
* If we fail because we can't get a read/write file descriptor,
* we return LOCK_SUCCESS, believing that the file is readonly
* and that will be sufficient to warn the user.
if (name
== NULL
|| fdp
== NULL
)
if ((fd
= open(name
, O_RDWR
, 0)) == -1)
if (!fcntl(fd
, F_SETLK
, &arg
))
* We need to distinguish a lock not being available for the file
* from the file system not supporting locking. Fcntl is documented
* as returning EACCESS and EAGAIN; add EWOULDBLOCK for good measure,
* and assume they are the former. There's no portable way to do this.
return (errno
== EACCES
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
?
LOCK_UNAVAIL
: LOCK_FAILED
);