Annotation of src/usr.bin/mg/fileio.c, Revision 1.74
1.74 ! jason 1: /* $OpenBSD: fileio.c,v 1.73 2006/05/03 22:25:34 kjell 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.74 ! jason 242: adjustname(const char *fn, int slashslash)
1.1 deraadt 243: {
1.49 db 244: static char fnb[MAXPATHLEN];
1.74 ! jason 245: const char *cp, *ep = NULL;
1.57 kjell 246: char user[LOGIN_NAME_MAX], path[MAXPATHLEN];
247: size_t ulen, plen;
1.33 vincent 248:
249: path[0] = '\0';
1.74 ! jason 250:
! 251: if (slashslash) {
! 252: cp = fn + strlen(fn) - 1;
! 253: for (; cp >= fn; cp--) {
! 254: if (ep && (*cp == '/')) {
! 255: fn = ep;
! 256: break;
! 257: }
! 258: if (*cp == '/' || *cp == '~')
! 259: ep = cp;
! 260: else
! 261: ep = NULL;
! 262: }
! 263: }
! 264:
1.33 vincent 265: /* first handle tilde expansion */
266: if (fn[0] == '~') {
267: struct passwd *pw;
268:
269: cp = strchr(fn, '/');
270: if (cp == NULL)
271: cp = fn + strlen(fn); /* point to the NUL byte */
1.57 kjell 272: ulen = cp - &fn[1];
273: if (ulen >= sizeof(user)) {
274: ewprintf("Login name too long");
1.33 vincent 275: return (NULL);
276: }
1.57 kjell 277: if (ulen == 0) /* ~/ or ~ */
278: (void)strlcpy(user, getlogin(), sizeof(user));
1.59 deraadt 279: else { /* ~user/ or ~user */
1.57 kjell 280: memcpy(user, &fn[1], ulen);
281: user[ulen] = '\0';
282: }
1.33 vincent 283: pw = getpwnam(user);
284: if (pw == NULL) {
1.57 kjell 285: ewprintf("Unknown user %s", user);
1.33 vincent 286: return (NULL);
287: }
1.57 kjell 288: plen = strlcpy(path, pw->pw_dir, sizeof(path));
289: if (plen == 0 || path[plen - 1] != '/') {
290: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
291: ewprintf("Path too long");
292: return (NULL);
293: }
1.1 deraadt 294: }
1.33 vincent 295: fn = cp;
296: if (*fn == '/')
1.7 millert 297: fn++;
298: }
1.57 kjell 299: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
300: ewprintf("Path too long");
301: return (NULL);
302: }
1.33 vincent 303:
1.47 henning 304: if (realpath(path, fnb) == NULL)
1.57 kjell 305: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 306:
307: return (fnb);
1.1 deraadt 308: }
309:
310: #ifndef NO_STARTUP
311: /*
312: * Find a startup file for the user and return its name. As a service
313: * to other pieces of code that may want to find a startup file (like
314: * the terminal driver in particular), accepts a suffix to be appended
315: * to the startup file name.
316: */
317: char *
1.27 millert 318: startupfile(char *suffix)
1.1 deraadt 319: {
1.49 db 320: static char file[NFILEN];
1.16 mickey 321: char *home;
1.61 kjell 322: int ret;
1.12 art 323:
324: if ((home = getenv("HOME")) == NULL || *home == '\0')
325: goto nohome;
1.1 deraadt 326:
1.12 art 327: if (suffix == NULL) {
1.61 kjell 328: ret = snprintf(file, sizeof(file), "%s/.mg", home);
329: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 330: return (NULL);
1.12 art 331: } else {
1.61 kjell 332: ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
333: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 334: return (NULL);
1.1 deraadt 335: }
1.12 art 336:
337: if (access(file, R_OK) == 0)
1.32 vincent 338: return (file);
1.12 art 339: nohome:
340: #ifdef STARTUPFILE
1.27 millert 341: if (suffix == NULL) {
1.61 kjell 342: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
343: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 344: return (NULL);
1.12 art 345: } else {
1.61 kjell 346: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
347: suffix);
348: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 349: return (NULL);
1.12 art 350: }
351:
352: if (access(file, R_OK) == 0)
1.32 vincent 353: return (file);
1.61 kjell 354: #endif /* STARTUPFILE */
1.32 vincent 355: return (NULL);
1.1 deraadt 356: }
1.61 kjell 357: #endif /* !NO_STARTUP */
1.1 deraadt 358:
1.7 millert 359: int
1.27 millert 360: copy(char *frname, char *toname)
1.1 deraadt 361: {
1.70 deraadt 362: int ifd, ofd;
1.49 db 363: char buf[BUFSIZ];
1.66 kjell 364: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 365: struct stat orig;
1.70 deraadt 366: ssize_t sr;
1.30 vincent 367:
368: if ((ifd = open(frname, O_RDONLY)) == -1)
369: return (FALSE);
370: if (fstat(ifd, &orig) == -1) {
371: ewprintf("fstat: %s", strerror(errno));
372: close(ifd);
373: return (FALSE);
374: }
375:
1.66 kjell 376: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 377: close(ifd);
378: return (FALSE);
379: }
1.70 deraadt 380: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
381: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 382: ewprintf("write error : %s", strerror(errno));
383: break;
384: }
385: }
386: if (fchmod(ofd, orig.st_mode) == -1)
387: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 388:
1.70 deraadt 389: if (sr == -1) {
1.30 vincent 390: ewprintf("Read error : %s", strerror(errno));
391: close(ifd);
392: close(ofd);
393: return (FALSE);
1.7 millert 394: }
1.30 vincent 395: /*
1.32 vincent 396: * It is "normal" for this to fail since we can't guarantee that
1.49 db 397: * we will be running as root.
1.30 vincent 398: */
399: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
400: ewprintf("Cannot set owner : %s", strerror(errno));
401:
402: (void) close(ifd);
403: (void) close(ofd);
404:
405: return (TRUE);
1.7 millert 406: }
1.1 deraadt 407:
1.7 millert 408: /*
1.1 deraadt 409: * return list of file names that match the name in buf.
410: */
1.62 deraadt 411: struct list *
1.25 vincent 412: make_file_list(char *buf)
1.7 millert 413: {
1.16 mickey 414: char *dir, *file, *cp;
1.71 kjell 415: size_t len, preflen;
416: int ret;
1.16 mickey 417: DIR *dirp;
418: struct dirent *dent;
1.68 kjell 419: struct list *last, *current;
420: char fl_name[NFILEN + 2];
1.49 db 421: char prefixx[NFILEN + 1];
1.7 millert 422:
423: /*
1.72 kjell 424: * We need three different strings:
425:
426: * dir - the name of the directory containing what the user typed.
427: * Must be a real unix file name, e.g. no ~user, etc..
428: * Must not end in /.
429: * prefix - the portion of what the user typed that is before the
430: * names we are going to find in the directory. Must have a
431: * trailing / if the user typed it.
432: * names from the directory - We open dir, and return prefix
1.7 millert 433: * concatenated with names.
434: */
435:
436: /* first we get a directory name we can look up */
437: /*
438: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 439: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 440: * interested in names starting with ..
441: */
442: len = strlen(buf);
1.71 kjell 443: if (len && buf[len - 1] == '.') {
1.7 millert 444: buf[len - 1] = 'x';
1.74 ! jason 445: dir = adjustname(buf, TRUE);
1.7 millert 446: buf[len - 1] = '.';
447: } else
1.74 ! jason 448: dir = adjustname(buf, TRUE);
1.33 vincent 449: if (dir == NULL)
1.36 vincent 450: return (NULL);
1.7 millert 451: /*
452: * If the user typed a trailing / or the empty string
453: * he wants us to use his file spec as a directory name.
454: */
1.71 kjell 455: if (len && buf[len - 1] != '/') {
1.7 millert 456: file = strrchr(dir, '/');
457: if (file) {
1.69 kjell 458: *file = '\0';
459: if (*dir == '\0')
1.7 millert 460: dir = "/";
1.49 db 461: } else
1.7 millert 462: return (NULL);
1.1 deraadt 463: }
1.7 millert 464: /* Now we get the prefix of the name the user typed. */
1.67 kjell 465: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
466: return (NULL);
1.7 millert 467: cp = strrchr(prefixx, '/');
468: if (cp == NULL)
1.67 kjell 469: prefixx[0] = '\0';
1.7 millert 470: else
1.67 kjell 471: cp[1] = '\0';
1.7 millert 472:
473: preflen = strlen(prefixx);
1.49 db 474: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 475: cp = buf + preflen;
476: len = strlen(cp);
477:
478: /*
479: * Now make sure that file names will fit in the buffers allocated.
480: * SV files are fairly short. For BSD, something more general would
481: * be required.
482: */
1.71 kjell 483: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 484: return (NULL);
485:
486: /* loop over the specified directory, making up the list of files */
487:
488: /*
489: * Note that it is worth our time to filter out names that don't
490: * match, even though our caller is going to do so again, and to
491: * avoid doing the stat if completion is being done, because stat'ing
492: * every file in the directory is relatively expensive.
493: */
1.1 deraadt 494:
1.11 art 495: dirp = opendir(dir);
496: if (dirp == NULL)
1.7 millert 497: return (NULL);
498: last = NULL;
1.11 art 499:
500: while ((dent = readdir(dirp)) != NULL) {
1.18 art 501: int isdir;
502:
1.11 art 503: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
504: continue;
505:
1.18 art 506: isdir = 0;
507: if (dent->d_type == DT_DIR) {
508: isdir = 1;
509: } else if (dent->d_type == DT_LNK ||
510: dent->d_type == DT_UNKNOWN) {
511: struct stat statbuf;
512: char statname[NFILEN + 2];
513:
514: statbuf.st_mode = 0;
1.61 kjell 515: ret = snprintf(statname, sizeof(statname), "%s/%s",
516: dir, dent->d_name);
517: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 518: continue;
519: if (stat(statname, &statbuf) < 0)
520: continue;
521: if (statbuf.st_mode & S_IFDIR)
522: isdir = 1;
523: }
524:
1.68 kjell 525: if ((current = malloc(sizeof(struct list))) == NULL) {
526: free_file_list(last);
527: return (NULL);
528: }
529: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 530: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 531: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 532: free(current);
1.7 millert 533: continue;
534: }
1.68 kjell 535: current->l_next = last;
536: current->l_name = strdup(fl_name);
537: last = current;
1.1 deraadt 538: }
1.11 art 539: closedir(dirp);
1.1 deraadt 540:
1.7 millert 541: return (last);
1.54 kjell 542: }
543:
544: /*
545: * Test if a supplied filename refers to a directory
546: * Returns ABORT on error, TRUE if directory. FALSE otherwise
547: */
548: int
549: fisdir(const char *fname)
550: {
551: struct stat statbuf;
552:
553: if (stat(fname, &statbuf) != 0)
554: return (ABORT);
555:
556: if (S_ISDIR(statbuf.st_mode))
557: return (TRUE);
558:
559: return (FALSE);
1.1 deraadt 560: }