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