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