Annotation of src/usr.bin/mg/fileio.c, Revision 1.90
1.90 ! lum 1: /* $OpenBSD: fileio.c,v 1.89 2012/05/25 04:56:58 lum Exp $ */
1.50 kjell 2:
3: /* This file is in the public domain. */
1.10 niklas 4:
1.1 deraadt 5: /*
1.7 millert 6: * POSIX fileio.c
1.1 deraadt 7: */
1.49 db 8: #include "def.h"
1.56 deraadt 9:
1.82 kjell 10: #include <sys/types.h>
1.72 kjell 11: #include <sys/stat.h>
1.56 deraadt 12: #include <sys/time.h>
1.76 jason 13: #include <sys/resource.h>
1.53 kjell 14: #include <sys/wait.h>
1.72 kjell 15:
16: #include <fcntl.h>
1.33 vincent 17: #include <limits.h>
1.76 jason 18: #include <dirent.h>
1.72 kjell 19: #include <pwd.h>
1.21 vincent 20: #include <string.h>
1.9 millert 21: #include <unistd.h>
1.1 deraadt 22:
1.72 kjell 23: #include "kbd.h"
24:
1.1 deraadt 25: /*
26: * Open a file for reading.
27: */
1.7 millert 28: int
1.89 lum 29: ffropen(FILE ** ffp, const char *fn, struct buffer *bp)
1.7 millert 30: {
1.89 lum 31: if ((*ffp = fopen(fn, "r")) == NULL) {
1.46 jfb 32: if (errno == ENOENT)
33: return (FIOFNF);
34: return (FIOERR);
35: }
1.53 kjell 36:
37: /* If 'fn' is a directory open it with dired. */
1.54 kjell 38: if (fisdir(fn) == TRUE)
1.53 kjell 39: return (FIODIR);
40:
1.89 lum 41: ffstat(*ffp, bp);
1.82 kjell 42:
43: return (FIOSUC);
44: }
45:
46: /*
47: * Update stat/dirty info
48: */
49: void
1.89 lum 50: ffstat(FILE *ffp, struct buffer *bp)
1.82 kjell 51: {
52: struct stat sb;
53:
54: if (bp && fstat(fileno(ffp), &sb) == 0) {
1.7 millert 55: /* set highorder bit to make sure this isn't all zero */
1.82 kjell 56: bp->b_fi.fi_mode = sb.st_mode | 0x8000;
57: bp->b_fi.fi_uid = sb.st_uid;
58: bp->b_fi.fi_gid = sb.st_gid;
59: bp->b_fi.fi_mtime = sb.st_mtimespec;
60: /* Clear the ignore flag */
61: bp->b_flag &= ~(BFIGNDIRTY | BFDIRTY);
1.1 deraadt 62: }
1.82 kjell 63: }
1.53 kjell 64:
1.82 kjell 65: /*
66: * Update the status/dirty info. If there is an error,
67: * there's not a lot we can do.
68: */
69: int
70: fupdstat(struct buffer *bp)
71: {
1.89 lum 72: FILE *ffp;
73:
1.82 kjell 74: if ((ffp = fopen(bp->b_fname, "r")) == NULL) {
75: if (errno == ENOENT)
76: return (FIOFNF);
77: return (FIOERR);
78: }
1.89 lum 79: ffstat(ffp, bp);
80: (void)ffclose(ffp, bp);
1.1 deraadt 81: return (FIOSUC);
82: }
83:
84: /*
85: * Open a file for writing.
86: */
1.7 millert 87: int
1.89 lum 88: ffwopen(FILE ** ffp, const char *fn, struct buffer *bp)
1.7 millert 89: {
1.49 db 90: int fd;
1.66 kjell 91: mode_t fmode = DEFFILEMODE;
1.7 millert 92:
1.24 deraadt 93: if (bp && bp->b_fi.fi_mode)
1.66 kjell 94: fmode = bp->b_fi.fi_mode & 07777;
1.24 deraadt 95:
1.66 kjell 96: fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, fmode);
1.24 deraadt 97: if (fd == -1) {
98: ffp = NULL;
1.21 vincent 99: ewprintf("Cannot open file for writing : %s", strerror(errno));
1.1 deraadt 100: return (FIOERR);
1.34 deraadt 101: }
1.24 deraadt 102:
1.89 lum 103: if ((*ffp = fdopen(fd, "w")) == NULL) {
1.24 deraadt 104: ewprintf("Cannot open file for writing : %s", strerror(errno));
105: close(fd);
1.34 deraadt 106: return (FIOERR);
1.1 deraadt 107: }
1.7 millert 108:
109: /*
110: * If we have file information, use it. We don't bother to check for
111: * errors, because there's no a lot we can do about it. Certainly
1.24 deraadt 112: * trying to change ownership will fail if we aren't root. That's
1.7 millert 113: * probably OK. If we don't have info, no need to get it, since any
114: * future writes will do the same thing.
1.1 deraadt 115: */
116: if (bp && bp->b_fi.fi_mode) {
1.28 deraadt 117: fchmod(fd, bp->b_fi.fi_mode & 07777);
118: fchown(fd, bp->b_fi.fi_uid, bp->b_fi.fi_gid);
1.1 deraadt 119: }
120: return (FIOSUC);
121: }
122:
123: /*
124: * Close a file.
125: */
1.7 millert 126: /* ARGSUSED */
127: int
1.89 lum 128: ffclose(FILE *ffp, struct buffer *bp)
1.7 millert 129: {
1.85 lum 130: if (fclose(ffp) == 0)
131: return (FIOSUC);
132: return (FIOERR);
1.1 deraadt 133: }
134:
135: /*
1.41 vincent 136: * Write a buffer to the already opened file. bp points to the
1.1 deraadt 137: * buffer. Return the status.
138: */
1.7 millert 139: int
1.89 lum 140: ffputbuf(FILE *ffp, struct buffer *bp)
1.1 deraadt 141: {
1.62 deraadt 142: struct line *lp, *lpend;
1.7 millert 143:
1.77 kjell 144: lpend = bp->b_headp;
1.41 vincent 145: for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
146: if (fwrite(ltext(lp), 1, llength(lp), ffp) != llength(lp)) {
147: ewprintf("Write I/O error");
1.49 db 148: return (FIOERR);
1.7 millert 149: }
1.41 vincent 150: if (lforw(lp) != lpend) /* no implied \n on last line */
151: putc('\n', ffp);
1.48 deraadt 152: }
1.42 vincent 153: /*
154: * XXX should be variable controlled (once we have variables)
155: */
156: if (llength(lback(lpend)) != 0) {
157: if (eyorn("No newline at end of file, add one") == TRUE) {
158: lnewline_at(lback(lpend), llength(lback(lpend)));
159: putc('\n', ffp);
160: }
161: }
1.7 millert 162: return (FIOSUC);
1.1 deraadt 163: }
164:
165: /*
166: * Read a line from a file, and store the bytes
167: * in the supplied buffer. Stop on end of file or end of
168: * line. When FIOEOF is returned, there is a valid line
169: * of data without the normally implied \n.
1.80 kjell 170: * If the line length exceeds nbuf, FIOLONG is returned.
1.1 deraadt 171: */
1.7 millert 172: int
1.89 lum 173: ffgetline(FILE *ffp, char *buf, int nbuf, int *nbytes)
1.1 deraadt 174: {
1.16 mickey 175: int c, i;
1.1 deraadt 176:
177: i = 0;
1.7 millert 178: while ((c = getc(ffp)) != EOF && c != '\n') {
1.1 deraadt 179: buf[i++] = c;
1.7 millert 180: if (i >= nbuf)
1.49 db 181: return (FIOLONG);
1.1 deraadt 182: }
1.7 millert 183: if (c == EOF && ferror(ffp) != FALSE) {
1.1 deraadt 184: ewprintf("File read error");
1.49 db 185: return (FIOERR);
1.1 deraadt 186: }
187: *nbytes = i;
1.49 db 188: return (c == EOF ? FIOEOF : FIOSUC);
1.1 deraadt 189: }
190:
191: /*
1.9 millert 192: * Make a backup copy of "fname". On Unix the backup has the same
193: * name as the original file, with a "~" on the end; this seems to
194: * be newest of the new-speak. The error handling is all in "file.c".
195: * We do a copy instead of a rename since otherwise another process
196: * with an open fd will get the backup, not the new file. This is
197: * a problem when using mg with things like crontab and vipw.
1.1 deraadt 198: */
1.7 millert 199: int
1.25 vincent 200: fbackupfile(const char *fn)
1.7 millert 201: {
1.49 db 202: struct stat sb;
203: int from, to, serrno;
204: ssize_t nread;
205: char buf[BUFSIZ];
1.44 millert 206: char *nname, *tname;
1.1 deraadt 207:
1.35 vincent 208: if (stat(fn, &sb) == -1) {
209: ewprintf("Can't stat %s : %s", fn, strerror(errno));
210: return (FALSE);
211: }
212:
1.21 vincent 213: if (asprintf(&nname, "%s~", fn) == -1) {
1.90 ! lum 214: ewprintf("Can't allocate backup file name : %s", strerror(errno));
1.44 millert 215: return (ABORT);
216: }
217:
218: if (asprintf(&tname, "%s.XXXXXXXXXX", fn) == -1) {
219: ewprintf("Can't allocate temp file name : %s", strerror(errno));
220: free(nname);
1.1 deraadt 221: return (ABORT);
1.9 millert 222: }
223:
1.21 vincent 224: if ((from = open(fn, O_RDONLY)) == -1) {
225: free(nname);
1.44 millert 226: free(tname);
1.1 deraadt 227: return (FALSE);
1.21 vincent 228: }
1.44 millert 229: to = mkstemp(tname);
1.9 millert 230: if (to == -1) {
231: serrno = errno;
232: close(from);
1.21 vincent 233: free(nname);
1.44 millert 234: free(tname);
1.9 millert 235: errno = serrno;
236: return (FALSE);
237: }
238: while ((nread = read(from, buf, sizeof(buf))) > 0) {
1.70 deraadt 239: if (write(to, buf, (size_t)nread) != nread) {
1.40 vincent 240: nread = -1;
241: break;
1.9 millert 242: }
1.1 deraadt 243: }
1.9 millert 244: serrno = errno;
1.44 millert 245: (void) fchmod(to, (sb.st_mode & 0777));
1.9 millert 246: close(from);
247: close(to);
1.21 vincent 248: if (nread == -1) {
1.44 millert 249: if (unlink(tname) == -1)
1.21 vincent 250: ewprintf("Can't unlink temp : %s", strerror(errno));
1.44 millert 251: } else {
252: if (rename(tname, nname) == -1) {
253: ewprintf("Can't rename temp : %s", strerror(errno));
254: (void) unlink(tname);
1.45 henning 255: nread = -1;
1.44 millert 256: }
1.21 vincent 257: }
1.1 deraadt 258: free(nname);
1.44 millert 259: free(tname);
1.9 millert 260: errno = serrno;
1.22 deraadt 261:
1.9 millert 262: return (nread == -1 ? FALSE : TRUE);
1.1 deraadt 263: }
264:
265: /*
1.73 kjell 266: * Convert "fn" to a canonicalized absolute filename, replacing
267: * a leading ~/ with the user's home dir, following symlinks, and
1.81 martynas 268: * and remove all occurrences of /./ and /../
1.1 deraadt 269: */
1.7 millert 270: char *
1.74 jason 271: adjustname(const char *fn, int slashslash)
1.1 deraadt 272: {
1.88 lum 273: struct stat statbuf;
1.49 db 274: static char fnb[MAXPATHLEN];
1.74 jason 275: const char *cp, *ep = NULL;
1.57 kjell 276: char user[LOGIN_NAME_MAX], path[MAXPATHLEN];
277: size_t ulen, plen;
1.33 vincent 278:
279: path[0] = '\0';
1.74 jason 280:
1.75 kjell 281: if (slashslash == TRUE) {
1.74 jason 282: cp = fn + strlen(fn) - 1;
283: for (; cp >= fn; cp--) {
284: if (ep && (*cp == '/')) {
285: fn = ep;
286: break;
287: }
288: if (*cp == '/' || *cp == '~')
289: ep = cp;
290: else
291: ep = NULL;
292: }
293: }
294:
1.88 lum 295: /*
296: * Next, expand file names beginning with '~', if appropriate:
297: * 1, if ./~fn exists, continue without expanding tilde.
298: * 2, otherwise, if username 'fn' exists, expand tilde with home
299: * directory path.
300: * 3, otherwise, continue and create new buffer called ~fn.
301: */
302: if (fn[0] == '~' && stat(fn, &statbuf) != 0) {
1.33 vincent 303: struct passwd *pw;
304:
305: cp = strchr(fn, '/');
306: if (cp == NULL)
307: cp = fn + strlen(fn); /* point to the NUL byte */
1.57 kjell 308: ulen = cp - &fn[1];
309: if (ulen >= sizeof(user)) {
310: ewprintf("Login name too long");
1.33 vincent 311: return (NULL);
312: }
1.57 kjell 313: if (ulen == 0) /* ~/ or ~ */
314: (void)strlcpy(user, getlogin(), sizeof(user));
1.59 deraadt 315: else { /* ~user/ or ~user */
1.57 kjell 316: memcpy(user, &fn[1], ulen);
317: user[ulen] = '\0';
318: }
1.33 vincent 319: pw = getpwnam(user);
1.88 lum 320: if (pw != NULL) {
321: plen = strlcpy(path, pw->pw_dir, sizeof(path));
322: if (plen == 0 || path[plen - 1] != '/') {
323: if (strlcat(path, "/", sizeof(path)) >=
324: sizeof(path)) {
325: ewprintf("Path too long");
326: return (NULL);
327: }
1.57 kjell 328: }
1.88 lum 329: fn = cp;
330: if (*fn == '/')
331: fn++;
1.1 deraadt 332: }
1.7 millert 333: }
1.57 kjell 334: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
335: ewprintf("Path too long");
336: return (NULL);
337: }
1.33 vincent 338:
1.47 henning 339: if (realpath(path, fnb) == NULL)
1.57 kjell 340: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 341:
342: return (fnb);
1.1 deraadt 343: }
344:
345: /*
346: * Find a startup file for the user and return its name. As a service
347: * to other pieces of code that may want to find a startup file (like
348: * the terminal driver in particular), accepts a suffix to be appended
349: * to the startup file name.
350: */
351: char *
1.27 millert 352: startupfile(char *suffix)
1.1 deraadt 353: {
1.49 db 354: static char file[NFILEN];
1.16 mickey 355: char *home;
1.61 kjell 356: int ret;
1.12 art 357:
358: if ((home = getenv("HOME")) == NULL || *home == '\0')
359: goto nohome;
1.1 deraadt 360:
1.12 art 361: if (suffix == NULL) {
1.61 kjell 362: ret = snprintf(file, sizeof(file), "%s/.mg", home);
363: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 364: return (NULL);
1.12 art 365: } else {
1.61 kjell 366: ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
367: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 368: return (NULL);
1.1 deraadt 369: }
1.12 art 370:
371: if (access(file, R_OK) == 0)
1.32 vincent 372: return (file);
1.12 art 373: nohome:
374: #ifdef STARTUPFILE
1.27 millert 375: if (suffix == NULL) {
1.61 kjell 376: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
377: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 378: return (NULL);
1.12 art 379: } else {
1.61 kjell 380: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
381: suffix);
382: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 383: return (NULL);
1.12 art 384: }
385:
386: if (access(file, R_OK) == 0)
1.32 vincent 387: return (file);
1.61 kjell 388: #endif /* STARTUPFILE */
1.32 vincent 389: return (NULL);
1.1 deraadt 390: }
391:
1.7 millert 392: int
1.27 millert 393: copy(char *frname, char *toname)
1.1 deraadt 394: {
1.70 deraadt 395: int ifd, ofd;
1.49 db 396: char buf[BUFSIZ];
1.66 kjell 397: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 398: struct stat orig;
1.70 deraadt 399: ssize_t sr;
1.30 vincent 400:
401: if ((ifd = open(frname, O_RDONLY)) == -1)
402: return (FALSE);
403: if (fstat(ifd, &orig) == -1) {
404: ewprintf("fstat: %s", strerror(errno));
405: close(ifd);
406: return (FALSE);
407: }
408:
1.66 kjell 409: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 410: close(ifd);
411: return (FALSE);
412: }
1.70 deraadt 413: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
414: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 415: ewprintf("write error : %s", strerror(errno));
416: break;
417: }
418: }
419: if (fchmod(ofd, orig.st_mode) == -1)
420: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 421:
1.70 deraadt 422: if (sr == -1) {
1.30 vincent 423: ewprintf("Read error : %s", strerror(errno));
424: close(ifd);
425: close(ofd);
426: return (FALSE);
1.7 millert 427: }
1.30 vincent 428: /*
1.32 vincent 429: * It is "normal" for this to fail since we can't guarantee that
1.49 db 430: * we will be running as root.
1.30 vincent 431: */
432: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
433: ewprintf("Cannot set owner : %s", strerror(errno));
434:
435: (void) close(ifd);
436: (void) close(ofd);
437:
438: return (TRUE);
1.7 millert 439: }
1.1 deraadt 440:
1.7 millert 441: /*
1.1 deraadt 442: * return list of file names that match the name in buf.
443: */
1.62 deraadt 444: struct list *
1.25 vincent 445: make_file_list(char *buf)
1.7 millert 446: {
1.16 mickey 447: char *dir, *file, *cp;
1.71 kjell 448: size_t len, preflen;
449: int ret;
1.16 mickey 450: DIR *dirp;
451: struct dirent *dent;
1.68 kjell 452: struct list *last, *current;
453: char fl_name[NFILEN + 2];
1.49 db 454: char prefixx[NFILEN + 1];
1.7 millert 455:
456: /*
1.79 deraadt 457: * We need three different strings:
1.72 kjell 458:
459: * dir - the name of the directory containing what the user typed.
460: * Must be a real unix file name, e.g. no ~user, etc..
461: * Must not end in /.
462: * prefix - the portion of what the user typed that is before the
463: * names we are going to find in the directory. Must have a
464: * trailing / if the user typed it.
465: * names from the directory - We open dir, and return prefix
1.7 millert 466: * concatenated with names.
467: */
468:
469: /* first we get a directory name we can look up */
470: /*
471: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 472: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 473: * interested in names starting with ..
474: */
475: len = strlen(buf);
1.71 kjell 476: if (len && buf[len - 1] == '.') {
1.7 millert 477: buf[len - 1] = 'x';
1.74 jason 478: dir = adjustname(buf, TRUE);
1.7 millert 479: buf[len - 1] = '.';
480: } else
1.74 jason 481: dir = adjustname(buf, TRUE);
1.33 vincent 482: if (dir == NULL)
1.36 vincent 483: return (NULL);
1.7 millert 484: /*
485: * If the user typed a trailing / or the empty string
486: * he wants us to use his file spec as a directory name.
487: */
1.71 kjell 488: if (len && buf[len - 1] != '/') {
1.7 millert 489: file = strrchr(dir, '/');
490: if (file) {
1.69 kjell 491: *file = '\0';
492: if (*dir == '\0')
1.7 millert 493: dir = "/";
1.49 db 494: } else
1.7 millert 495: return (NULL);
1.1 deraadt 496: }
1.7 millert 497: /* Now we get the prefix of the name the user typed. */
1.67 kjell 498: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
499: return (NULL);
1.7 millert 500: cp = strrchr(prefixx, '/');
501: if (cp == NULL)
1.67 kjell 502: prefixx[0] = '\0';
1.7 millert 503: else
1.67 kjell 504: cp[1] = '\0';
1.7 millert 505:
506: preflen = strlen(prefixx);
1.49 db 507: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 508: cp = buf + preflen;
509: len = strlen(cp);
510:
511: /*
512: * Now make sure that file names will fit in the buffers allocated.
513: * SV files are fairly short. For BSD, something more general would
514: * be required.
515: */
1.71 kjell 516: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 517: return (NULL);
518:
519: /* loop over the specified directory, making up the list of files */
520:
521: /*
522: * Note that it is worth our time to filter out names that don't
523: * match, even though our caller is going to do so again, and to
524: * avoid doing the stat if completion is being done, because stat'ing
525: * every file in the directory is relatively expensive.
526: */
1.1 deraadt 527:
1.11 art 528: dirp = opendir(dir);
529: if (dirp == NULL)
1.7 millert 530: return (NULL);
531: last = NULL;
1.11 art 532:
533: while ((dent = readdir(dirp)) != NULL) {
1.18 art 534: int isdir;
1.83 kjell 535: if (strncmp(cp, dent->d_name, len) != 0)
1.11 art 536: continue;
1.18 art 537: isdir = 0;
538: if (dent->d_type == DT_DIR) {
539: isdir = 1;
540: } else if (dent->d_type == DT_LNK ||
541: dent->d_type == DT_UNKNOWN) {
542: struct stat statbuf;
543: char statname[NFILEN + 2];
544:
545: statbuf.st_mode = 0;
1.61 kjell 546: ret = snprintf(statname, sizeof(statname), "%s/%s",
547: dir, dent->d_name);
548: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 549: continue;
550: if (stat(statname, &statbuf) < 0)
551: continue;
1.78 otto 552: if (S_ISDIR(statbuf.st_mode))
1.18 art 553: isdir = 1;
554: }
555:
1.68 kjell 556: if ((current = malloc(sizeof(struct list))) == NULL) {
557: free_file_list(last);
1.86 lum 558: closedir(dirp);
1.68 kjell 559: return (NULL);
560: }
561: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 562: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 563: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 564: free(current);
1.7 millert 565: continue;
566: }
1.68 kjell 567: current->l_next = last;
568: current->l_name = strdup(fl_name);
569: last = current;
1.1 deraadt 570: }
1.11 art 571: closedir(dirp);
1.1 deraadt 572:
1.7 millert 573: return (last);
1.54 kjell 574: }
575:
576: /*
577: * Test if a supplied filename refers to a directory
578: * Returns ABORT on error, TRUE if directory. FALSE otherwise
579: */
580: int
581: fisdir(const char *fname)
582: {
583: struct stat statbuf;
584:
585: if (stat(fname, &statbuf) != 0)
586: return (ABORT);
587:
588: if (S_ISDIR(statbuf.st_mode))
589: return (TRUE);
590:
591: return (FALSE);
1.82 kjell 592: }
593:
594: /*
595: * Check the mtime of the supplied filename.
596: * Return TRUE if last mtime matches, FALSE if not,
597: * If the stat fails, return TRUE and try the save anyway
598: */
599: int
600: fchecktime(struct buffer *bp)
601: {
602: struct stat sb;
603:
604: if (stat(bp->b_fname, &sb) == -1)
605: return (TRUE);
606:
607: if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
608: bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
609: return (FALSE);
610:
611: return (TRUE);
612:
1.1 deraadt 613: }