Annotation of src/usr.bin/mg/fileio.c, Revision 1.87
1.87 ! lum 1: /* $OpenBSD: fileio.c,v 1.86 2012/03/28 17:16:53 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.49 db 25: static FILE *ffp;
26:
1.1 deraadt 27: /*
28: * Open a file for reading.
29: */
1.7 millert 30: int
1.62 deraadt 31: ffropen(const char *fn, struct buffer *bp)
1.7 millert 32: {
1.46 jfb 33: if ((ffp = fopen(fn, "r")) == NULL) {
34: if (errno == ENOENT)
35: return (FIOFNF);
36: return (FIOERR);
37: }
1.53 kjell 38:
39: /* If 'fn' is a directory open it with dired. */
1.54 kjell 40: if (fisdir(fn) == TRUE)
1.53 kjell 41: return (FIODIR);
42:
1.82 kjell 43: ffstat(bp);
44:
45: return (FIOSUC);
46: }
47:
48: /*
49: * Update stat/dirty info
50: */
51: void
52: ffstat(struct buffer *bp)
53: {
54: struct stat sb;
55:
56: if (bp && fstat(fileno(ffp), &sb) == 0) {
1.7 millert 57: /* set highorder bit to make sure this isn't all zero */
1.82 kjell 58: bp->b_fi.fi_mode = sb.st_mode | 0x8000;
59: bp->b_fi.fi_uid = sb.st_uid;
60: bp->b_fi.fi_gid = sb.st_gid;
61: bp->b_fi.fi_mtime = sb.st_mtimespec;
62: /* Clear the ignore flag */
63: bp->b_flag &= ~(BFIGNDIRTY | BFDIRTY);
1.1 deraadt 64: }
1.82 kjell 65: }
1.53 kjell 66:
1.82 kjell 67: /*
68: * Update the status/dirty info. If there is an error,
69: * there's not a lot we can do.
70: */
71: int
72: fupdstat(struct buffer *bp)
73: {
74: if ((ffp = fopen(bp->b_fname, "r")) == NULL) {
75: if (errno == ENOENT)
76: return (FIOFNF);
77: return (FIOERR);
78: }
79: ffstat(bp);
1.85 lum 80: (void)ffclose(bp);
1.1 deraadt 81: return (FIOSUC);
82: }
83:
84: /*
85: * Open a file for writing.
86: */
1.7 millert 87: int
1.62 deraadt 88: ffwopen(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:
103: if ((ffp = fdopen(fd, "w")) == NULL) {
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.62 deraadt 128: ffclose(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.62 deraadt 140: ffputbuf(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.25 vincent 173: ffgetline(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.44 millert 214: ewprintf("Can't allocate temp file name : %s", strerror(errno));
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.49 db 273: static char fnb[MAXPATHLEN];
1.74 jason 274: const char *cp, *ep = NULL;
1.57 kjell 275: char user[LOGIN_NAME_MAX], path[MAXPATHLEN];
276: size_t ulen, plen;
1.33 vincent 277:
278: path[0] = '\0';
1.74 jason 279:
1.75 kjell 280: if (slashslash == TRUE) {
1.74 jason 281: cp = fn + strlen(fn) - 1;
282: for (; cp >= fn; cp--) {
283: if (ep && (*cp == '/')) {
284: fn = ep;
285: break;
286: }
287: if (*cp == '/' || *cp == '~')
288: ep = cp;
289: else
290: ep = NULL;
291: }
292: }
293:
1.33 vincent 294: /* first handle tilde expansion */
295: if (fn[0] == '~') {
296: struct passwd *pw;
297:
298: cp = strchr(fn, '/');
299: if (cp == NULL)
300: cp = fn + strlen(fn); /* point to the NUL byte */
1.57 kjell 301: ulen = cp - &fn[1];
302: if (ulen >= sizeof(user)) {
303: ewprintf("Login name too long");
1.33 vincent 304: return (NULL);
305: }
1.57 kjell 306: if (ulen == 0) /* ~/ or ~ */
307: (void)strlcpy(user, getlogin(), sizeof(user));
1.59 deraadt 308: else { /* ~user/ or ~user */
1.57 kjell 309: memcpy(user, &fn[1], ulen);
310: user[ulen] = '\0';
311: }
1.33 vincent 312: pw = getpwnam(user);
313: if (pw == NULL) {
1.57 kjell 314: ewprintf("Unknown user %s", user);
1.33 vincent 315: return (NULL);
316: }
1.57 kjell 317: plen = strlcpy(path, pw->pw_dir, sizeof(path));
318: if (plen == 0 || path[plen - 1] != '/') {
319: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
320: ewprintf("Path too long");
321: return (NULL);
322: }
1.1 deraadt 323: }
1.33 vincent 324: fn = cp;
325: if (*fn == '/')
1.7 millert 326: fn++;
327: }
1.57 kjell 328: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
329: ewprintf("Path too long");
330: return (NULL);
331: }
1.33 vincent 332:
1.47 henning 333: if (realpath(path, fnb) == NULL)
1.57 kjell 334: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 335:
336: return (fnb);
1.1 deraadt 337: }
338:
339: /*
340: * Find a startup file for the user and return its name. As a service
341: * to other pieces of code that may want to find a startup file (like
342: * the terminal driver in particular), accepts a suffix to be appended
343: * to the startup file name.
344: */
345: char *
1.27 millert 346: startupfile(char *suffix)
1.1 deraadt 347: {
1.49 db 348: static char file[NFILEN];
1.16 mickey 349: char *home;
1.61 kjell 350: int ret;
1.12 art 351:
352: if ((home = getenv("HOME")) == NULL || *home == '\0')
353: goto nohome;
1.1 deraadt 354:
1.12 art 355: if (suffix == NULL) {
1.61 kjell 356: ret = snprintf(file, sizeof(file), "%s/.mg", home);
357: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 358: return (NULL);
1.12 art 359: } else {
1.61 kjell 360: ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
361: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 362: return (NULL);
1.1 deraadt 363: }
1.12 art 364:
365: if (access(file, R_OK) == 0)
1.32 vincent 366: return (file);
1.12 art 367: nohome:
368: #ifdef STARTUPFILE
1.27 millert 369: if (suffix == NULL) {
1.61 kjell 370: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
371: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 372: return (NULL);
1.12 art 373: } else {
1.61 kjell 374: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
375: suffix);
376: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 377: return (NULL);
1.12 art 378: }
379:
380: if (access(file, R_OK) == 0)
1.32 vincent 381: return (file);
1.61 kjell 382: #endif /* STARTUPFILE */
1.32 vincent 383: return (NULL);
1.1 deraadt 384: }
385:
1.7 millert 386: int
1.27 millert 387: copy(char *frname, char *toname)
1.1 deraadt 388: {
1.70 deraadt 389: int ifd, ofd;
1.49 db 390: char buf[BUFSIZ];
1.66 kjell 391: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 392: struct stat orig;
1.70 deraadt 393: ssize_t sr;
1.30 vincent 394:
395: if ((ifd = open(frname, O_RDONLY)) == -1)
396: return (FALSE);
397: if (fstat(ifd, &orig) == -1) {
398: ewprintf("fstat: %s", strerror(errno));
399: close(ifd);
400: return (FALSE);
401: }
402:
1.66 kjell 403: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 404: close(ifd);
405: return (FALSE);
406: }
1.70 deraadt 407: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
408: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 409: ewprintf("write error : %s", strerror(errno));
410: break;
411: }
412: }
413: if (fchmod(ofd, orig.st_mode) == -1)
414: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 415:
1.70 deraadt 416: if (sr == -1) {
1.30 vincent 417: ewprintf("Read error : %s", strerror(errno));
418: close(ifd);
419: close(ofd);
420: return (FALSE);
1.7 millert 421: }
1.30 vincent 422: /*
1.32 vincent 423: * It is "normal" for this to fail since we can't guarantee that
1.49 db 424: * we will be running as root.
1.30 vincent 425: */
426: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
427: ewprintf("Cannot set owner : %s", strerror(errno));
428:
429: (void) close(ifd);
430: (void) close(ofd);
431:
432: return (TRUE);
1.7 millert 433: }
1.1 deraadt 434:
1.7 millert 435: /*
1.1 deraadt 436: * return list of file names that match the name in buf.
437: */
1.62 deraadt 438: struct list *
1.25 vincent 439: make_file_list(char *buf)
1.7 millert 440: {
1.16 mickey 441: char *dir, *file, *cp;
1.71 kjell 442: size_t len, preflen;
443: int ret;
1.16 mickey 444: DIR *dirp;
445: struct dirent *dent;
1.68 kjell 446: struct list *last, *current;
447: char fl_name[NFILEN + 2];
1.49 db 448: char prefixx[NFILEN + 1];
1.7 millert 449:
450: /*
1.79 deraadt 451: * We need three different strings:
1.72 kjell 452:
453: * dir - the name of the directory containing what the user typed.
454: * Must be a real unix file name, e.g. no ~user, etc..
455: * Must not end in /.
456: * prefix - the portion of what the user typed that is before the
457: * names we are going to find in the directory. Must have a
458: * trailing / if the user typed it.
459: * names from the directory - We open dir, and return prefix
1.7 millert 460: * concatenated with names.
461: */
462:
463: /* first we get a directory name we can look up */
464: /*
465: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 466: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 467: * interested in names starting with ..
468: */
469: len = strlen(buf);
1.71 kjell 470: if (len && buf[len - 1] == '.') {
1.7 millert 471: buf[len - 1] = 'x';
1.74 jason 472: dir = adjustname(buf, TRUE);
1.7 millert 473: buf[len - 1] = '.';
474: } else
1.74 jason 475: dir = adjustname(buf, TRUE);
1.33 vincent 476: if (dir == NULL)
1.36 vincent 477: return (NULL);
1.7 millert 478: /*
479: * If the user typed a trailing / or the empty string
480: * he wants us to use his file spec as a directory name.
481: */
1.71 kjell 482: if (len && buf[len - 1] != '/') {
1.7 millert 483: file = strrchr(dir, '/');
484: if (file) {
1.69 kjell 485: *file = '\0';
486: if (*dir == '\0')
1.7 millert 487: dir = "/";
1.49 db 488: } else
1.7 millert 489: return (NULL);
1.1 deraadt 490: }
1.7 millert 491: /* Now we get the prefix of the name the user typed. */
1.67 kjell 492: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
493: return (NULL);
1.7 millert 494: cp = strrchr(prefixx, '/');
495: if (cp == NULL)
1.67 kjell 496: prefixx[0] = '\0';
1.7 millert 497: else
1.67 kjell 498: cp[1] = '\0';
1.7 millert 499:
500: preflen = strlen(prefixx);
1.49 db 501: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 502: cp = buf + preflen;
503: len = strlen(cp);
504:
505: /*
506: * Now make sure that file names will fit in the buffers allocated.
507: * SV files are fairly short. For BSD, something more general would
508: * be required.
509: */
1.71 kjell 510: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 511: return (NULL);
512:
513: /* loop over the specified directory, making up the list of files */
514:
515: /*
516: * Note that it is worth our time to filter out names that don't
517: * match, even though our caller is going to do so again, and to
518: * avoid doing the stat if completion is being done, because stat'ing
519: * every file in the directory is relatively expensive.
520: */
1.1 deraadt 521:
1.11 art 522: dirp = opendir(dir);
523: if (dirp == NULL)
1.7 millert 524: return (NULL);
525: last = NULL;
1.11 art 526:
527: while ((dent = readdir(dirp)) != NULL) {
1.18 art 528: int isdir;
1.83 kjell 529: if (strncmp(cp, dent->d_name, len) != 0)
1.11 art 530: continue;
1.18 art 531: isdir = 0;
532: if (dent->d_type == DT_DIR) {
533: isdir = 1;
534: } else if (dent->d_type == DT_LNK ||
535: dent->d_type == DT_UNKNOWN) {
536: struct stat statbuf;
537: char statname[NFILEN + 2];
538:
539: statbuf.st_mode = 0;
1.61 kjell 540: ret = snprintf(statname, sizeof(statname), "%s/%s",
541: dir, dent->d_name);
542: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 543: continue;
544: if (stat(statname, &statbuf) < 0)
545: continue;
1.78 otto 546: if (S_ISDIR(statbuf.st_mode))
1.18 art 547: isdir = 1;
548: }
549:
1.68 kjell 550: if ((current = malloc(sizeof(struct list))) == NULL) {
551: free_file_list(last);
1.86 lum 552: closedir(dirp);
1.68 kjell 553: return (NULL);
554: }
555: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 556: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 557: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 558: free(current);
1.7 millert 559: continue;
560: }
1.68 kjell 561: current->l_next = last;
562: current->l_name = strdup(fl_name);
563: last = current;
1.1 deraadt 564: }
1.11 art 565: closedir(dirp);
1.1 deraadt 566:
1.7 millert 567: return (last);
1.54 kjell 568: }
569:
570: /*
571: * Test if a supplied filename refers to a directory
572: * Returns ABORT on error, TRUE if directory. FALSE otherwise
573: */
574: int
575: fisdir(const char *fname)
576: {
577: struct stat statbuf;
578:
579: if (stat(fname, &statbuf) != 0)
580: return (ABORT);
581:
582: if (S_ISDIR(statbuf.st_mode))
583: return (TRUE);
584:
585: return (FALSE);
1.82 kjell 586: }
587:
588: /*
589: * Check the mtime of the supplied filename.
590: * Return TRUE if last mtime matches, FALSE if not,
591: * If the stat fails, return TRUE and try the save anyway
592: */
593: int
594: fchecktime(struct buffer *bp)
595: {
596: struct stat sb;
597:
598: if (stat(bp->b_fname, &sb) == -1)
599: return (TRUE);
600:
601: if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
602: bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
603: return (FALSE);
604:
605: return (TRUE);
606:
1.1 deraadt 607: }