Annotation of src/usr.bin/mg/fileio.c, Revision 1.73
1.71 kjell 1: /* $OpenBSD: fileio.c,v 1.70 2006/04/03 00:40:56 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/dir.h>
12: #include <sys/resource.h>
13: #include <sys/stat.h>
1.56 deraadt 14: #include <sys/types.h>
15: #include <sys/time.h>
1.53 kjell 16: #include <sys/wait.h>
1.72 kjell 17:
18: #include <fcntl.h>
1.33 vincent 19: #include <limits.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:
116: lpend = bp->b_linep;
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.
142: */
1.7 millert 143: int
1.25 vincent 144: ffgetline(char *buf, int nbuf, int *nbytes)
1.1 deraadt 145: {
1.16 mickey 146: int c, i;
1.1 deraadt 147:
148: i = 0;
1.7 millert 149: while ((c = getc(ffp)) != EOF && c != '\n') {
1.1 deraadt 150: buf[i++] = c;
1.7 millert 151: if (i >= nbuf)
1.49 db 152: return (FIOLONG);
1.1 deraadt 153: }
1.7 millert 154: if (c == EOF && ferror(ffp) != FALSE) {
1.1 deraadt 155: ewprintf("File read error");
1.49 db 156: return (FIOERR);
1.1 deraadt 157: }
158: *nbytes = i;
1.49 db 159: return (c == EOF ? FIOEOF : FIOSUC);
1.1 deraadt 160: }
161:
162: /*
1.9 millert 163: * Make a backup copy of "fname". On Unix the backup has the same
164: * name as the original file, with a "~" on the end; this seems to
165: * be newest of the new-speak. The error handling is all in "file.c".
166: * We do a copy instead of a rename since otherwise another process
167: * with an open fd will get the backup, not the new file. This is
168: * a problem when using mg with things like crontab and vipw.
1.1 deraadt 169: */
1.7 millert 170: int
1.25 vincent 171: fbackupfile(const char *fn)
1.7 millert 172: {
1.49 db 173: struct stat sb;
174: int from, to, serrno;
175: ssize_t nread;
176: char buf[BUFSIZ];
1.44 millert 177: char *nname, *tname;
1.1 deraadt 178:
1.35 vincent 179: if (stat(fn, &sb) == -1) {
180: ewprintf("Can't stat %s : %s", fn, strerror(errno));
181: return (FALSE);
182: }
183:
1.21 vincent 184: if (asprintf(&nname, "%s~", fn) == -1) {
1.44 millert 185: ewprintf("Can't allocate temp file name : %s", strerror(errno));
186: return (ABORT);
187: }
188:
189: if (asprintf(&tname, "%s.XXXXXXXXXX", fn) == -1) {
190: ewprintf("Can't allocate temp file name : %s", strerror(errno));
191: free(nname);
1.1 deraadt 192: return (ABORT);
1.9 millert 193: }
194:
1.21 vincent 195: if ((from = open(fn, O_RDONLY)) == -1) {
196: free(nname);
1.44 millert 197: free(tname);
1.1 deraadt 198: return (FALSE);
1.21 vincent 199: }
1.44 millert 200: to = mkstemp(tname);
1.9 millert 201: if (to == -1) {
202: serrno = errno;
203: close(from);
1.21 vincent 204: free(nname);
1.44 millert 205: free(tname);
1.9 millert 206: errno = serrno;
207: return (FALSE);
208: }
209: while ((nread = read(from, buf, sizeof(buf))) > 0) {
1.70 deraadt 210: if (write(to, buf, (size_t)nread) != nread) {
1.40 vincent 211: nread = -1;
212: break;
1.9 millert 213: }
1.1 deraadt 214: }
1.9 millert 215: serrno = errno;
1.44 millert 216: (void) fchmod(to, (sb.st_mode & 0777));
1.9 millert 217: close(from);
218: close(to);
1.21 vincent 219: if (nread == -1) {
1.44 millert 220: if (unlink(tname) == -1)
1.21 vincent 221: ewprintf("Can't unlink temp : %s", strerror(errno));
1.44 millert 222: } else {
223: if (rename(tname, nname) == -1) {
224: ewprintf("Can't rename temp : %s", strerror(errno));
225: (void) unlink(tname);
1.45 henning 226: nread = -1;
1.44 millert 227: }
1.21 vincent 228: }
1.1 deraadt 229: free(nname);
1.44 millert 230: free(tname);
1.9 millert 231: errno = serrno;
1.22 deraadt 232:
1.9 millert 233: return (nread == -1 ? FALSE : TRUE);
1.1 deraadt 234: }
235:
236: /*
1.73 ! kjell 237: * Convert "fn" to a canonicalized absolute filename, replacing
! 238: * a leading ~/ with the user's home dir, following symlinks, and
! 239: * and remove all occurences of /./ and /../
1.1 deraadt 240: */
1.7 millert 241: char *
1.25 vincent 242: adjustname(const char *fn)
1.1 deraadt 243: {
1.49 db 244: static char fnb[MAXPATHLEN];
1.60 kjell 245: const char *cp;
1.57 kjell 246: char user[LOGIN_NAME_MAX], path[MAXPATHLEN];
247: size_t ulen, plen;
1.33 vincent 248:
249: path[0] = '\0';
250: /* first handle tilde expansion */
251: if (fn[0] == '~') {
252: struct passwd *pw;
253:
254: cp = strchr(fn, '/');
255: if (cp == NULL)
256: cp = fn + strlen(fn); /* point to the NUL byte */
1.57 kjell 257: ulen = cp - &fn[1];
258: if (ulen >= sizeof(user)) {
259: ewprintf("Login name too long");
1.33 vincent 260: return (NULL);
261: }
1.57 kjell 262: if (ulen == 0) /* ~/ or ~ */
263: (void)strlcpy(user, getlogin(), sizeof(user));
1.59 deraadt 264: else { /* ~user/ or ~user */
1.57 kjell 265: memcpy(user, &fn[1], ulen);
266: user[ulen] = '\0';
267: }
1.33 vincent 268: pw = getpwnam(user);
269: if (pw == NULL) {
1.57 kjell 270: ewprintf("Unknown user %s", user);
1.33 vincent 271: return (NULL);
272: }
1.57 kjell 273: plen = strlcpy(path, pw->pw_dir, sizeof(path));
274: if (plen == 0 || path[plen - 1] != '/') {
275: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
276: ewprintf("Path too long");
277: return (NULL);
278: }
1.1 deraadt 279: }
1.33 vincent 280: fn = cp;
281: if (*fn == '/')
1.7 millert 282: fn++;
283: }
1.57 kjell 284: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
285: ewprintf("Path too long");
286: return (NULL);
287: }
1.33 vincent 288:
1.47 henning 289: if (realpath(path, fnb) == NULL)
1.57 kjell 290: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 291:
292: return (fnb);
1.1 deraadt 293: }
294:
295: #ifndef NO_STARTUP
296: /*
297: * Find a startup file for the user and return its name. As a service
298: * to other pieces of code that may want to find a startup file (like
299: * the terminal driver in particular), accepts a suffix to be appended
300: * to the startup file name.
301: */
302: char *
1.27 millert 303: startupfile(char *suffix)
1.1 deraadt 304: {
1.49 db 305: static char file[NFILEN];
1.16 mickey 306: char *home;
1.61 kjell 307: int ret;
1.12 art 308:
309: if ((home = getenv("HOME")) == NULL || *home == '\0')
310: goto nohome;
1.1 deraadt 311:
1.12 art 312: if (suffix == NULL) {
1.61 kjell 313: ret = snprintf(file, sizeof(file), "%s/.mg", home);
314: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 315: return (NULL);
1.12 art 316: } else {
1.61 kjell 317: ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
318: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 319: return (NULL);
1.1 deraadt 320: }
1.12 art 321:
322: if (access(file, R_OK) == 0)
1.32 vincent 323: return (file);
1.12 art 324: nohome:
325: #ifdef STARTUPFILE
1.27 millert 326: if (suffix == NULL) {
1.61 kjell 327: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
328: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 329: return (NULL);
1.12 art 330: } else {
1.61 kjell 331: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
332: suffix);
333: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 334: return (NULL);
1.12 art 335: }
336:
337: if (access(file, R_OK) == 0)
1.32 vincent 338: return (file);
1.61 kjell 339: #endif /* STARTUPFILE */
1.32 vincent 340: return (NULL);
1.1 deraadt 341: }
1.61 kjell 342: #endif /* !NO_STARTUP */
1.1 deraadt 343:
1.7 millert 344: int
1.27 millert 345: copy(char *frname, char *toname)
1.1 deraadt 346: {
1.70 deraadt 347: int ifd, ofd;
1.49 db 348: char buf[BUFSIZ];
1.66 kjell 349: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 350: struct stat orig;
1.70 deraadt 351: ssize_t sr;
1.30 vincent 352:
353: if ((ifd = open(frname, O_RDONLY)) == -1)
354: return (FALSE);
355: if (fstat(ifd, &orig) == -1) {
356: ewprintf("fstat: %s", strerror(errno));
357: close(ifd);
358: return (FALSE);
359: }
360:
1.66 kjell 361: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 362: close(ifd);
363: return (FALSE);
364: }
1.70 deraadt 365: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
366: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 367: ewprintf("write error : %s", strerror(errno));
368: break;
369: }
370: }
371: if (fchmod(ofd, orig.st_mode) == -1)
372: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 373:
1.70 deraadt 374: if (sr == -1) {
1.30 vincent 375: ewprintf("Read error : %s", strerror(errno));
376: close(ifd);
377: close(ofd);
378: return (FALSE);
1.7 millert 379: }
1.30 vincent 380: /*
1.32 vincent 381: * It is "normal" for this to fail since we can't guarantee that
1.49 db 382: * we will be running as root.
1.30 vincent 383: */
384: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
385: ewprintf("Cannot set owner : %s", strerror(errno));
386:
387: (void) close(ifd);
388: (void) close(ofd);
389:
390: return (TRUE);
1.7 millert 391: }
1.1 deraadt 392:
1.7 millert 393: /*
1.1 deraadt 394: * return list of file names that match the name in buf.
395: */
1.62 deraadt 396: struct list *
1.25 vincent 397: make_file_list(char *buf)
1.7 millert 398: {
1.16 mickey 399: char *dir, *file, *cp;
1.71 kjell 400: size_t len, preflen;
401: int ret;
1.16 mickey 402: DIR *dirp;
403: struct dirent *dent;
1.68 kjell 404: struct list *last, *current;
405: char fl_name[NFILEN + 2];
1.49 db 406: char prefixx[NFILEN + 1];
1.7 millert 407:
408: /*
1.72 kjell 409: * We need three different strings:
410:
411: * dir - the name of the directory containing what the user typed.
412: * Must be a real unix file name, e.g. no ~user, etc..
413: * Must not end in /.
414: * prefix - the portion of what the user typed that is before the
415: * names we are going to find in the directory. Must have a
416: * trailing / if the user typed it.
417: * names from the directory - We open dir, and return prefix
1.7 millert 418: * concatenated with names.
419: */
420:
421: /* first we get a directory name we can look up */
422: /*
423: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 424: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 425: * interested in names starting with ..
426: */
427: len = strlen(buf);
1.71 kjell 428: if (len && buf[len - 1] == '.') {
1.7 millert 429: buf[len - 1] = 'x';
430: dir = adjustname(buf);
431: buf[len - 1] = '.';
432: } else
433: dir = adjustname(buf);
1.33 vincent 434: if (dir == NULL)
1.36 vincent 435: return (NULL);
1.7 millert 436: /*
437: * If the user typed a trailing / or the empty string
438: * he wants us to use his file spec as a directory name.
439: */
1.71 kjell 440: if (len && buf[len - 1] != '/') {
1.7 millert 441: file = strrchr(dir, '/');
442: if (file) {
1.69 kjell 443: *file = '\0';
444: if (*dir == '\0')
1.7 millert 445: dir = "/";
1.49 db 446: } else
1.7 millert 447: return (NULL);
1.1 deraadt 448: }
1.7 millert 449: /* Now we get the prefix of the name the user typed. */
1.67 kjell 450: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
451: return (NULL);
1.7 millert 452: cp = strrchr(prefixx, '/');
453: if (cp == NULL)
1.67 kjell 454: prefixx[0] = '\0';
1.7 millert 455: else
1.67 kjell 456: cp[1] = '\0';
1.7 millert 457:
458: preflen = strlen(prefixx);
1.49 db 459: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 460: cp = buf + preflen;
461: len = strlen(cp);
462:
463: /*
464: * Now make sure that file names will fit in the buffers allocated.
465: * SV files are fairly short. For BSD, something more general would
466: * be required.
467: */
1.71 kjell 468: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 469: return (NULL);
470:
471: /* loop over the specified directory, making up the list of files */
472:
473: /*
474: * Note that it is worth our time to filter out names that don't
475: * match, even though our caller is going to do so again, and to
476: * avoid doing the stat if completion is being done, because stat'ing
477: * every file in the directory is relatively expensive.
478: */
1.1 deraadt 479:
1.11 art 480: dirp = opendir(dir);
481: if (dirp == NULL)
1.7 millert 482: return (NULL);
483: last = NULL;
1.11 art 484:
485: while ((dent = readdir(dirp)) != NULL) {
1.18 art 486: int isdir;
487:
1.11 art 488: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
489: continue;
490:
1.18 art 491: isdir = 0;
492: if (dent->d_type == DT_DIR) {
493: isdir = 1;
494: } else if (dent->d_type == DT_LNK ||
495: dent->d_type == DT_UNKNOWN) {
496: struct stat statbuf;
497: char statname[NFILEN + 2];
498:
499: statbuf.st_mode = 0;
1.61 kjell 500: ret = snprintf(statname, sizeof(statname), "%s/%s",
501: dir, dent->d_name);
502: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 503: continue;
504: if (stat(statname, &statbuf) < 0)
505: continue;
506: if (statbuf.st_mode & S_IFDIR)
507: isdir = 1;
508: }
509:
1.68 kjell 510: if ((current = malloc(sizeof(struct list))) == NULL) {
511: free_file_list(last);
512: return (NULL);
513: }
514: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 515: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 516: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 517: free(current);
1.7 millert 518: continue;
519: }
1.68 kjell 520: current->l_next = last;
521: current->l_name = strdup(fl_name);
522: last = current;
1.1 deraadt 523: }
1.11 art 524: closedir(dirp);
1.1 deraadt 525:
1.7 millert 526: return (last);
1.54 kjell 527: }
528:
529: /*
530: * Test if a supplied filename refers to a directory
531: * Returns ABORT on error, TRUE if directory. FALSE otherwise
532: */
533: int
534: fisdir(const char *fname)
535: {
536: struct stat statbuf;
537:
538: if (stat(fname, &statbuf) != 0)
539: return (ABORT);
540:
541: if (S_ISDIR(statbuf.st_mode))
542: return (TRUE);
543:
544: return (FALSE);
1.1 deraadt 545: }