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