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