xref: /dflybsd-src/contrib/nvi2/common/exf.c (revision 07bc39c2f4bbca56f12568e06d89da17f2eeb965)
1e0b8e63eSJohn Marino /*-
2e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994
3e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6e0b8e63eSJohn Marino  *
7e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8e0b8e63eSJohn Marino  */
9e0b8e63eSJohn Marino 
10e0b8e63eSJohn Marino #include "config.h"
11e0b8e63eSJohn Marino 
12e0b8e63eSJohn Marino #include <sys/types.h>
13e0b8e63eSJohn Marino #include <sys/queue.h>
14e0b8e63eSJohn Marino #include <sys/stat.h>
15e0b8e63eSJohn Marino #include <sys/time.h>
16e0b8e63eSJohn Marino 
17e0b8e63eSJohn Marino /*
18e0b8e63eSJohn Marino  * We include <sys/file.h>, because the flock(2) and open(2) #defines
19e0b8e63eSJohn Marino  * were found there on historical systems.  We also include <fcntl.h>
20e0b8e63eSJohn Marino  * because the open(2) #defines are found there on newer systems.
21e0b8e63eSJohn Marino  */
22e0b8e63eSJohn Marino #include <sys/file.h>
23e0b8e63eSJohn Marino 
24e0b8e63eSJohn Marino #include <bitstring.h>
25e0b8e63eSJohn Marino #include <dirent.h>
26e0b8e63eSJohn Marino #include <errno.h>
27e0b8e63eSJohn Marino #include <fcntl.h>
28e0b8e63eSJohn Marino #include <limits.h>
29e0b8e63eSJohn Marino #include <stdio.h>
30e0b8e63eSJohn Marino #include <stdlib.h>
31e0b8e63eSJohn Marino #include <string.h>
32e0b8e63eSJohn Marino #include <unistd.h>
33e0b8e63eSJohn Marino 
34e0b8e63eSJohn Marino #include "common.h"
35e0b8e63eSJohn Marino 
36e0b8e63eSJohn Marino static int	file_backup(SCR *, char *, char *);
37e0b8e63eSJohn Marino static void	file_cinit(SCR *);
38e0b8e63eSJohn Marino static void	file_encinit(SCR *);
39e0b8e63eSJohn Marino static void	file_comment(SCR *);
40e0b8e63eSJohn Marino static int	file_spath(SCR *, FREF *, struct stat *, int *);
41e0b8e63eSJohn Marino 
42e0b8e63eSJohn Marino /*
43e0b8e63eSJohn Marino  * file_add --
44e0b8e63eSJohn Marino  *	Insert a file name into the FREF list, if it doesn't already
45e0b8e63eSJohn Marino  *	appear in it.
46e0b8e63eSJohn Marino  *
47e0b8e63eSJohn Marino  * !!!
48e0b8e63eSJohn Marino  * The "if it doesn't already appear" changes vi's semantics slightly.  If
49e0b8e63eSJohn Marino  * you do a "vi foo bar", and then execute "next bar baz", the edit of bar
50e0b8e63eSJohn Marino  * will reflect the line/column of the previous edit session.  Historic nvi
51e0b8e63eSJohn Marino  * did not do this.  The change is a logical extension of the change where
52e0b8e63eSJohn Marino  * vi now remembers the last location in any file that it has ever edited,
53e0b8e63eSJohn Marino  * not just the previously edited file.
54e0b8e63eSJohn Marino  *
55e0b8e63eSJohn Marino  * PUBLIC: FREF *file_add(SCR *, char *);
56e0b8e63eSJohn Marino  */
57e0b8e63eSJohn Marino FREF *
file_add(SCR * sp,char * name)58*b1ac2ebbSDaniel Fojt file_add(SCR *sp, char *name)
59e0b8e63eSJohn Marino {
60e0b8e63eSJohn Marino 	GS *gp;
61e0b8e63eSJohn Marino 	FREF *frp, *tfrp;
62e0b8e63eSJohn Marino 
63e0b8e63eSJohn Marino 	/*
64e0b8e63eSJohn Marino 	 * Return it if it already exists.  Note that we test against the
65e0b8e63eSJohn Marino 	 * user's name, whatever that happens to be, including if it's a
66e0b8e63eSJohn Marino 	 * temporary file.
67e0b8e63eSJohn Marino 	 *
68e0b8e63eSJohn Marino 	 * If the user added a file but was unable to initialize it, there
69e0b8e63eSJohn Marino 	 * can be file list entries where the name field is NULL.  Discard
70e0b8e63eSJohn Marino 	 * them the next time we see them.
71e0b8e63eSJohn Marino 	 */
72e0b8e63eSJohn Marino 	gp = sp->gp;
73e0b8e63eSJohn Marino 	if (name != NULL)
74c5dc3490SJohn Marino 		TAILQ_FOREACH_MUTABLE(frp, gp->frefq, q, tfrp) {
75e0b8e63eSJohn Marino 			if (frp->name == NULL) {
76e0b8e63eSJohn Marino 				TAILQ_REMOVE(gp->frefq, frp, q);
77e0b8e63eSJohn Marino 				free(frp->name);
78e0b8e63eSJohn Marino 				free(frp);
79e0b8e63eSJohn Marino 				continue;
80e0b8e63eSJohn Marino 			}
81e0b8e63eSJohn Marino 			if (!strcmp(frp->name, name))
82e0b8e63eSJohn Marino 				return (frp);
83e0b8e63eSJohn Marino 		}
84e0b8e63eSJohn Marino 
85e0b8e63eSJohn Marino 	/* Allocate and initialize the FREF structure. */
86*b1ac2ebbSDaniel Fojt 	CALLOC(sp, frp, 1, sizeof(FREF));
87e0b8e63eSJohn Marino 	if (frp == NULL)
88e0b8e63eSJohn Marino 		return (NULL);
89e0b8e63eSJohn Marino 
90e0b8e63eSJohn Marino 	/*
91e0b8e63eSJohn Marino 	 * If no file name specified, or if the file name is a request
92e0b8e63eSJohn Marino 	 * for something temporary, file_init() will allocate the file
93e0b8e63eSJohn Marino 	 * name.  Temporary files are always ignored.
94e0b8e63eSJohn Marino 	 */
95e0b8e63eSJohn Marino 	if (name != NULL && strcmp(name, TEMPORARY_FILE_STRING) &&
96e0b8e63eSJohn Marino 	    (frp->name = strdup(name)) == NULL) {
97e0b8e63eSJohn Marino 		free(frp);
98e0b8e63eSJohn Marino 		msgq(sp, M_SYSERR, NULL);
99e0b8e63eSJohn Marino 		return (NULL);
100e0b8e63eSJohn Marino 	}
101e0b8e63eSJohn Marino 
102e0b8e63eSJohn Marino 	/* Append into the chain of file names. */
103e0b8e63eSJohn Marino 	TAILQ_INSERT_TAIL(gp->frefq, frp, q);
104e0b8e63eSJohn Marino 
105e0b8e63eSJohn Marino 	return (frp);
106e0b8e63eSJohn Marino }
107e0b8e63eSJohn Marino 
108e0b8e63eSJohn Marino /*
109e0b8e63eSJohn Marino  * file_init --
110e0b8e63eSJohn Marino  *	Start editing a file, based on the FREF structure.  If successsful,
111e0b8e63eSJohn Marino  *	let go of any previous file.  Don't release the previous file until
112e0b8e63eSJohn Marino  *	absolutely sure we have the new one.
113e0b8e63eSJohn Marino  *
114e0b8e63eSJohn Marino  * PUBLIC: int file_init(SCR *, FREF *, char *, int);
115e0b8e63eSJohn Marino  */
116e0b8e63eSJohn Marino int
file_init(SCR * sp,FREF * frp,char * rcv_name,int flags)117*b1ac2ebbSDaniel Fojt file_init(SCR *sp, FREF *frp, char *rcv_name, int flags)
118e0b8e63eSJohn Marino {
119e0b8e63eSJohn Marino 	EXF *ep;
120e0b8e63eSJohn Marino 	RECNOINFO oinfo = { 0 };
121e0b8e63eSJohn Marino 	struct stat sb;
122e0b8e63eSJohn Marino 	size_t psize;
123e0b8e63eSJohn Marino 	int fd, exists, open_err, readonly;
124e0b8e63eSJohn Marino 	char *oname, *tname;
125e0b8e63eSJohn Marino 
126e0b8e63eSJohn Marino 	open_err = readonly = 0;
127e0b8e63eSJohn Marino 
128e0b8e63eSJohn Marino 	/*
129e0b8e63eSJohn Marino 	 * If the file is a recovery file, let the recovery code handle it.
130e0b8e63eSJohn Marino 	 * Clear the FR_RECOVER flag first -- the recovery code does set up,
131e0b8e63eSJohn Marino 	 * and then calls us!  If the recovery call fails, it's probably
132e0b8e63eSJohn Marino 	 * because the named file doesn't exist.  So, move boldly forward,
133e0b8e63eSJohn Marino 	 * presuming that there's an error message the user will get to see.
134e0b8e63eSJohn Marino 	 */
135e0b8e63eSJohn Marino 	if (F_ISSET(frp, FR_RECOVER)) {
136e0b8e63eSJohn Marino 		F_CLR(frp, FR_RECOVER);
137e0b8e63eSJohn Marino 		return (rcv_read(sp, frp));
138e0b8e63eSJohn Marino 	}
139e0b8e63eSJohn Marino 
140e0b8e63eSJohn Marino 	/*
141e0b8e63eSJohn Marino 	 * Required FRP initialization; the only flag we keep is the
142e0b8e63eSJohn Marino 	 * cursor information.
143e0b8e63eSJohn Marino 	 */
144e0b8e63eSJohn Marino 	F_CLR(frp, ~FR_CURSORSET);
145e0b8e63eSJohn Marino 
146e0b8e63eSJohn Marino 	/*
147e0b8e63eSJohn Marino 	 * Required EXF initialization:
148e0b8e63eSJohn Marino 	 *	Flush the line caches.
149e0b8e63eSJohn Marino 	 *	Default recover mail file fd to -1.
150e0b8e63eSJohn Marino 	 *	Set initial EXF flag bits.
151e0b8e63eSJohn Marino 	 */
152*b1ac2ebbSDaniel Fojt 	CALLOC_RET(sp, ep, 1, sizeof(EXF));
153e0b8e63eSJohn Marino 	ep->c_lno = ep->c_nlines = OOBLNO;
154e0b8e63eSJohn Marino 	ep->rcv_fd = -1;
155e0b8e63eSJohn Marino 	F_SET(ep, F_FIRSTMODIFY);
156e0b8e63eSJohn Marino 
157e0b8e63eSJohn Marino 	/*
158e0b8e63eSJohn Marino 	 * Scan the user's path to find the file that we're going to
159e0b8e63eSJohn Marino 	 * try and open.
160e0b8e63eSJohn Marino 	 */
161e0b8e63eSJohn Marino 	if (file_spath(sp, frp, &sb, &exists))
162e0b8e63eSJohn Marino 		return (1);
163e0b8e63eSJohn Marino 
164e0b8e63eSJohn Marino 	/*
165e0b8e63eSJohn Marino 	 * If no name or backing file, for whatever reason, create a backing
166e0b8e63eSJohn Marino 	 * temporary file, saving the temp file name so we can later unlink
167e0b8e63eSJohn Marino 	 * it.  If the user never named this file, copy the temporary file name
168e0b8e63eSJohn Marino 	 * to the real name (we display that until the user renames it).
169e0b8e63eSJohn Marino 	 */
170e0b8e63eSJohn Marino 	oname = frp->name;
171e0b8e63eSJohn Marino 	if (LF_ISSET(FS_OPENERR) || oname == NULL || !exists) {
172e0b8e63eSJohn Marino 		struct stat sb;
173e0b8e63eSJohn Marino 
174e0b8e63eSJohn Marino 		if (opts_empty(sp, O_TMPDIR, 0))
175e0b8e63eSJohn Marino 			goto err;
176e0b8e63eSJohn Marino 		if ((tname =
177e0b8e63eSJohn Marino 		    join(O_STR(sp, O_TMPDIR), "vi.XXXXXXXXXX")) == NULL) {
178e0b8e63eSJohn Marino 			msgq(sp, M_SYSERR, NULL);
179e0b8e63eSJohn Marino 			goto err;
180e0b8e63eSJohn Marino 		}
181e0b8e63eSJohn Marino 		if ((fd = mkstemp(tname)) == -1 || fstat(fd, &sb)) {
182e0b8e63eSJohn Marino 			free(tname);
183e0b8e63eSJohn Marino 			msgq(sp, M_SYSERR,
184e0b8e63eSJohn Marino 			    "237|Unable to create temporary file");
185e0b8e63eSJohn Marino 			goto err;
186e0b8e63eSJohn Marino 		}
187e0b8e63eSJohn Marino 		(void)close(fd);
188e0b8e63eSJohn Marino 
189e0b8e63eSJohn Marino 		frp->tname = tname;
190e0b8e63eSJohn Marino 		if (frp->name == NULL) {
191e0b8e63eSJohn Marino 			F_SET(frp, FR_TMPFILE);
192e0b8e63eSJohn Marino 			if ((frp->name = strdup(tname)) == NULL) {
193e0b8e63eSJohn Marino 				msgq(sp, M_SYSERR, NULL);
194e0b8e63eSJohn Marino 				goto err;
195e0b8e63eSJohn Marino 			}
196e0b8e63eSJohn Marino 		}
197e0b8e63eSJohn Marino 		oname = frp->tname;
198e0b8e63eSJohn Marino 		psize = 1024;
199e0b8e63eSJohn Marino 		if (!LF_ISSET(FS_OPENERR))
200e0b8e63eSJohn Marino 			F_SET(frp, FR_NEWFILE);
201e0b8e63eSJohn Marino 
202*b1ac2ebbSDaniel Fojt 		ep->mtim = sb.st_mtim;
203e0b8e63eSJohn Marino 	} else {
204e0b8e63eSJohn Marino 		/*
205e0b8e63eSJohn Marino 		 * XXX
206e0b8e63eSJohn Marino 		 * A seat of the pants calculation: try to keep the file in
207e0b8e63eSJohn Marino 		 * 15 pages or less.  Don't use a page size larger than 16K
208e0b8e63eSJohn Marino 		 * (vi should have good locality) or smaller than 1K.
209e0b8e63eSJohn Marino 		 */
210e0b8e63eSJohn Marino 		psize = ((sb.st_size / 15) + 1023) / 1024;
211e0b8e63eSJohn Marino 		if (psize > 16)
212e0b8e63eSJohn Marino 			psize = 16;
213e0b8e63eSJohn Marino 		if (psize == 0)
214e0b8e63eSJohn Marino 			psize = 1;
215e0b8e63eSJohn Marino 		psize = p2roundup(psize) << 10;
216e0b8e63eSJohn Marino 
217e0b8e63eSJohn Marino 		F_SET(ep, F_DEVSET);
218e0b8e63eSJohn Marino 		ep->mdev = sb.st_dev;
219e0b8e63eSJohn Marino 		ep->minode = sb.st_ino;
220e0b8e63eSJohn Marino 
221*b1ac2ebbSDaniel Fojt 		ep->mtim = sb.st_mtim;
222e0b8e63eSJohn Marino 
223e0b8e63eSJohn Marino 		if (!S_ISREG(sb.st_mode))
224e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, oname,
225e0b8e63eSJohn Marino 			    "238|Warning: %s is not a regular file");
226e0b8e63eSJohn Marino 	}
227e0b8e63eSJohn Marino 
228e0b8e63eSJohn Marino 	/* Set up recovery. */
229e0b8e63eSJohn Marino 	oinfo.bval = '\n';			/* Always set. */
230e0b8e63eSJohn Marino 	oinfo.psize = psize;
231e0b8e63eSJohn Marino 	oinfo.flags = F_ISSET(sp->gp, G_SNAPSHOT) ? R_SNAPSHOT : 0;
232e0b8e63eSJohn Marino 	if (rcv_name == NULL) {
233e0b8e63eSJohn Marino 		if (!rcv_tmp(sp, ep, frp->name))
234e0b8e63eSJohn Marino 			oinfo.bfname = ep->rcv_path;
235e0b8e63eSJohn Marino 	} else {
236e0b8e63eSJohn Marino 		if ((ep->rcv_path = strdup(rcv_name)) == NULL) {
237e0b8e63eSJohn Marino 			msgq(sp, M_SYSERR, NULL);
238e0b8e63eSJohn Marino 			goto err;
239e0b8e63eSJohn Marino 		}
240e0b8e63eSJohn Marino 		oinfo.bfname = ep->rcv_path;
241e0b8e63eSJohn Marino 		F_SET(ep, F_MODIFIED);
242e0b8e63eSJohn Marino 	}
243e0b8e63eSJohn Marino 
244e0b8e63eSJohn Marino 	/* Open a db structure. */
245e0b8e63eSJohn Marino 	if ((ep->db = dbopen(rcv_name == NULL ? oname : NULL,
246e0b8e63eSJohn Marino 	    O_NONBLOCK | O_RDONLY,
247e0b8e63eSJohn Marino 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
248e0b8e63eSJohn Marino 	    DB_RECNO, &oinfo)) == NULL) {
249e0b8e63eSJohn Marino 		msgq_str(sp,
250e0b8e63eSJohn Marino 		    M_SYSERR, rcv_name == NULL ? oname : rcv_name, "%s");
251e0b8e63eSJohn Marino 		if (F_ISSET(frp, FR_NEWFILE))
252e0b8e63eSJohn Marino 			goto err;
253e0b8e63eSJohn Marino 		/*
254e0b8e63eSJohn Marino 		 * !!!
255e0b8e63eSJohn Marino 		 * Historically, vi permitted users to edit files that couldn't
256e0b8e63eSJohn Marino 		 * be read.  This isn't useful for single files from a command
257e0b8e63eSJohn Marino 		 * line, but it's quite useful for "vi *.c", since you can skip
258e0b8e63eSJohn Marino 		 * past files that you can't read.
259e0b8e63eSJohn Marino 		 */
260e0b8e63eSJohn Marino 		open_err = 1;
261e0b8e63eSJohn Marino 		goto oerr;
262e0b8e63eSJohn Marino 	}
263e0b8e63eSJohn Marino 
264e0b8e63eSJohn Marino 	/*
265e0b8e63eSJohn Marino 	 * Do the remaining things that can cause failure of the new file,
266e0b8e63eSJohn Marino 	 * mark and logging initialization.
267e0b8e63eSJohn Marino 	 */
268e0b8e63eSJohn Marino 	if (mark_init(sp, ep) || log_init(sp, ep))
269e0b8e63eSJohn Marino 		goto err;
270e0b8e63eSJohn Marino 
271e0b8e63eSJohn Marino 	/*
272e0b8e63eSJohn Marino 	 * Set the alternate file name to be the file we're discarding.
273e0b8e63eSJohn Marino 	 *
274e0b8e63eSJohn Marino 	 * !!!
275e0b8e63eSJohn Marino 	 * Temporary files can't become alternate files, so there's no file
276e0b8e63eSJohn Marino 	 * name.  This matches historical practice, although it could only
277e0b8e63eSJohn Marino 	 * happen in historical vi as the result of the initial command, i.e.
278e0b8e63eSJohn Marino 	 * if vi was executed without a file name.
279e0b8e63eSJohn Marino 	 */
280e0b8e63eSJohn Marino 	if (LF_ISSET(FS_SETALT))
281e0b8e63eSJohn Marino 		set_alt_name(sp, sp->frp == NULL ||
282e0b8e63eSJohn Marino 		    F_ISSET(sp->frp, FR_TMPFILE) ? NULL : sp->frp->name);
283e0b8e63eSJohn Marino 
284e0b8e63eSJohn Marino 	/*
285e0b8e63eSJohn Marino 	 * Close the previous file; if that fails, close the new one and run
286e0b8e63eSJohn Marino 	 * for the border.
287e0b8e63eSJohn Marino 	 *
288e0b8e63eSJohn Marino 	 * !!!
289e0b8e63eSJohn Marino 	 * There's a nasty special case.  If the user edits a temporary file,
290e0b8e63eSJohn Marino 	 * and then does an ":e! %", we need to re-initialize the backing
291e0b8e63eSJohn Marino 	 * file, but we can't change the name.  (It's worse -- we're dealing
292e0b8e63eSJohn Marino 	 * with *names* here, we can't even detect that it happened.)  Set a
293e0b8e63eSJohn Marino 	 * flag so that the file_end routine ignores the backing information
294e0b8e63eSJohn Marino 	 * of the old file if it happens to be the same as the new one.
295e0b8e63eSJohn Marino 	 *
296e0b8e63eSJohn Marino 	 * !!!
297e0b8e63eSJohn Marino 	 * Side-effect: after the call to file_end(), sp->frp may be NULL.
298e0b8e63eSJohn Marino 	 */
299e0b8e63eSJohn Marino 	if (sp->ep != NULL) {
300e0b8e63eSJohn Marino 		F_SET(frp, FR_DONTDELETE);
301e0b8e63eSJohn Marino 		if (file_end(sp, NULL, LF_ISSET(FS_FORCE))) {
302e0b8e63eSJohn Marino 			(void)file_end(sp, ep, 1);
303e0b8e63eSJohn Marino 			goto err;
304e0b8e63eSJohn Marino 		}
305e0b8e63eSJohn Marino 		F_CLR(frp, FR_DONTDELETE);
306e0b8e63eSJohn Marino 	}
307e0b8e63eSJohn Marino 
308e0b8e63eSJohn Marino 	/*
309e0b8e63eSJohn Marino 	 * Lock the file; if it's a recovery file, it should already be
310e0b8e63eSJohn Marino 	 * locked.  Note, we acquire the lock after the previous file
311e0b8e63eSJohn Marino 	 * has been ended, so that we don't get an "already locked" error
312e0b8e63eSJohn Marino 	 * for ":edit!".
313e0b8e63eSJohn Marino 	 *
314e0b8e63eSJohn Marino 	 * XXX
315e0b8e63eSJohn Marino 	 * While the user can't interrupt us between the open and here,
316e0b8e63eSJohn Marino 	 * there's a race between the dbopen() and the lock.  Not much
317e0b8e63eSJohn Marino 	 * we can do about it.
318e0b8e63eSJohn Marino 	 *
319e0b8e63eSJohn Marino 	 * XXX
320e0b8e63eSJohn Marino 	 * We don't make a big deal of not being able to lock the file.  As
321e0b8e63eSJohn Marino 	 * locking rarely works over NFS, and often fails if the file was
322e0b8e63eSJohn Marino 	 * mmap(2)'d, it's far too common to do anything like print an error
323e0b8e63eSJohn Marino 	 * message, let alone make the file readonly.  At some future time,
324e0b8e63eSJohn Marino 	 * when locking is a little more reliable, this should change to be
325e0b8e63eSJohn Marino 	 * an error.
326e0b8e63eSJohn Marino 	 */
327e0b8e63eSJohn Marino 	if (rcv_name == NULL)
328e0b8e63eSJohn Marino 		switch (file_lock(sp, oname, ep->db->fd(ep->db), 0)) {
329e0b8e63eSJohn Marino 		case LOCK_FAILED:
330e0b8e63eSJohn Marino 			F_SET(frp, FR_UNLOCKED);
331e0b8e63eSJohn Marino 			break;
332e0b8e63eSJohn Marino 		case LOCK_UNAVAIL:
333e0b8e63eSJohn Marino 			readonly = 1;
334e0b8e63eSJohn Marino 			if (F_ISSET(sp, SC_READONLY))
335e0b8e63eSJohn Marino 				break;
336e0b8e63eSJohn Marino 			msgq_str(sp, M_INFO, oname,
337e0b8e63eSJohn Marino 			    "239|%s already locked, session is read-only");
338e0b8e63eSJohn Marino 			break;
339e0b8e63eSJohn Marino 		case LOCK_SUCCESS:
340e0b8e63eSJohn Marino 			break;
341e0b8e63eSJohn Marino 		}
342e0b8e63eSJohn Marino 
343e0b8e63eSJohn Marino 	/*
344e0b8e63eSJohn Marino          * Historically, the readonly edit option was set per edit buffer in
345e0b8e63eSJohn Marino          * vi, unless the -R command-line option was specified or the program
346e0b8e63eSJohn Marino          * was executed as "view".  (Well, to be truthful, if the letter 'w'
347e0b8e63eSJohn Marino          * occurred anywhere in the program name, but let's not get into that.)
348*b1ac2ebbSDaniel Fojt 	 * So, the persistent readonly state has to be stored in the screen
349e0b8e63eSJohn Marino 	 * structure, and the edit option value toggles with the contents of
350*b1ac2ebbSDaniel Fojt 	 * the edit buffer.  If the persistent readonly flag is set, set the
351e0b8e63eSJohn Marino 	 * readonly edit option.
352e0b8e63eSJohn Marino 	 *
353e0b8e63eSJohn Marino 	 * Otherwise, try and figure out if a file is readonly.  This is a
354e0b8e63eSJohn Marino 	 * dangerous thing to do.  The kernel is the only arbiter of whether
355e0b8e63eSJohn Marino 	 * or not a file is writeable, and the best that a user program can
356e0b8e63eSJohn Marino 	 * do is guess.  Obvious loopholes are files that are on a file system
357e0b8e63eSJohn Marino 	 * mounted readonly (access catches this one on a few systems), or
358e0b8e63eSJohn Marino 	 * alternate protection mechanisms, ACL's for example, that we can't
359e0b8e63eSJohn Marino 	 * portably check.  Lots of fun, and only here because users whined.
360e0b8e63eSJohn Marino 	 *
361e0b8e63eSJohn Marino 	 * !!!
362e0b8e63eSJohn Marino 	 * Historic vi displayed the readonly message if none of the file
363e0b8e63eSJohn Marino 	 * write bits were set, or if an an access(2) call on the path
364e0b8e63eSJohn Marino 	 * failed.  This seems reasonable.  If the file is mode 444, root
365e0b8e63eSJohn Marino 	 * users may want to know that the owner of the file did not expect
366e0b8e63eSJohn Marino 	 * it to be written.
367e0b8e63eSJohn Marino 	 *
368e0b8e63eSJohn Marino 	 * Historic vi set the readonly bit if no write bits were set for
369e0b8e63eSJohn Marino 	 * a file, even if the access call would have succeeded.  This makes
370e0b8e63eSJohn Marino 	 * the superuser force the write even when vi expects that it will
371e0b8e63eSJohn Marino 	 * succeed.  I'm less supportive of this semantic, but it's historic
372e0b8e63eSJohn Marino 	 * practice and the conservative approach to vi'ing files as root.
373e0b8e63eSJohn Marino 	 *
374e0b8e63eSJohn Marino 	 * It would be nice if there was some way to update this when the user
375e0b8e63eSJohn Marino 	 * does a "^Z; chmod ...".  The problem is that we'd first have to
376e0b8e63eSJohn Marino 	 * distinguish between readonly bits set because of file permissions
377e0b8e63eSJohn Marino 	 * and those set for other reasons.  That's not too hard, but deciding
378e0b8e63eSJohn Marino 	 * when to reevaluate the permissions is trickier.  An alternative
379e0b8e63eSJohn Marino 	 * might be to turn off the readonly bit if the user forces a write
380e0b8e63eSJohn Marino 	 * and it succeeds.
381e0b8e63eSJohn Marino 	 *
382e0b8e63eSJohn Marino 	 * XXX
383e0b8e63eSJohn Marino 	 * Access(2) doesn't consider the effective uid/gid values.  This
384e0b8e63eSJohn Marino 	 * probably isn't a problem for vi when it's running standalone.
385e0b8e63eSJohn Marino 	 */
386e0b8e63eSJohn Marino 	if (readonly || F_ISSET(sp, SC_READONLY) ||
387e0b8e63eSJohn Marino 	    (!F_ISSET(frp, FR_NEWFILE) &&
388e0b8e63eSJohn Marino 	    (!(sb.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) ||
389e0b8e63eSJohn Marino 	    access(frp->name, W_OK))))
390e0b8e63eSJohn Marino 		O_SET(sp, O_READONLY);
391e0b8e63eSJohn Marino 	else
392e0b8e63eSJohn Marino 		O_CLR(sp, O_READONLY);
393e0b8e63eSJohn Marino 
394e0b8e63eSJohn Marino 	/* Switch... */
395e0b8e63eSJohn Marino 	++ep->refcnt;
396e0b8e63eSJohn Marino 	sp->ep = ep;
397e0b8e63eSJohn Marino 	sp->frp = frp;
398e0b8e63eSJohn Marino 
399e0b8e63eSJohn Marino 	/* Detect and set the file encoding */
400e0b8e63eSJohn Marino 	file_encinit(sp);
401e0b8e63eSJohn Marino 
402e0b8e63eSJohn Marino 	/* Set the initial cursor position, queue initial command. */
403e0b8e63eSJohn Marino 	file_cinit(sp);
404e0b8e63eSJohn Marino 
405e0b8e63eSJohn Marino 	/* Redraw the screen from scratch, schedule a welcome message. */
406e0b8e63eSJohn Marino 	F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);
407e0b8e63eSJohn Marino 
408e0b8e63eSJohn Marino 	return (0);
409e0b8e63eSJohn Marino 
410*b1ac2ebbSDaniel Fojt err:	free(frp->name);
411e0b8e63eSJohn Marino 	frp->name = NULL;
412e0b8e63eSJohn Marino 	if (frp->tname != NULL) {
413e0b8e63eSJohn Marino 		(void)unlink(frp->tname);
414e0b8e63eSJohn Marino 		free(frp->tname);
415e0b8e63eSJohn Marino 		frp->tname = NULL;
416e0b8e63eSJohn Marino 	}
417e0b8e63eSJohn Marino 
418e0b8e63eSJohn Marino oerr:	if (F_ISSET(ep, F_RCV_ON))
419e0b8e63eSJohn Marino 		(void)unlink(ep->rcv_path);
420e0b8e63eSJohn Marino 	free(ep->rcv_path);
421e0b8e63eSJohn Marino 	ep->rcv_path = NULL;
422*b1ac2ebbSDaniel Fojt 
423e0b8e63eSJohn Marino 	if (ep->db != NULL)
424e0b8e63eSJohn Marino 		(void)ep->db->close(ep->db);
425e0b8e63eSJohn Marino 	free(ep);
426e0b8e63eSJohn Marino 
427e0b8e63eSJohn Marino 	return (open_err ?
428e0b8e63eSJohn Marino 	    file_init(sp, frp, rcv_name, flags | FS_OPENERR) : 1);
429e0b8e63eSJohn Marino }
430e0b8e63eSJohn Marino 
431e0b8e63eSJohn Marino /*
432e0b8e63eSJohn Marino  * file_spath --
433e0b8e63eSJohn Marino  *	Scan the user's path to find the file that we're going to
434e0b8e63eSJohn Marino  *	try and open.
435e0b8e63eSJohn Marino  */
436e0b8e63eSJohn Marino static int
file_spath(SCR * sp,FREF * frp,struct stat * sbp,int * existsp)437*b1ac2ebbSDaniel Fojt file_spath(SCR *sp, FREF *frp, struct stat *sbp, int *existsp)
438e0b8e63eSJohn Marino {
439e0b8e63eSJohn Marino 	int savech;
440e0b8e63eSJohn Marino 	size_t len;
441e0b8e63eSJohn Marino 	int found;
442e0b8e63eSJohn Marino 	char *name, *p, *t, *path;
443e0b8e63eSJohn Marino 
444e0b8e63eSJohn Marino 	/*
445e0b8e63eSJohn Marino 	 * If the name is NULL or an explicit reference (i.e., the first
446e0b8e63eSJohn Marino 	 * component is . or ..) ignore the O_PATH option.
447e0b8e63eSJohn Marino 	 */
448e0b8e63eSJohn Marino 	name = frp->name;
449e0b8e63eSJohn Marino 	if (name == NULL) {
450e0b8e63eSJohn Marino 		*existsp = 0;
451e0b8e63eSJohn Marino 		return (0);
452e0b8e63eSJohn Marino 	}
453e0b8e63eSJohn Marino 	if (name[0] == '/' || (name[0] == '.' &&
454e0b8e63eSJohn Marino 	    (name[1] == '/' || (name[1] == '.' && name[2] == '/')))) {
455e0b8e63eSJohn Marino 		*existsp = !stat(name, sbp);
456e0b8e63eSJohn Marino 		return (0);
457e0b8e63eSJohn Marino 	}
458e0b8e63eSJohn Marino 
459e0b8e63eSJohn Marino 	/* Try . */
460e0b8e63eSJohn Marino 	if (!stat(name, sbp)) {
461e0b8e63eSJohn Marino 		*existsp = 1;
462e0b8e63eSJohn Marino 		return (0);
463e0b8e63eSJohn Marino 	}
464e0b8e63eSJohn Marino 
465e0b8e63eSJohn Marino 	/* Try the O_PATH option values. */
466e0b8e63eSJohn Marino 	for (found = 0, p = t = O_STR(sp, O_PATH);; ++p)
467e0b8e63eSJohn Marino 		if (*p == ':' || *p == '\0') {
468e0b8e63eSJohn Marino 			/*
469e0b8e63eSJohn Marino 			 * Ignore the empty strings and ".", since we've already
470e0b8e63eSJohn Marino 			 * tried the current directory.
471e0b8e63eSJohn Marino 			 */
472e0b8e63eSJohn Marino 			if (t < p && (p - t != 1 || *t != '.')) {
473e0b8e63eSJohn Marino 				savech = *p;
474e0b8e63eSJohn Marino 				*p = '\0';
475e0b8e63eSJohn Marino 				if ((path = join(t, name)) == NULL) {
476e0b8e63eSJohn Marino 					msgq(sp, M_SYSERR, NULL);
477e0b8e63eSJohn Marino 					break;
478e0b8e63eSJohn Marino 				}
479e0b8e63eSJohn Marino 				len = strlen(path);
480e0b8e63eSJohn Marino 				*p = savech;
481e0b8e63eSJohn Marino 				if (!stat(path, sbp)) {
482e0b8e63eSJohn Marino 					found = 1;
483e0b8e63eSJohn Marino 					break;
484e0b8e63eSJohn Marino 				}
485e0b8e63eSJohn Marino 				free(path);
486e0b8e63eSJohn Marino 			}
487e0b8e63eSJohn Marino 			t = p + 1;
488e0b8e63eSJohn Marino 			if (*p == '\0')
489e0b8e63eSJohn Marino 				break;
490e0b8e63eSJohn Marino 		}
491e0b8e63eSJohn Marino 
492e0b8e63eSJohn Marino 	/* If we found it, build a new pathname and discard the old one. */
493e0b8e63eSJohn Marino 	if (found) {
494e0b8e63eSJohn Marino 		free(frp->name);
495e0b8e63eSJohn Marino 		frp->name = path;
496e0b8e63eSJohn Marino 	}
497e0b8e63eSJohn Marino 	*existsp = found;
498e0b8e63eSJohn Marino 	return (0);
499e0b8e63eSJohn Marino }
500e0b8e63eSJohn Marino 
501e0b8e63eSJohn Marino /*
502e0b8e63eSJohn Marino  * file_cinit --
503e0b8e63eSJohn Marino  *	Set up the initial cursor position.
504e0b8e63eSJohn Marino  */
505e0b8e63eSJohn Marino static void
file_cinit(SCR * sp)506e0b8e63eSJohn Marino file_cinit(SCR *sp)
507e0b8e63eSJohn Marino {
508e0b8e63eSJohn Marino 	GS *gp;
509e0b8e63eSJohn Marino 	MARK m;
510e0b8e63eSJohn Marino 	size_t len;
511e0b8e63eSJohn Marino 	int nb;
512e0b8e63eSJohn Marino 	CHAR_T *wp;
513e0b8e63eSJohn Marino 	size_t wlen;
514e0b8e63eSJohn Marino 
515e0b8e63eSJohn Marino 	/* Set some basic defaults. */
516e0b8e63eSJohn Marino 	sp->lno = 1;
517e0b8e63eSJohn Marino 	sp->cno = 0;
518e0b8e63eSJohn Marino 
519e0b8e63eSJohn Marino 	/*
520e0b8e63eSJohn Marino 	 * Historically, initial commands (the -c option) weren't executed
521e0b8e63eSJohn Marino 	 * until a file was loaded, e.g. "vi +10 nofile", followed by an
522e0b8e63eSJohn Marino 	 * :edit or :tag command, would execute the +10 on the file loaded
523e0b8e63eSJohn Marino 	 * by the subsequent command, (assuming that it existed).  This
524e0b8e63eSJohn Marino 	 * applied as well to files loaded using the tag commands, and we
525e0b8e63eSJohn Marino 	 * follow that historic practice.  Also, all initial commands were
526e0b8e63eSJohn Marino 	 * ex commands and were always executed on the last line of the file.
527e0b8e63eSJohn Marino 	 *
528e0b8e63eSJohn Marino 	 * Otherwise, if no initial command for this file:
529e0b8e63eSJohn Marino 	 *    If in ex mode, move to the last line, first nonblank character.
530e0b8e63eSJohn Marino 	 *    If the file has previously been edited, move to the last known
531e0b8e63eSJohn Marino 	 *	  position, and check it for validity.
532e0b8e63eSJohn Marino 	 *    Otherwise, move to the first line, first nonblank.
533e0b8e63eSJohn Marino 	 *
534e0b8e63eSJohn Marino 	 * This gets called by the file init code, because we may be in a
535e0b8e63eSJohn Marino 	 * file of ex commands and we want to execute them from the right
536e0b8e63eSJohn Marino 	 * location in the file.
537e0b8e63eSJohn Marino 	 */
538e0b8e63eSJohn Marino 	nb = 0;
539e0b8e63eSJohn Marino 	gp = sp->gp;
540e0b8e63eSJohn Marino 	if (gp->c_option != NULL && !F_ISSET(sp->frp, FR_NEWFILE)) {
541e0b8e63eSJohn Marino 		if (db_last(sp, &sp->lno))
542e0b8e63eSJohn Marino 			return;
543e0b8e63eSJohn Marino 		if (sp->lno == 0) {
544e0b8e63eSJohn Marino 			sp->lno = 1;
545e0b8e63eSJohn Marino 			sp->cno = 0;
546e0b8e63eSJohn Marino 		}
547e0b8e63eSJohn Marino 		CHAR2INT(sp, gp->c_option, strlen(gp->c_option) + 1,
548e0b8e63eSJohn Marino 			 wp, wlen);
549*b1ac2ebbSDaniel Fojt 		if (ex_run_str(sp, "-c option", wp, wlen - 1, 1, 0))
550e0b8e63eSJohn Marino 			return;
551e0b8e63eSJohn Marino 		gp->c_option = NULL;
552e0b8e63eSJohn Marino 	} else if (F_ISSET(sp, SC_EX)) {
553e0b8e63eSJohn Marino 		if (db_last(sp, &sp->lno))
554e0b8e63eSJohn Marino 			return;
555e0b8e63eSJohn Marino 		if (sp->lno == 0) {
556e0b8e63eSJohn Marino 			sp->lno = 1;
557e0b8e63eSJohn Marino 			sp->cno = 0;
558e0b8e63eSJohn Marino 			return;
559e0b8e63eSJohn Marino 		}
560e0b8e63eSJohn Marino 		nb = 1;
561e0b8e63eSJohn Marino 	} else {
562e0b8e63eSJohn Marino 		if (F_ISSET(sp->frp, FR_CURSORSET)) {
563e0b8e63eSJohn Marino 			sp->lno = sp->frp->lno;
564e0b8e63eSJohn Marino 			sp->cno = sp->frp->cno;
565e0b8e63eSJohn Marino 
566e0b8e63eSJohn Marino 			/* If returning to a file in vi, center the line. */
567e0b8e63eSJohn Marino 			 F_SET(sp, SC_SCR_CENTER);
568e0b8e63eSJohn Marino 		} else {
569e0b8e63eSJohn Marino 			if (O_ISSET(sp, O_COMMENT))
570e0b8e63eSJohn Marino 				file_comment(sp);
571e0b8e63eSJohn Marino 			else
572e0b8e63eSJohn Marino 				sp->lno = 1;
573e0b8e63eSJohn Marino 			nb = 1;
574e0b8e63eSJohn Marino 		}
575e0b8e63eSJohn Marino 		if (db_get(sp, sp->lno, 0, NULL, &len)) {
576e0b8e63eSJohn Marino 			sp->lno = 1;
577e0b8e63eSJohn Marino 			sp->cno = 0;
578e0b8e63eSJohn Marino 			return;
579e0b8e63eSJohn Marino 		}
580e0b8e63eSJohn Marino 		if (!nb && sp->cno > len)
581e0b8e63eSJohn Marino 			nb = 1;
582e0b8e63eSJohn Marino 	}
583e0b8e63eSJohn Marino 	if (nb) {
584e0b8e63eSJohn Marino 		sp->cno = 0;
585e0b8e63eSJohn Marino 		(void)nonblank(sp, sp->lno, &sp->cno);
586e0b8e63eSJohn Marino 	}
587e0b8e63eSJohn Marino 
588e0b8e63eSJohn Marino 	/*
589e0b8e63eSJohn Marino 	 * !!!
590e0b8e63eSJohn Marino 	 * The initial column is also the most attractive column.
591e0b8e63eSJohn Marino 	 */
592e0b8e63eSJohn Marino 	sp->rcm = sp->cno;
593e0b8e63eSJohn Marino 
594e0b8e63eSJohn Marino 	/*
595e0b8e63eSJohn Marino 	 * !!!
596e0b8e63eSJohn Marino 	 * Historically, vi initialized the absolute mark, but ex did not.
597e0b8e63eSJohn Marino 	 * Which meant, that if the first command in ex mode was "visual",
598e0b8e63eSJohn Marino 	 * or if an ex command was executed first (e.g. vi +10 file) vi was
599e0b8e63eSJohn Marino 	 * entered without the mark being initialized.  For consistency, if
600e0b8e63eSJohn Marino 	 * the file isn't empty, we initialize it for everyone, believing
601e0b8e63eSJohn Marino 	 * that it can't hurt, and is generally useful.  Not initializing it
602e0b8e63eSJohn Marino 	 * if the file is empty is historic practice, although it has always
603e0b8e63eSJohn Marino 	 * been possible to set (and use) marks in empty vi files.
604e0b8e63eSJohn Marino 	 */
605e0b8e63eSJohn Marino 	m.lno = sp->lno;
606e0b8e63eSJohn Marino 	m.cno = sp->cno;
607e0b8e63eSJohn Marino 	(void)mark_set(sp, ABSMARK1, &m, 0);
608e0b8e63eSJohn Marino }
609e0b8e63eSJohn Marino 
610e0b8e63eSJohn Marino /*
611e0b8e63eSJohn Marino  * file_end --
612e0b8e63eSJohn Marino  *	Stop editing a file.
613e0b8e63eSJohn Marino  *
614e0b8e63eSJohn Marino  * PUBLIC: int file_end(SCR *, EXF *, int);
615e0b8e63eSJohn Marino  */
616e0b8e63eSJohn Marino int
file_end(SCR * sp,EXF * ep,int force)617*b1ac2ebbSDaniel Fojt file_end(SCR *sp, EXF *ep, int force)
618e0b8e63eSJohn Marino {
619e0b8e63eSJohn Marino 	FREF *frp;
620e0b8e63eSJohn Marino 
621e0b8e63eSJohn Marino 	/*
622e0b8e63eSJohn Marino 	 * !!!
623e0b8e63eSJohn Marino 	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
624e0b8e63eSJohn Marino 	 * (If argument ep is NULL, use sp->ep.)
625e0b8e63eSJohn Marino 	 *
626e0b8e63eSJohn Marino 	 * If multiply referenced, just decrement the count and return.
627e0b8e63eSJohn Marino 	 */
628e0b8e63eSJohn Marino 	if (ep == NULL)
629e0b8e63eSJohn Marino 		ep = sp->ep;
630e0b8e63eSJohn Marino 	if (--ep->refcnt != 0)
631e0b8e63eSJohn Marino 		return (0);
632e0b8e63eSJohn Marino 
633e0b8e63eSJohn Marino 	/*
634e0b8e63eSJohn Marino 	 *
635e0b8e63eSJohn Marino 	 * Clean up the FREF structure.
636e0b8e63eSJohn Marino 	 *
637e0b8e63eSJohn Marino 	 * Save the cursor location.
638e0b8e63eSJohn Marino 	 *
639e0b8e63eSJohn Marino 	 * XXX
640e0b8e63eSJohn Marino 	 * It would be cleaner to do this somewhere else, but by the time
641e0b8e63eSJohn Marino 	 * ex or vi knows that we're changing files it's already happened.
642e0b8e63eSJohn Marino 	 */
643e0b8e63eSJohn Marino 	frp = sp->frp;
644e0b8e63eSJohn Marino 	frp->lno = sp->lno;
645e0b8e63eSJohn Marino 	frp->cno = sp->cno;
646e0b8e63eSJohn Marino 	F_SET(frp, FR_CURSORSET);
647e0b8e63eSJohn Marino 
648e0b8e63eSJohn Marino 	/*
649e0b8e63eSJohn Marino 	 * We may no longer need the temporary backing file, so clean it
650e0b8e63eSJohn Marino 	 * up.  We don't need the FREF structure either, if the file was
651e0b8e63eSJohn Marino 	 * never named, so lose it.
652e0b8e63eSJohn Marino 	 *
653e0b8e63eSJohn Marino 	 * !!!
654e0b8e63eSJohn Marino 	 * Re: FR_DONTDELETE, see the comment above in file_init().
655e0b8e63eSJohn Marino 	 */
656e0b8e63eSJohn Marino 	if (!F_ISSET(frp, FR_DONTDELETE) && frp->tname != NULL) {
657e0b8e63eSJohn Marino 		if (unlink(frp->tname))
658e0b8e63eSJohn Marino 			msgq_str(sp, M_SYSERR, frp->tname, "240|%s: remove");
659e0b8e63eSJohn Marino 		free(frp->tname);
660e0b8e63eSJohn Marino 		frp->tname = NULL;
661e0b8e63eSJohn Marino 		if (F_ISSET(frp, FR_TMPFILE)) {
662e0b8e63eSJohn Marino 			TAILQ_REMOVE(sp->gp->frefq, frp, q);
663e0b8e63eSJohn Marino 			free(frp->name);
664e0b8e63eSJohn Marino 			free(frp);
665e0b8e63eSJohn Marino 		}
666e0b8e63eSJohn Marino 		sp->frp = NULL;
667e0b8e63eSJohn Marino 	}
668e0b8e63eSJohn Marino 
669e0b8e63eSJohn Marino 	/*
670e0b8e63eSJohn Marino 	 * Clean up the EXF structure.
671e0b8e63eSJohn Marino 	 *
672e0b8e63eSJohn Marino 	 * Close the db structure.
673e0b8e63eSJohn Marino 	 */
674e0b8e63eSJohn Marino 	if (ep->db->close != NULL && ep->db->close(ep->db) && !force) {
675e0b8e63eSJohn Marino 		msgq_str(sp, M_SYSERR, frp->name, "241|%s: close");
676e0b8e63eSJohn Marino 		++ep->refcnt;
677e0b8e63eSJohn Marino 		return (1);
678e0b8e63eSJohn Marino 	}
679e0b8e63eSJohn Marino 
680e0b8e63eSJohn Marino 	/* COMMITTED TO THE CLOSE.  THERE'S NO GOING BACK... */
681e0b8e63eSJohn Marino 
682e0b8e63eSJohn Marino 	/* Stop logging. */
683e0b8e63eSJohn Marino 	(void)log_end(sp, ep);
684e0b8e63eSJohn Marino 
685e0b8e63eSJohn Marino 	/* Free up any marks. */
686e0b8e63eSJohn Marino 	(void)mark_end(sp, ep);
687e0b8e63eSJohn Marino 
688e0b8e63eSJohn Marino 	/*
689e0b8e63eSJohn Marino 	 * Delete recovery files, close the open descriptor, free recovery
690e0b8e63eSJohn Marino 	 * memory.  See recover.c for a description of the protocol.
691e0b8e63eSJohn Marino 	 *
692e0b8e63eSJohn Marino 	 * XXX
693e0b8e63eSJohn Marino 	 * Unlink backup file first, we can detect that the recovery file
694e0b8e63eSJohn Marino 	 * doesn't reference anything when the user tries to recover it.
695e0b8e63eSJohn Marino 	 * There's a race, here, obviously, but it's fairly small.
696e0b8e63eSJohn Marino 	 */
697e0b8e63eSJohn Marino 	if (!F_ISSET(ep, F_RCV_NORM)) {
698e0b8e63eSJohn Marino 		if (ep->rcv_path != NULL && unlink(ep->rcv_path))
699e0b8e63eSJohn Marino 			msgq_str(sp, M_SYSERR, ep->rcv_path, "242|%s: remove");
700e0b8e63eSJohn Marino 		if (ep->rcv_mpath != NULL && unlink(ep->rcv_mpath))
701e0b8e63eSJohn Marino 			msgq_str(sp, M_SYSERR, ep->rcv_mpath, "243|%s: remove");
702e0b8e63eSJohn Marino 	}
703e0b8e63eSJohn Marino 	if (ep->rcv_fd != -1)
704e0b8e63eSJohn Marino 		(void)close(ep->rcv_fd);
705e0b8e63eSJohn Marino 	free(ep->rcv_path);
706e0b8e63eSJohn Marino 	free(ep->rcv_mpath);
707e0b8e63eSJohn Marino 	if (ep->c_blen > 0)
708e0b8e63eSJohn Marino 		free(ep->c_lp);
709e0b8e63eSJohn Marino 
710e0b8e63eSJohn Marino 	free(ep);
711e0b8e63eSJohn Marino 	return (0);
712e0b8e63eSJohn Marino }
713e0b8e63eSJohn Marino 
714e0b8e63eSJohn Marino /*
715e0b8e63eSJohn Marino  * file_write --
716e0b8e63eSJohn Marino  *	Write the file to disk.  Historic vi had fairly convoluted
717e0b8e63eSJohn Marino  *	semantics for whether or not writes would happen.  That's
718e0b8e63eSJohn Marino  *	why all the flags.
719e0b8e63eSJohn Marino  *
720e0b8e63eSJohn Marino  * PUBLIC: int file_write(SCR *, MARK *, MARK *, char *, int);
721e0b8e63eSJohn Marino  */
722e0b8e63eSJohn Marino int
file_write(SCR * sp,MARK * fm,MARK * tm,char * name,int flags)723*b1ac2ebbSDaniel Fojt file_write(SCR *sp, MARK *fm, MARK *tm, char *name, int flags)
724e0b8e63eSJohn Marino {
725e0b8e63eSJohn Marino 	enum { NEWFILE, OLDFILE } mtype;
726e0b8e63eSJohn Marino 	struct stat sb;
727e0b8e63eSJohn Marino 	EXF *ep;
728e0b8e63eSJohn Marino 	FILE *fp;
729e0b8e63eSJohn Marino 	FREF *frp;
730e0b8e63eSJohn Marino 	MARK from, to;
731e0b8e63eSJohn Marino 	size_t len;
732e0b8e63eSJohn Marino 	u_long nlno, nch;
733e0b8e63eSJohn Marino 	int fd, nf, noname, oflags, rval;
734e0b8e63eSJohn Marino 	char *p, *s, *t, buf[1024];
735e0b8e63eSJohn Marino 	const char *msgstr;
736e0b8e63eSJohn Marino 
737e0b8e63eSJohn Marino 	ep = sp->ep;
738e0b8e63eSJohn Marino 	frp = sp->frp;
739e0b8e63eSJohn Marino 
740e0b8e63eSJohn Marino 	/*
741e0b8e63eSJohn Marino 	 * Writing '%', or naming the current file explicitly, has the
742e0b8e63eSJohn Marino 	 * same semantics as writing without a name.
743e0b8e63eSJohn Marino 	 */
744e0b8e63eSJohn Marino 	if (name == NULL || !strcmp(name, frp->name)) {
745e0b8e63eSJohn Marino 		noname = 1;
746e0b8e63eSJohn Marino 		name = frp->name;
747e0b8e63eSJohn Marino 	} else
748e0b8e63eSJohn Marino 		noname = 0;
749e0b8e63eSJohn Marino 
750e0b8e63eSJohn Marino 	/* Can't write files marked read-only, unless forced. */
751e0b8e63eSJohn Marino 	if (!LF_ISSET(FS_FORCE) && noname && O_ISSET(sp, O_READONLY)) {
752e0b8e63eSJohn Marino 		msgq(sp, M_ERR, LF_ISSET(FS_POSSIBLE) ?
753e0b8e63eSJohn Marino 		    "244|Read-only file, not written; use ! to override" :
754e0b8e63eSJohn Marino 		    "245|Read-only file, not written");
755e0b8e63eSJohn Marino 		return (1);
756e0b8e63eSJohn Marino 	}
757e0b8e63eSJohn Marino 
758e0b8e63eSJohn Marino 	/* If not forced, not appending, and "writeany" not set ... */
759e0b8e63eSJohn Marino 	if (!LF_ISSET(FS_FORCE | FS_APPEND) && !O_ISSET(sp, O_WRITEANY)) {
760e0b8e63eSJohn Marino 		/* Don't overwrite anything but the original file. */
761e0b8e63eSJohn Marino 		if ((!noname || F_ISSET(frp, FR_NAMECHANGE)) &&
762e0b8e63eSJohn Marino 		    !stat(name, &sb)) {
763e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, name,
764e0b8e63eSJohn Marino 			    LF_ISSET(FS_POSSIBLE) ?
765e0b8e63eSJohn Marino 			    "246|%s exists, not written; use ! to override" :
766e0b8e63eSJohn Marino 			    "247|%s exists, not written");
767e0b8e63eSJohn Marino 			return (1);
768e0b8e63eSJohn Marino 		}
769e0b8e63eSJohn Marino 
770e0b8e63eSJohn Marino 		/*
771e0b8e63eSJohn Marino 		 * Don't write part of any existing file.  Only test for the
772e0b8e63eSJohn Marino 		 * original file, the previous test catches anything else.
773e0b8e63eSJohn Marino 		 */
774e0b8e63eSJohn Marino 		if (!LF_ISSET(FS_ALL) && noname && !stat(name, &sb)) {
775e0b8e63eSJohn Marino 			msgq(sp, M_ERR, LF_ISSET(FS_POSSIBLE) ?
776e0b8e63eSJohn Marino 			    "248|Partial file, not written; use ! to override" :
777e0b8e63eSJohn Marino 			    "249|Partial file, not written");
778e0b8e63eSJohn Marino 			return (1);
779e0b8e63eSJohn Marino 		}
780e0b8e63eSJohn Marino 	}
781e0b8e63eSJohn Marino 
782e0b8e63eSJohn Marino 	/*
783e0b8e63eSJohn Marino 	 * Figure out if the file already exists -- if it doesn't, we display
784e0b8e63eSJohn Marino 	 * the "new file" message.  The stat might not be necessary, but we
785e0b8e63eSJohn Marino 	 * just repeat it because it's easier than hacking the previous tests.
786e0b8e63eSJohn Marino 	 * The information is only used for the user message and modification
787e0b8e63eSJohn Marino 	 * time test, so we can ignore the obvious race condition.
788e0b8e63eSJohn Marino 	 *
789e0b8e63eSJohn Marino 	 * One final test.  If we're not forcing or appending the current file,
790e0b8e63eSJohn Marino 	 * and we have a saved modification time, object if the file changed
791e0b8e63eSJohn Marino 	 * since we last edited or wrote it, and make them force it.
792e0b8e63eSJohn Marino 	 */
793e0b8e63eSJohn Marino 	if (stat(name, &sb))
794e0b8e63eSJohn Marino 		mtype = NEWFILE;
795e0b8e63eSJohn Marino 	else {
796e0b8e63eSJohn Marino 		if (noname && !LF_ISSET(FS_FORCE | FS_APPEND) &&
797e0b8e63eSJohn Marino 		    ((F_ISSET(ep, F_DEVSET) &&
798e0b8e63eSJohn Marino 		    (sb.st_dev != ep->mdev || sb.st_ino != ep->minode)) ||
799*b1ac2ebbSDaniel Fojt 		    timespeccmp(&sb.st_mtim, &ep->mtim, !=))) {
800e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, name, LF_ISSET(FS_POSSIBLE) ?
801e0b8e63eSJohn Marino "250|%s: file modified more recently than this copy; use ! to override" :
802e0b8e63eSJohn Marino "251|%s: file modified more recently than this copy");
803e0b8e63eSJohn Marino 			return (1);
804e0b8e63eSJohn Marino 		}
805e0b8e63eSJohn Marino 
806e0b8e63eSJohn Marino 		mtype = OLDFILE;
807e0b8e63eSJohn Marino 	}
808e0b8e63eSJohn Marino 
809e0b8e63eSJohn Marino 	/* Set flags to create, write, and either append or truncate. */
810e0b8e63eSJohn Marino 	oflags = O_CREAT | O_WRONLY |
811e0b8e63eSJohn Marino 	    (LF_ISSET(FS_APPEND) ? O_APPEND : O_TRUNC);
812e0b8e63eSJohn Marino 
813e0b8e63eSJohn Marino 	/* Backup the file if requested. */
814e0b8e63eSJohn Marino 	if (!opts_empty(sp, O_BACKUP, 1) &&
815e0b8e63eSJohn Marino 	    file_backup(sp, name, O_STR(sp, O_BACKUP)) && !LF_ISSET(FS_FORCE))
816e0b8e63eSJohn Marino 		return (1);
817e0b8e63eSJohn Marino 
818e0b8e63eSJohn Marino 	/* Open the file. */
819e0b8e63eSJohn Marino 	if ((fd = open(name, oflags,
820e0b8e63eSJohn Marino 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
821e0b8e63eSJohn Marino 		if (errno == EACCES && LF_ISSET(FS_FORCE)) {
822e0b8e63eSJohn Marino 			/*
823e0b8e63eSJohn Marino 			 * If the user owns the file but does not
824e0b8e63eSJohn Marino 			 * have write permission on it, grant it
825e0b8e63eSJohn Marino 			 * automatically for the duration of the
826e0b8e63eSJohn Marino 			 * opening of the file, if possible.
827e0b8e63eSJohn Marino 			 */
828e0b8e63eSJohn Marino 			struct stat sb;
829e0b8e63eSJohn Marino 			mode_t fmode;
830e0b8e63eSJohn Marino 
831e0b8e63eSJohn Marino 			if (stat(name, &sb) != 0)
832e0b8e63eSJohn Marino 				goto fail_open;
833e0b8e63eSJohn Marino 			fmode = sb.st_mode;
834e0b8e63eSJohn Marino 			if (!(sb.st_mode & S_IWUSR) && sb.st_uid == getuid())
835e0b8e63eSJohn Marino 				fmode |= S_IWUSR;
836e0b8e63eSJohn Marino 			else
837e0b8e63eSJohn Marino 				goto fail_open;
838e0b8e63eSJohn Marino 			if (chmod(name, fmode) != 0)
839e0b8e63eSJohn Marino 				goto fail_open;
840e0b8e63eSJohn Marino 			fd = open(name, oflags, S_IRUSR | S_IWUSR |
841e0b8e63eSJohn Marino 			    S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
842e0b8e63eSJohn Marino 			if (fd == -1)
843e0b8e63eSJohn Marino 				goto fail_open;
844e0b8e63eSJohn Marino 			(void)fchmod(fd, sb.st_mode);
845e0b8e63eSJohn Marino 			goto success_open;
846e0b8e63eSJohn Marino 		fail_open:
847e0b8e63eSJohn Marino 			errno = EACCES;
848e0b8e63eSJohn Marino 		}
849e0b8e63eSJohn Marino 		msgq_str(sp, M_SYSERR, name, "%s");
850e0b8e63eSJohn Marino 		return (1);
851e0b8e63eSJohn Marino 	}
852e0b8e63eSJohn Marino success_open:
853e0b8e63eSJohn Marino 
854e0b8e63eSJohn Marino 	/* Try and get a lock. */
855e0b8e63eSJohn Marino 	if (!noname && file_lock(sp, NULL, fd, 0) == LOCK_UNAVAIL)
856e0b8e63eSJohn Marino 		msgq_str(sp, M_ERR, name,
857e0b8e63eSJohn Marino 		    "252|%s: write lock was unavailable");
858e0b8e63eSJohn Marino 
859e0b8e63eSJohn Marino 	/*
860e0b8e63eSJohn Marino 	 * Use stdio for buffering.
861e0b8e63eSJohn Marino 	 *
862e0b8e63eSJohn Marino 	 * XXX
863e0b8e63eSJohn Marino 	 * SVR4.2 requires the fdopen mode exactly match the original open
864e0b8e63eSJohn Marino 	 * mode, i.e. you have to open with "a" if appending.
865e0b8e63eSJohn Marino 	 */
866e0b8e63eSJohn Marino 	if ((fp = fdopen(fd, LF_ISSET(FS_APPEND) ? "a" : "w")) == NULL) {
867e0b8e63eSJohn Marino 		msgq_str(sp, M_SYSERR, name, "%s");
868e0b8e63eSJohn Marino 		(void)close(fd);
869e0b8e63eSJohn Marino 		return (1);
870e0b8e63eSJohn Marino 	}
871e0b8e63eSJohn Marino 
872e0b8e63eSJohn Marino 	/* Build fake addresses, if necessary. */
873e0b8e63eSJohn Marino 	if (fm == NULL) {
874e0b8e63eSJohn Marino 		from.lno = 1;
875e0b8e63eSJohn Marino 		from.cno = 0;
876e0b8e63eSJohn Marino 		fm = &from;
877e0b8e63eSJohn Marino 		if (db_last(sp, &to.lno))
878e0b8e63eSJohn Marino 			return (1);
879e0b8e63eSJohn Marino 		to.cno = 0;
880e0b8e63eSJohn Marino 		tm = &to;
881e0b8e63eSJohn Marino 	}
882e0b8e63eSJohn Marino 
883e0b8e63eSJohn Marino 	rval = ex_writefp(sp, name, fp, fm, tm, &nlno, &nch, 0);
884e0b8e63eSJohn Marino 
885e0b8e63eSJohn Marino 	/*
886e0b8e63eSJohn Marino 	 * Save the new last modification time -- even if the write fails
887e0b8e63eSJohn Marino 	 * we re-init the time.  That way the user can clean up the disk
888e0b8e63eSJohn Marino 	 * and rewrite without having to force it.
889e0b8e63eSJohn Marino 	 */
890e0b8e63eSJohn Marino 	if (noname)
891e0b8e63eSJohn Marino 		if (stat(name, &sb))
892e0b8e63eSJohn Marino 			timepoint_system(&ep->mtim);
893e0b8e63eSJohn Marino 		else {
894e0b8e63eSJohn Marino 			F_SET(ep, F_DEVSET);
895e0b8e63eSJohn Marino 			ep->mdev = sb.st_dev;
896e0b8e63eSJohn Marino 			ep->minode = sb.st_ino;
897e0b8e63eSJohn Marino 
898*b1ac2ebbSDaniel Fojt 			ep->mtim = sb.st_mtim;
899e0b8e63eSJohn Marino 		}
900e0b8e63eSJohn Marino 
901e0b8e63eSJohn Marino 	/*
902e0b8e63eSJohn Marino 	 * If the write failed, complain loudly.  ex_writefp() has already
903e0b8e63eSJohn Marino 	 * complained about the actual error, reinforce it if data was lost.
904e0b8e63eSJohn Marino 	 */
905e0b8e63eSJohn Marino 	if (rval) {
906e0b8e63eSJohn Marino 		if (!LF_ISSET(FS_APPEND))
907e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, name,
908e0b8e63eSJohn Marino 			    "254|%s: WARNING: FILE TRUNCATED");
909e0b8e63eSJohn Marino 		return (1);
910e0b8e63eSJohn Marino 	}
911e0b8e63eSJohn Marino 
912e0b8e63eSJohn Marino 	/*
913e0b8e63eSJohn Marino 	 * Once we've actually written the file, it doesn't matter that the
914e0b8e63eSJohn Marino 	 * file name was changed -- if it was, we've already whacked it.
915e0b8e63eSJohn Marino 	 */
916e0b8e63eSJohn Marino 	F_CLR(frp, FR_NAMECHANGE);
917e0b8e63eSJohn Marino 
918e0b8e63eSJohn Marino 	/*
919e0b8e63eSJohn Marino 	 * If wrote the entire file, and it wasn't by appending it to a file,
920e0b8e63eSJohn Marino 	 * clear the modified bit.  If the file was written to the original
921e0b8e63eSJohn Marino 	 * file name and the file is a temporary, set the "no exit" bit.  This
922e0b8e63eSJohn Marino 	 * permits the user to write the file and use it in the context of the
923e0b8e63eSJohn Marino 	 * filesystem, but still keeps them from discarding their changes by
924e0b8e63eSJohn Marino 	 * exiting.
925e0b8e63eSJohn Marino 	 */
926e0b8e63eSJohn Marino 	if (LF_ISSET(FS_ALL) && !LF_ISSET(FS_APPEND)) {
927e0b8e63eSJohn Marino 		F_CLR(ep, F_MODIFIED);
928e0b8e63eSJohn Marino 		if (F_ISSET(frp, FR_TMPFILE))
929e0b8e63eSJohn Marino 			if (noname)
930e0b8e63eSJohn Marino 				F_SET(frp, FR_TMPEXIT);
931e0b8e63eSJohn Marino 			else
932e0b8e63eSJohn Marino 				F_CLR(frp, FR_TMPEXIT);
933e0b8e63eSJohn Marino 	}
934e0b8e63eSJohn Marino 
935e0b8e63eSJohn Marino 	p = msg_print(sp, name, &nf);
936e0b8e63eSJohn Marino 	switch (mtype) {
937e0b8e63eSJohn Marino 	case NEWFILE:
938e0b8e63eSJohn Marino 		msgstr = msg_cat(sp,
939e0b8e63eSJohn Marino 		    "256|%s: new file: %lu lines, %lu characters", NULL);
940e0b8e63eSJohn Marino 		len = snprintf(buf, sizeof(buf), msgstr, p, nlno, nch);
941e0b8e63eSJohn Marino 		break;
942e0b8e63eSJohn Marino 	case OLDFILE:
943e0b8e63eSJohn Marino 		msgstr = msg_cat(sp, LF_ISSET(FS_APPEND) ?
944e0b8e63eSJohn Marino 		    "315|%s: appended: %lu lines, %lu characters" :
945e0b8e63eSJohn Marino 		    "257|%s: %lu lines, %lu characters", NULL);
946e0b8e63eSJohn Marino 		len = snprintf(buf, sizeof(buf), msgstr, p, nlno, nch);
947e0b8e63eSJohn Marino 		break;
948e0b8e63eSJohn Marino 	default:
949e0b8e63eSJohn Marino 		abort();
950e0b8e63eSJohn Marino 	}
951e0b8e63eSJohn Marino 
952e0b8e63eSJohn Marino 	/*
953e0b8e63eSJohn Marino 	 * There's a nasty problem with long path names.  Cscope and tags files
954e0b8e63eSJohn Marino 	 * can result in long paths and vi will request a continuation key from
955e0b8e63eSJohn Marino 	 * the user.  Unfortunately, the user has typed ahead, and chaos will
956e0b8e63eSJohn Marino 	 * result.  If we assume that the characters in the filenames only take
957e0b8e63eSJohn Marino 	 * a single screen column each, we can trim the filename.
958e0b8e63eSJohn Marino 	 */
959e0b8e63eSJohn Marino 	s = buf;
960e0b8e63eSJohn Marino 	if (len >= sp->cols) {
961e0b8e63eSJohn Marino 		for (s = buf, t = buf + strlen(p); s < t &&
962e0b8e63eSJohn Marino 		    (*s != '/' || len >= sp->cols - 3); ++s, --len);
963e0b8e63eSJohn Marino 		if (s == t)
964e0b8e63eSJohn Marino 			s = buf;
965e0b8e63eSJohn Marino 		else {
966e0b8e63eSJohn Marino 			*--s = '.';		/* Leading ellipses. */
967e0b8e63eSJohn Marino 			*--s = '.';
968e0b8e63eSJohn Marino 			*--s = '.';
969e0b8e63eSJohn Marino 		}
970e0b8e63eSJohn Marino 	}
971e0b8e63eSJohn Marino 	msgq(sp, M_INFO, "%s", s);
972e0b8e63eSJohn Marino 	if (nf)
973e0b8e63eSJohn Marino 		FREE_SPACE(sp, p, 0);
974e0b8e63eSJohn Marino 	return (0);
975e0b8e63eSJohn Marino }
976e0b8e63eSJohn Marino 
977e0b8e63eSJohn Marino /*
978e0b8e63eSJohn Marino  * file_backup --
979e0b8e63eSJohn Marino  *	Backup the about-to-be-written file.
980e0b8e63eSJohn Marino  *
981e0b8e63eSJohn Marino  * XXX
982e0b8e63eSJohn Marino  * We do the backup by copying the entire file.  It would be nice to do
983e0b8e63eSJohn Marino  * a rename instead, but: (1) both files may not fit and we want to fail
984e0b8e63eSJohn Marino  * before doing the rename; (2) the backup file may not be on the same
985e0b8e63eSJohn Marino  * disk partition as the file being written; (3) there may be optional
986e0b8e63eSJohn Marino  * file information (MACs, DACs, whatever) that we won't get right if we
987e0b8e63eSJohn Marino  * recreate the file.  So, let's not risk it.
988e0b8e63eSJohn Marino  */
989e0b8e63eSJohn Marino static int
file_backup(SCR * sp,char * name,char * bname)990*b1ac2ebbSDaniel Fojt file_backup(SCR *sp, char *name, char *bname)
991e0b8e63eSJohn Marino {
992e0b8e63eSJohn Marino 	struct dirent *dp;
993e0b8e63eSJohn Marino 	struct stat sb;
994e0b8e63eSJohn Marino 	DIR *dirp;
995e0b8e63eSJohn Marino 	EXCMD cmd;
996e0b8e63eSJohn Marino 	off_t off;
997e0b8e63eSJohn Marino 	size_t blen;
998e0b8e63eSJohn Marino 	int flags, maxnum, nr, num, nw, rfd, wfd, version;
999e0b8e63eSJohn Marino 	char *bp, *estr, *p, *pct, *slash, *t, *wfname, buf[8192];
1000e0b8e63eSJohn Marino 	CHAR_T *wp;
1001e0b8e63eSJohn Marino 	size_t wlen;
1002e0b8e63eSJohn Marino 	size_t nlen;
1003e0b8e63eSJohn Marino 	char *d = NULL;
1004e0b8e63eSJohn Marino 
1005e0b8e63eSJohn Marino 	rfd = wfd = -1;
1006e0b8e63eSJohn Marino 	bp = estr = wfname = NULL;
1007e0b8e63eSJohn Marino 
1008e0b8e63eSJohn Marino 	/*
1009e0b8e63eSJohn Marino 	 * Open the current file for reading.  Do this first, so that
1010e0b8e63eSJohn Marino 	 * we don't exec a shell before the most likely failure point.
1011e0b8e63eSJohn Marino 	 * If it doesn't exist, it's okay, there's just nothing to back
1012e0b8e63eSJohn Marino 	 * up.
1013e0b8e63eSJohn Marino 	 */
1014e0b8e63eSJohn Marino 	errno = 0;
1015e0b8e63eSJohn Marino 	if ((rfd = open(name, O_RDONLY, 0)) < 0) {
1016e0b8e63eSJohn Marino 		if (errno == ENOENT)
1017e0b8e63eSJohn Marino 			return (0);
1018e0b8e63eSJohn Marino 		estr = name;
1019e0b8e63eSJohn Marino 		goto err;
1020e0b8e63eSJohn Marino 	}
1021e0b8e63eSJohn Marino 
1022e0b8e63eSJohn Marino 	/*
1023e0b8e63eSJohn Marino 	 * If the name starts with an 'N' character, add a version number
1024e0b8e63eSJohn Marino 	 * to the name.  Strip the leading N from the string passed to the
1025e0b8e63eSJohn Marino 	 * expansion routines, for no particular reason.  It would be nice
1026e0b8e63eSJohn Marino 	 * to permit users to put the version number anywhere in the backup
1027e0b8e63eSJohn Marino 	 * name, but there isn't a special character that we can use in the
1028e0b8e63eSJohn Marino 	 * name, and giving a new character a special meaning leads to ugly
1029e0b8e63eSJohn Marino 	 * hacks both here and in the supporting ex routines.
1030e0b8e63eSJohn Marino 	 *
1031e0b8e63eSJohn Marino 	 * Shell and file name expand the option's value.
1032e0b8e63eSJohn Marino 	 */
1033e0b8e63eSJohn Marino 	ex_cinit(sp, &cmd, 0, 0, 0, 0, 0);
1034e0b8e63eSJohn Marino 	if (bname[0] == 'N') {
1035e0b8e63eSJohn Marino 		version = 1;
1036e0b8e63eSJohn Marino 		++bname;
1037e0b8e63eSJohn Marino 	} else
1038e0b8e63eSJohn Marino 		version = 0;
1039e0b8e63eSJohn Marino 	CHAR2INT(sp, bname, strlen(bname), wp, wlen);
1040e0b8e63eSJohn Marino 	if ((wp = v_wstrdup(sp, wp, wlen)) == NULL)
1041e0b8e63eSJohn Marino 		return (1);
1042e0b8e63eSJohn Marino 	if (argv_exp2(sp, &cmd, wp, wlen)) {
1043e0b8e63eSJohn Marino 		free(wp);
1044e0b8e63eSJohn Marino 		return (1);
1045e0b8e63eSJohn Marino 	}
1046e0b8e63eSJohn Marino 	free(wp);
1047e0b8e63eSJohn Marino 
1048e0b8e63eSJohn Marino 	/*
1049e0b8e63eSJohn Marino 	 *  0 args: impossible.
1050e0b8e63eSJohn Marino 	 *  1 args: use it.
1051e0b8e63eSJohn Marino 	 * >1 args: object, too many args.
1052e0b8e63eSJohn Marino 	 */
1053e0b8e63eSJohn Marino 	if (cmd.argc != 1) {
1054e0b8e63eSJohn Marino 		msgq_str(sp, M_ERR, bname,
1055e0b8e63eSJohn Marino 		    "258|%s expanded into too many file names");
1056e0b8e63eSJohn Marino 		(void)close(rfd);
1057e0b8e63eSJohn Marino 		return (1);
1058e0b8e63eSJohn Marino 	}
1059e0b8e63eSJohn Marino 
1060e0b8e63eSJohn Marino 	/*
1061e0b8e63eSJohn Marino 	 * If appending a version number, read through the directory, looking
1062e0b8e63eSJohn Marino 	 * for file names that match the name followed by a number.  Make all
1063e0b8e63eSJohn Marino 	 * of the other % characters in name literal, so the user doesn't get
1064e0b8e63eSJohn Marino 	 * surprised and sscanf doesn't drop core indirecting through pointers
1065e0b8e63eSJohn Marino 	 * that don't exist.  If any such files are found, increment its number
1066e0b8e63eSJohn Marino 	 * by one.
1067e0b8e63eSJohn Marino 	 */
1068e0b8e63eSJohn Marino 	if (version) {
1069e0b8e63eSJohn Marino 		GET_SPACE_GOTOC(sp, bp, blen, cmd.argv[0]->len * 2 + 50);
1070e0b8e63eSJohn Marino 		INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1,
1071e0b8e63eSJohn Marino 			 p, nlen);
1072e0b8e63eSJohn Marino 		d = strdup(p);
1073e0b8e63eSJohn Marino 		p = d;
1074e0b8e63eSJohn Marino 		for (t = bp, slash = NULL;
1075e0b8e63eSJohn Marino 		     p[0] != '\0'; *t++ = *p++)
1076e0b8e63eSJohn Marino 			if (p[0] == '%') {
1077e0b8e63eSJohn Marino 				if (p[1] != '%')
1078e0b8e63eSJohn Marino 					*t++ = '%';
1079e0b8e63eSJohn Marino 			} else if (p[0] == '/')
1080e0b8e63eSJohn Marino 				slash = t;
1081e0b8e63eSJohn Marino 		pct = t;
1082e0b8e63eSJohn Marino 		*t++ = '%';
1083e0b8e63eSJohn Marino 		*t++ = 'd';
1084e0b8e63eSJohn Marino 		*t = '\0';
1085e0b8e63eSJohn Marino 
1086e0b8e63eSJohn Marino 		if (slash == NULL) {
1087e0b8e63eSJohn Marino 			dirp = opendir(".");
1088e0b8e63eSJohn Marino 			p = bp;
1089e0b8e63eSJohn Marino 		} else {
1090e0b8e63eSJohn Marino 			*slash = '\0';
1091e0b8e63eSJohn Marino 			dirp = opendir(bp);
1092e0b8e63eSJohn Marino 			*slash = '/';
1093e0b8e63eSJohn Marino 			p = slash + 1;
1094e0b8e63eSJohn Marino 		}
1095e0b8e63eSJohn Marino 		if (dirp == NULL) {
1096e0b8e63eSJohn Marino 			INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1,
1097e0b8e63eSJohn Marino 				estr, nlen);
1098e0b8e63eSJohn Marino 			goto err;
1099e0b8e63eSJohn Marino 		}
1100e0b8e63eSJohn Marino 
1101e0b8e63eSJohn Marino 		for (maxnum = 0; (dp = readdir(dirp)) != NULL;)
1102e0b8e63eSJohn Marino 			if (sscanf(dp->d_name, p, &num) == 1 && num > maxnum)
1103e0b8e63eSJohn Marino 				maxnum = num;
1104e0b8e63eSJohn Marino 		(void)closedir(dirp);
1105e0b8e63eSJohn Marino 
1106e0b8e63eSJohn Marino 		/* Format the backup file name. */
1107e0b8e63eSJohn Marino 		(void)snprintf(pct, blen - (pct - bp), "%d", maxnum + 1);
1108e0b8e63eSJohn Marino 		wfname = bp;
1109e0b8e63eSJohn Marino 	} else {
1110e0b8e63eSJohn Marino 		bp = NULL;
1111e0b8e63eSJohn Marino 		INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1,
1112e0b8e63eSJohn Marino 			wfname, nlen);
1113e0b8e63eSJohn Marino 	}
1114e0b8e63eSJohn Marino 
1115e0b8e63eSJohn Marino 	/* Open the backup file, avoiding lurkers. */
1116e0b8e63eSJohn Marino 	if (stat(wfname, &sb) == 0) {
1117e0b8e63eSJohn Marino 		if (!S_ISREG(sb.st_mode)) {
1118e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, bname,
1119e0b8e63eSJohn Marino 			    "259|%s: not a regular file");
1120e0b8e63eSJohn Marino 			goto err;
1121e0b8e63eSJohn Marino 		}
1122e0b8e63eSJohn Marino 		if (sb.st_uid != getuid()) {
1123e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, bname, "260|%s: not owned by you");
1124e0b8e63eSJohn Marino 			goto err;
1125e0b8e63eSJohn Marino 		}
1126e0b8e63eSJohn Marino 		if (sb.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) {
1127e0b8e63eSJohn Marino 			msgq_str(sp, M_ERR, bname,
1128e0b8e63eSJohn Marino 			   "261|%s: accessible by a user other than the owner");
1129e0b8e63eSJohn Marino 			goto err;
1130e0b8e63eSJohn Marino 		}
1131e0b8e63eSJohn Marino 		flags = O_TRUNC;
1132e0b8e63eSJohn Marino 	} else
1133e0b8e63eSJohn Marino 		flags = O_CREAT | O_EXCL;
1134e0b8e63eSJohn Marino 	if ((wfd = open(wfname, flags | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) {
1135e0b8e63eSJohn Marino 		estr = bname;
1136e0b8e63eSJohn Marino 		goto err;
1137e0b8e63eSJohn Marino 	}
1138e0b8e63eSJohn Marino 
1139e0b8e63eSJohn Marino 	/* Copy the file's current contents to its backup value. */
1140e0b8e63eSJohn Marino 	while ((nr = read(rfd, buf, sizeof(buf))) > 0)
1141e0b8e63eSJohn Marino 		for (off = 0; nr != 0; nr -= nw, off += nw)
1142e0b8e63eSJohn Marino 			if ((nw = write(wfd, buf + off, nr)) < 0) {
1143e0b8e63eSJohn Marino 				estr = wfname;
1144e0b8e63eSJohn Marino 				goto err;
1145e0b8e63eSJohn Marino 			}
1146e0b8e63eSJohn Marino 	if (nr < 0) {
1147e0b8e63eSJohn Marino 		estr = name;
1148e0b8e63eSJohn Marino 		goto err;
1149e0b8e63eSJohn Marino 	}
1150e0b8e63eSJohn Marino 
1151e0b8e63eSJohn Marino 	if (close(rfd)) {
1152e0b8e63eSJohn Marino 		estr = name;
1153e0b8e63eSJohn Marino 		goto err;
1154e0b8e63eSJohn Marino 	}
1155e0b8e63eSJohn Marino 	if (close(wfd)) {
1156e0b8e63eSJohn Marino 		estr = wfname;
1157e0b8e63eSJohn Marino 		goto err;
1158e0b8e63eSJohn Marino 	}
1159*b1ac2ebbSDaniel Fojt 	free(d);
1160e0b8e63eSJohn Marino 	if (bp != NULL)
1161e0b8e63eSJohn Marino 		FREE_SPACE(sp, bp, blen);
1162e0b8e63eSJohn Marino 	return (0);
1163e0b8e63eSJohn Marino 
1164e0b8e63eSJohn Marino alloc_err:
1165e0b8e63eSJohn Marino err:	if (rfd != -1)
1166e0b8e63eSJohn Marino 		(void)close(rfd);
1167e0b8e63eSJohn Marino 	if (wfd != -1) {
1168e0b8e63eSJohn Marino 		(void)unlink(wfname);
1169e0b8e63eSJohn Marino 		(void)close(wfd);
1170e0b8e63eSJohn Marino 	}
1171e0b8e63eSJohn Marino 	if (estr)
1172e0b8e63eSJohn Marino 		msgq_str(sp, M_SYSERR, estr, "%s");
1173e0b8e63eSJohn Marino 	free(d);
1174e0b8e63eSJohn Marino 	if (bp != NULL)
1175e0b8e63eSJohn Marino 		FREE_SPACE(sp, bp, blen);
1176e0b8e63eSJohn Marino 	return (1);
1177e0b8e63eSJohn Marino }
1178e0b8e63eSJohn Marino 
1179e0b8e63eSJohn Marino /*
1180e0b8e63eSJohn Marino  * file_encinit --
1181e0b8e63eSJohn Marino  *	Read the first line and set the O_FILEENCODING.
1182e0b8e63eSJohn Marino  */
1183e0b8e63eSJohn Marino static void
file_encinit(SCR * sp)1184e0b8e63eSJohn Marino file_encinit(SCR *sp)
1185e0b8e63eSJohn Marino {
1186e0b8e63eSJohn Marino #if defined(USE_WIDECHAR) && defined(USE_ICONV)
1187e0b8e63eSJohn Marino 	size_t len;
1188e0b8e63eSJohn Marino 	char *p;
1189e0b8e63eSJohn Marino 	size_t blen = 0;
1190e0b8e63eSJohn Marino 	char buf[4096];	/* not need to be '\0'-terminated */
1191e0b8e63eSJohn Marino 	recno_t ln = 1;
1192e0b8e63eSJohn Marino 	EXF *ep;
1193e0b8e63eSJohn Marino 
1194e0b8e63eSJohn Marino 	ep = sp->ep;
1195e0b8e63eSJohn Marino 
1196e0b8e63eSJohn Marino 	while (!db_rget(sp, ln++, &p, &len)) {
1197e0b8e63eSJohn Marino 		if (blen + len > sizeof(buf))
1198e0b8e63eSJohn Marino 			len = sizeof(buf) - blen;
1199e0b8e63eSJohn Marino 		memcpy(buf + blen, p, len);
1200e0b8e63eSJohn Marino 		blen += len;
1201e0b8e63eSJohn Marino 		if (blen == sizeof(buf))
1202e0b8e63eSJohn Marino 			break;
1203e0b8e63eSJohn Marino 		else
1204e0b8e63eSJohn Marino 			buf[blen++] = '\n';
1205e0b8e63eSJohn Marino 	}
1206e0b8e63eSJohn Marino 
1207e0b8e63eSJohn Marino 	/*
1208e0b8e63eSJohn Marino 	 * Detect UTF-8 and fallback to the locale/preset encoding.
1209e0b8e63eSJohn Marino 	 *
1210e0b8e63eSJohn Marino 	 * XXX
1211e0b8e63eSJohn Marino 	 * A manually set O_FILEENCODING indicates the "fallback
1212e0b8e63eSJohn Marino 	 * encoding", but UTF-8, which can be safely detected, is not
1213e0b8e63eSJohn Marino 	 * inherited from the old screen.
1214e0b8e63eSJohn Marino 	 */
1215e0b8e63eSJohn Marino 	if (looks_utf8(buf, blen) > 1)
1216e0b8e63eSJohn Marino 		o_set(sp, O_FILEENCODING, OS_STRDUP, "utf-8", 0);
1217e0b8e63eSJohn Marino 	else if (!O_ISSET(sp, O_FILEENCODING) ||
1218e0b8e63eSJohn Marino 	    !strcasecmp(O_STR(sp, O_FILEENCODING), "utf-8"))
1219e0b8e63eSJohn Marino 		o_set(sp, O_FILEENCODING, OS_STRDUP, codeset(), 0);
1220e0b8e63eSJohn Marino 
1221e0b8e63eSJohn Marino 	conv_enc(sp, O_FILEENCODING, 0);
1222e0b8e63eSJohn Marino #endif
1223e0b8e63eSJohn Marino }
1224e0b8e63eSJohn Marino 
1225e0b8e63eSJohn Marino /*
1226e0b8e63eSJohn Marino  * file_comment --
1227e0b8e63eSJohn Marino  *	Skip the first comment.
1228e0b8e63eSJohn Marino  */
1229e0b8e63eSJohn Marino static void
file_comment(SCR * sp)1230e0b8e63eSJohn Marino file_comment(SCR *sp)
1231e0b8e63eSJohn Marino {
1232e0b8e63eSJohn Marino 	recno_t lno;
1233e0b8e63eSJohn Marino 	size_t len;
1234e0b8e63eSJohn Marino 	CHAR_T *p;
1235e0b8e63eSJohn Marino 
1236e0b8e63eSJohn Marino 	for (lno = 1; !db_get(sp, lno, 0, &p, &len) && len == 0; ++lno);
1237e0b8e63eSJohn Marino 	if (p == NULL)
1238e0b8e63eSJohn Marino 		return;
1239e0b8e63eSJohn Marino 	if (p[0] == '#') {
1240e0b8e63eSJohn Marino 		F_SET(sp, SC_SCR_TOP);
1241e0b8e63eSJohn Marino 		while (!db_get(sp, ++lno, 0, &p, &len))
1242e0b8e63eSJohn Marino 			if (len < 1 || p[0] != '#') {
1243e0b8e63eSJohn Marino 				sp->lno = lno;
1244e0b8e63eSJohn Marino 				return;
1245e0b8e63eSJohn Marino 			}
1246e0b8e63eSJohn Marino 	} else if (len > 1 && p[0] == '/' && p[1] == '*') {
1247e0b8e63eSJohn Marino 		F_SET(sp, SC_SCR_TOP);
1248e0b8e63eSJohn Marino 		do {
1249e0b8e63eSJohn Marino 			for (; len > 1; --len, ++p)
1250e0b8e63eSJohn Marino 				if (p[0] == '*' && p[1] == '/') {
1251e0b8e63eSJohn Marino 					sp->lno = lno;
1252e0b8e63eSJohn Marino 					return;
1253e0b8e63eSJohn Marino 				}
1254e0b8e63eSJohn Marino 		} while (!db_get(sp, ++lno, 0, &p, &len));
1255e0b8e63eSJohn Marino 	} else if (len > 1 && p[0] == '/' && p[1] == '/') {
1256e0b8e63eSJohn Marino 		F_SET(sp, SC_SCR_TOP);
1257e0b8e63eSJohn Marino 		p += 2;
1258e0b8e63eSJohn Marino 		len -= 2;
1259e0b8e63eSJohn Marino 		do {
1260e0b8e63eSJohn Marino 			for (; len > 1; --len, ++p)
1261e0b8e63eSJohn Marino 				if (p[0] == '/' && p[1] == '/') {
1262e0b8e63eSJohn Marino 					sp->lno = lno;
1263e0b8e63eSJohn Marino 					return;
1264e0b8e63eSJohn Marino 				}
1265e0b8e63eSJohn Marino 		} while (!db_get(sp, ++lno, 0, &p, &len));
1266e0b8e63eSJohn Marino 	}
1267e0b8e63eSJohn Marino }
1268e0b8e63eSJohn Marino 
1269e0b8e63eSJohn Marino /*
1270e0b8e63eSJohn Marino  * file_m1 --
1271e0b8e63eSJohn Marino  * 	First modification check routine.  The :next, :prev, :rewind, :tag,
1272e0b8e63eSJohn Marino  *	:tagpush, :tagpop, ^^ modifications check.
1273e0b8e63eSJohn Marino  *
1274e0b8e63eSJohn Marino  * PUBLIC: int file_m1(SCR *, int, int);
1275e0b8e63eSJohn Marino  */
1276e0b8e63eSJohn Marino int
file_m1(SCR * sp,int force,int flags)1277*b1ac2ebbSDaniel Fojt file_m1(SCR *sp, int force, int flags)
1278e0b8e63eSJohn Marino {
1279e0b8e63eSJohn Marino 	EXF *ep;
1280e0b8e63eSJohn Marino 
1281e0b8e63eSJohn Marino 	ep = sp->ep;
1282e0b8e63eSJohn Marino 
1283e0b8e63eSJohn Marino 	/* If no file loaded, return no modifications. */
1284e0b8e63eSJohn Marino 	if (ep == NULL)
1285e0b8e63eSJohn Marino 		return (0);
1286e0b8e63eSJohn Marino 
1287e0b8e63eSJohn Marino 	/*
1288e0b8e63eSJohn Marino 	 * If the file has been modified, we'll want to write it back or
1289e0b8e63eSJohn Marino 	 * fail.  If autowrite is set, we'll write it back automatically,
1290e0b8e63eSJohn Marino 	 * unless force is also set.  Otherwise, we fail unless forced or
1291e0b8e63eSJohn Marino 	 * there's another open screen on this file.
1292e0b8e63eSJohn Marino 	 */
1293e0b8e63eSJohn Marino 	if (F_ISSET(ep, F_MODIFIED))
1294e0b8e63eSJohn Marino 		if (O_ISSET(sp, O_AUTOWRITE)) {
1295e0b8e63eSJohn Marino 			if (!force && file_aw(sp, flags))
1296e0b8e63eSJohn Marino 				return (1);
1297e0b8e63eSJohn Marino 		} else if (ep->refcnt <= 1 && !force) {
1298e0b8e63eSJohn Marino 			msgq(sp, M_ERR, LF_ISSET(FS_POSSIBLE) ?
1299e0b8e63eSJohn Marino "262|File modified since last complete write; write or use ! to override" :
1300e0b8e63eSJohn Marino "263|File modified since last complete write; write or use :edit! to override");
1301e0b8e63eSJohn Marino 			return (1);
1302e0b8e63eSJohn Marino 		}
1303e0b8e63eSJohn Marino 
1304e0b8e63eSJohn Marino 	return (file_m3(sp, force));
1305e0b8e63eSJohn Marino }
1306e0b8e63eSJohn Marino 
1307e0b8e63eSJohn Marino /*
1308e0b8e63eSJohn Marino  * file_m2 --
1309e0b8e63eSJohn Marino  * 	Second modification check routine.  The :edit, :quit, :recover
1310e0b8e63eSJohn Marino  *	modifications check.
1311e0b8e63eSJohn Marino  *
1312e0b8e63eSJohn Marino  * PUBLIC: int file_m2(SCR *, int);
1313e0b8e63eSJohn Marino  */
1314e0b8e63eSJohn Marino int
file_m2(SCR * sp,int force)1315*b1ac2ebbSDaniel Fojt file_m2(SCR *sp, int force)
1316e0b8e63eSJohn Marino {
1317e0b8e63eSJohn Marino 	EXF *ep;
1318e0b8e63eSJohn Marino 
1319e0b8e63eSJohn Marino 	ep = sp->ep;
1320e0b8e63eSJohn Marino 
1321e0b8e63eSJohn Marino 	/* If no file loaded, return no modifications. */
1322e0b8e63eSJohn Marino 	if (ep == NULL)
1323e0b8e63eSJohn Marino 		return (0);
1324e0b8e63eSJohn Marino 
1325e0b8e63eSJohn Marino 	/*
1326e0b8e63eSJohn Marino 	 * If the file has been modified, we'll want to fail, unless forced
1327e0b8e63eSJohn Marino 	 * or there's another open screen on this file.
1328e0b8e63eSJohn Marino 	 */
1329e0b8e63eSJohn Marino 	if (F_ISSET(ep, F_MODIFIED) && ep->refcnt <= 1 && !force) {
1330e0b8e63eSJohn Marino 		msgq(sp, M_ERR,
1331e0b8e63eSJohn Marino "264|File modified since last complete write; write or use ! to override");
1332e0b8e63eSJohn Marino 		return (1);
1333e0b8e63eSJohn Marino 	}
1334e0b8e63eSJohn Marino 
1335e0b8e63eSJohn Marino 	return (file_m3(sp, force));
1336e0b8e63eSJohn Marino }
1337e0b8e63eSJohn Marino 
1338e0b8e63eSJohn Marino /*
1339e0b8e63eSJohn Marino  * file_m3 --
1340e0b8e63eSJohn Marino  * 	Third modification check routine.
1341e0b8e63eSJohn Marino  *
1342e0b8e63eSJohn Marino  * PUBLIC: int file_m3(SCR *, int);
1343e0b8e63eSJohn Marino  */
1344e0b8e63eSJohn Marino int
file_m3(SCR * sp,int force)1345*b1ac2ebbSDaniel Fojt file_m3(SCR *sp, int force)
1346e0b8e63eSJohn Marino {
1347e0b8e63eSJohn Marino 	EXF *ep;
1348e0b8e63eSJohn Marino 
1349e0b8e63eSJohn Marino 	ep = sp->ep;
1350e0b8e63eSJohn Marino 
1351e0b8e63eSJohn Marino 	/* If no file loaded, return no modifications. */
1352e0b8e63eSJohn Marino 	if (ep == NULL)
1353e0b8e63eSJohn Marino 		return (0);
1354e0b8e63eSJohn Marino 
1355e0b8e63eSJohn Marino 	/*
1356e0b8e63eSJohn Marino 	 * Don't exit while in a temporary files if the file was ever modified.
1357e0b8e63eSJohn Marino 	 * The problem is that if the user does a ":wq", we write and quit,
1358e0b8e63eSJohn Marino 	 * unlinking the temporary file.  Not what the user had in mind at all.
1359e0b8e63eSJohn Marino 	 * We permit writing to temporary files, so that user maps using file
1360e0b8e63eSJohn Marino 	 * system names work with temporary files.
1361e0b8e63eSJohn Marino 	 */
1362e0b8e63eSJohn Marino 	if (F_ISSET(sp->frp, FR_TMPEXIT) && ep->refcnt <= 1 && !force) {
1363e0b8e63eSJohn Marino 		msgq(sp, M_ERR,
1364e0b8e63eSJohn Marino 		    "265|File is a temporary; exit will discard modifications");
1365e0b8e63eSJohn Marino 		return (1);
1366e0b8e63eSJohn Marino 	}
1367e0b8e63eSJohn Marino 	return (0);
1368e0b8e63eSJohn Marino }
1369e0b8e63eSJohn Marino 
1370e0b8e63eSJohn Marino /*
1371e0b8e63eSJohn Marino  * file_aw --
1372e0b8e63eSJohn Marino  *	Autowrite routine.  If modified, autowrite is set and the readonly bit
1373e0b8e63eSJohn Marino  *	is not set, write the file.  A routine so there's a place to put the
1374e0b8e63eSJohn Marino  *	comment.
1375e0b8e63eSJohn Marino  *
1376e0b8e63eSJohn Marino  * PUBLIC: int file_aw(SCR *, int);
1377e0b8e63eSJohn Marino  */
1378e0b8e63eSJohn Marino int
file_aw(SCR * sp,int flags)1379*b1ac2ebbSDaniel Fojt file_aw(SCR *sp, int flags)
1380e0b8e63eSJohn Marino {
1381e0b8e63eSJohn Marino 	if (!F_ISSET(sp->ep, F_MODIFIED))
1382e0b8e63eSJohn Marino 		return (0);
1383e0b8e63eSJohn Marino 	if (!O_ISSET(sp, O_AUTOWRITE))
1384e0b8e63eSJohn Marino 		return (0);
1385e0b8e63eSJohn Marino 
1386e0b8e63eSJohn Marino 	/*
1387e0b8e63eSJohn Marino 	 * !!!
1388e0b8e63eSJohn Marino 	 * Historic 4BSD vi attempted to write the file if autowrite was set,
1389e0b8e63eSJohn Marino 	 * regardless of the writeability of the file (as defined by the file
1390e0b8e63eSJohn Marino 	 * readonly flag).  System V changed this as some point, not attempting
1391e0b8e63eSJohn Marino 	 * autowrite if the file was readonly.  This feels like a bug fix to
1392e0b8e63eSJohn Marino 	 * me (e.g. the principle of least surprise is violated if readonly is
1393e0b8e63eSJohn Marino 	 * set and vi writes the file), so I'm compatible with System V.
1394e0b8e63eSJohn Marino 	 */
1395e0b8e63eSJohn Marino 	if (O_ISSET(sp, O_READONLY)) {
1396e0b8e63eSJohn Marino 		msgq(sp, M_INFO,
1397e0b8e63eSJohn Marino 		    "266|File readonly, modifications not auto-written");
1398e0b8e63eSJohn Marino 		return (1);
1399e0b8e63eSJohn Marino 	}
1400e0b8e63eSJohn Marino 	return (file_write(sp, NULL, NULL, NULL, flags));
1401e0b8e63eSJohn Marino }
1402e0b8e63eSJohn Marino 
1403e0b8e63eSJohn Marino /*
1404e0b8e63eSJohn Marino  * set_alt_name --
1405e0b8e63eSJohn Marino  *	Set the alternate pathname.
1406e0b8e63eSJohn Marino  *
1407e0b8e63eSJohn Marino  * Set the alternate pathname.  It's a routine because I wanted some place
1408e0b8e63eSJohn Marino  * to hang this comment.  The alternate pathname (normally referenced using
1409e0b8e63eSJohn Marino  * the special character '#' during file expansion and in the vi ^^ command)
1410e0b8e63eSJohn Marino  * is set by almost all ex commands that take file names as arguments.  The
1411e0b8e63eSJohn Marino  * rules go something like this:
1412e0b8e63eSJohn Marino  *
1413e0b8e63eSJohn Marino  *    1: If any ex command takes a file name as an argument (except for the
1414e0b8e63eSJohn Marino  *	 :next command), the alternate pathname is set to that file name.
1415e0b8e63eSJohn Marino  *	 This excludes the command ":e" and ":w !command" as no file name
1416e0b8e63eSJohn Marino  *       was specified.  Note, historically, the :source command did not set
1417e0b8e63eSJohn Marino  *	 the alternate pathname.  It does in nvi, for consistency.
1418e0b8e63eSJohn Marino  *
1419e0b8e63eSJohn Marino  *    2: However, if any ex command sets the current pathname, e.g. the
1420e0b8e63eSJohn Marino  *	 ":e file" or ":rew" commands succeed, then the alternate pathname
1421e0b8e63eSJohn Marino  *	 is set to the previous file's current pathname, if it had one.
1422e0b8e63eSJohn Marino  *	 This includes the ":file" command and excludes the ":e" command.
1423e0b8e63eSJohn Marino  *	 So, by rule #1 and rule #2, if ":edit foo" fails, the alternate
1424e0b8e63eSJohn Marino  *	 pathname will be "foo", if it succeeds, the alternate pathname will
1425e0b8e63eSJohn Marino  *	 be the previous current pathname.  The ":e" command will not set
1426e0b8e63eSJohn Marino  *       the alternate or current pathnames regardless.
1427e0b8e63eSJohn Marino  *
1428e0b8e63eSJohn Marino  *    3: However, if it's a read or write command with a file argument and
1429e0b8e63eSJohn Marino  *	 the current pathname has not yet been set, the file name becomes
1430e0b8e63eSJohn Marino  *	 the current pathname, and the alternate pathname is unchanged.
1431e0b8e63eSJohn Marino  *
1432e0b8e63eSJohn Marino  * If the user edits a temporary file, there may be times when there is no
1433e0b8e63eSJohn Marino  * alternative file name.  A name argument of NULL turns it off.
1434e0b8e63eSJohn Marino  *
1435e0b8e63eSJohn Marino  * PUBLIC: void set_alt_name(SCR *, char *);
1436e0b8e63eSJohn Marino  */
1437e0b8e63eSJohn Marino void
set_alt_name(SCR * sp,char * name)1438*b1ac2ebbSDaniel Fojt set_alt_name(SCR *sp, char *name)
1439e0b8e63eSJohn Marino {
1440e0b8e63eSJohn Marino 	free(sp->alt_name);
1441e0b8e63eSJohn Marino 	if (name == NULL)
1442e0b8e63eSJohn Marino 		sp->alt_name = NULL;
1443e0b8e63eSJohn Marino 	else if ((sp->alt_name = strdup(name)) == NULL)
1444e0b8e63eSJohn Marino 		msgq(sp, M_SYSERR, NULL);
1445e0b8e63eSJohn Marino }
1446e0b8e63eSJohn Marino 
1447e0b8e63eSJohn Marino /*
1448e0b8e63eSJohn Marino  * file_lock --
1449e0b8e63eSJohn Marino  *	Get an exclusive lock on a file.
1450e0b8e63eSJohn Marino  *
1451e0b8e63eSJohn Marino  * PUBLIC: lockr_t file_lock(SCR *, char *, int, int);
1452e0b8e63eSJohn Marino  */
1453e0b8e63eSJohn Marino lockr_t
file_lock(SCR * sp,char * name,int fd,int iswrite)1454*b1ac2ebbSDaniel Fojt file_lock(SCR *sp, char *name, int fd, int iswrite)
1455e0b8e63eSJohn Marino {
1456e0b8e63eSJohn Marino 	if (!O_ISSET(sp, O_LOCKFILES))
1457e0b8e63eSJohn Marino 		return (LOCK_SUCCESS);
1458e0b8e63eSJohn Marino 
1459e0b8e63eSJohn Marino 	/*
1460e0b8e63eSJohn Marino 	 * !!!
1461e0b8e63eSJohn Marino 	 * We need to distinguish a lock not being available for the file
1462e0b8e63eSJohn Marino 	 * from the file system not supporting locking.  Flock is documented
1463e0b8e63eSJohn Marino 	 * as returning EWOULDBLOCK; add EAGAIN for good measure, and assume
1464e0b8e63eSJohn Marino 	 * they are the former.  There's no portable way to do this.
1465e0b8e63eSJohn Marino 	 */
1466e0b8e63eSJohn Marino 	errno = 0;
1467e0b8e63eSJohn Marino 	if (!flock(fd, LOCK_EX | LOCK_NB)) {
1468e0b8e63eSJohn Marino 		fcntl(fd, F_SETFD, 1);
1469e0b8e63eSJohn Marino 		return (LOCK_SUCCESS);
1470e0b8e63eSJohn Marino 	}
1471e0b8e63eSJohn Marino 	return (errno == EAGAIN
1472e0b8e63eSJohn Marino #ifdef EWOULDBLOCK
1473e0b8e63eSJohn Marino 	    || errno == EWOULDBLOCK
1474e0b8e63eSJohn Marino #endif
1475e0b8e63eSJohn Marino 	    ? LOCK_UNAVAIL : LOCK_FAILED);
1476e0b8e63eSJohn Marino }
1477