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