Annotation of src/usr.bin/mg/fileio.c, Revision 1.25
1.25 ! vincent 1: /* $OpenBSD: fileio.c,v 1.24 2002/02/22 00:18:37 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) {
71: chmod(fn, bp->b_fi.fi_mode & 07777);
72: chown(fn, bp->b_fi.fi_uid, bp->b_fi.fi_gid);
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.9 millert 177: return (FALSE);
178: }
179:
1.21 vincent 180: if ((from = open(fn, O_RDONLY)) == -1) {
181: free(nname);
1.1 deraadt 182: return (FALSE);
1.21 vincent 183: }
1.9 millert 184: to = open(nname, O_WRONLY|O_CREAT|O_TRUNC, (sb.st_mode & 0777));
185: if (to == -1) {
186: serrno = errno;
187: close(from);
1.21 vincent 188: free(nname);
1.9 millert 189: errno = serrno;
190: return (FALSE);
191: }
192: while ((nread = read(from, buf, sizeof(buf))) > 0) {
193: if (write(to, buf, nread) != nread) {
194: nread = -1;
195: break;
196: }
1.1 deraadt 197: }
1.9 millert 198: serrno = errno;
199: close(from);
200: close(to);
1.21 vincent 201: if (nread == -1) {
202: if (unlink(nname) == -1)
203: ewprintf("Can't unlink temp : %s", strerror(errno));
204: }
1.1 deraadt 205: free(nname);
1.9 millert 206: errno = serrno;
1.22 deraadt 207:
1.9 millert 208: return (nread == -1 ? FALSE : TRUE);
1.1 deraadt 209: }
210: #endif
211:
212: /*
213: * The string "fn" is a file name.
214: * Perform any required appending of directory name or case adjustments.
215: * If NO_DIR is not defined, the same file should be refered to even if the
216: * working directory changes.
217: */
218: #ifdef SYMBLINK
219: #include <sys/types.h>
220: #include <sys/stat.h>
221: #ifndef MAXLINK
222: #define MAXLINK 8 /* maximum symbolic links to follow */
223: #endif
224: #endif
225: #include <pwd.h>
226: #ifndef NO_DIR
1.16 mickey 227: extern char *wdir;
1.1 deraadt 228: #endif
229:
1.7 millert 230: char *
1.25 ! vincent 231: adjustname(const char *fn)
1.1 deraadt 232: {
1.16 mickey 233: char *cp;
234: static char fnb[NFILEN];
235: struct passwd *pwent;
1.1 deraadt 236: #ifdef SYMBLINK
1.16 mickey 237: struct stat statbuf;
238: int i, j;
239: char linkbuf[NFILEN];
1.1 deraadt 240: #endif
241:
1.7 millert 242: switch (*fn) {
243: case '/':
244: cp = fnb;
245: *cp++ = *fn++;
246: break;
1.1 deraadt 247: case '~':
1.7 millert 248: fn++;
1.8 millert 249: cp = getenv("HOME");
250: if (cp != NULL && *cp != '\0' && (*fn == '/' || *fn == '\0')) {
1.15 mickey 251: cp = fnb + strlcpy(fnb, cp, sizeof(fnb));
1.7 millert 252: if (*fn)
253: fn++;
254: break;
1.1 deraadt 255: } else {
1.7 millert 256: cp = fnb;
257: while (*fn && *fn != '/')
258: *cp++ = *fn++;
259: *cp = '\0';
260: if ((pwent = getpwnam(fnb)) != NULL) {
1.15 mickey 261: cp = fnb + strlcpy(fnb, pwent->pw_dir, sizeof(fnb));
1.7 millert 262: break;
263: } else {
264: fn -= strlen(fnb) + 1;
265: /* can't find ~user, continue to default case */
266: }
1.1 deraadt 267: }
268: default:
269: #ifndef NODIR
1.15 mickey 270: cp = fnb + strlcpy(fnb, wdir, sizeof(fnb));
1.7 millert 271: break;
1.1 deraadt 272: #else
1.7 millert 273: return fn; /* punt */
1.1 deraadt 274: #endif
1.7 millert 275: }
276: if (cp != fnb && cp[-1] != '/')
277: *cp++ = '/';
278: while (*fn) {
279: switch (*fn) {
280: case '.':
281: switch (fn[1]) {
282: case '\0':
283: *--cp = '\0';
284: return fnb;
285: case '/':
286: fn += 2;
287: continue;
288: case '.':
289: if (fn[2] != '/' && fn[2] != '\0')
290: break;
1.1 deraadt 291: #ifdef SYMBLINK
1.7 millert 292: cp[-1] = '\0';
293: for (j = MAXLINK; j-- &&
294: lstat(fnb, &statbuf) != -1 &&
295: (statbuf.st_mode & S_IFMT) == S_IFLNK &&
296: (i = readlink(fnb, linkbuf, sizeof linkbuf))
297: != -1;) {
298: if (linkbuf[0] != '/') {
299: --cp;
300: while (cp > fnb && *--cp != '/') {
301: }
302: ++cp;
1.13 art 303: (void) strncpy(cp, linkbuf, i);
1.7 millert 304: cp += i;
305: } else {
1.13 art 306: (void) strncpy(fnb, linkbuf, i);
1.7 millert 307: cp = fnb + i;
308: }
309: if (cp[-1] != '/')
310: *cp++ = '\0';
311: else
312: cp[-1] = '\0';
1.1 deraadt 313: }
1.7 millert 314: cp[-1] = '/';
1.1 deraadt 315: #endif
1.7 millert 316: --cp;
1.21 vincent 317: while (cp > fnb && *--cp != '/')
318: ;
1.7 millert 319: ++cp;
320: if (fn[2] == '\0') {
321: *--cp = '\0';
322: return fnb;
323: }
324: fn += 3;
325: continue;
326: default:
327: break;
328: }
329: break;
330: case '/':
331: fn++;
332: continue;
333: default:
334: break;
335: }
336: while (*fn && (*cp++ = *fn++) != '/') {
337: }
338: }
339: if (cp[-1] == '/')
340: --cp;
341: *cp = '\0';
342: return fnb;
1.1 deraadt 343: }
344:
345: #ifndef NO_STARTUP
346: /*
347: * Find a startup file for the user and return its name. As a service
348: * to other pieces of code that may want to find a startup file (like
349: * the terminal driver in particular), accepts a suffix to be appended
350: * to the startup file name.
351: */
352: char *
353: startupfile(suffix)
1.16 mickey 354: 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
376: if (suffix == NULL)
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.1 deraadt 398: copy(frname, toname)
1.16 mickey 399: char *frname;
400: char *toname;
1.1 deraadt 401: {
1.16 mickey 402: pid_t pid;
403: int status;
1.1 deraadt 404:
1.20 wilfried 405: switch ((pid = vfork())) {
406: case -1:
407: return -1;
408: case 0:
1.19 deraadt 409: execl("/bin/cp", "cp", frname, toname, (char *)NULL);
1.7 millert 410: _exit(1); /* shouldn't happen */
1.20 wilfried 411: default:
412: waitpid(pid, &status, 0);
413: return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
1.7 millert 414: }
415: }
416:
417: BUFFER *
418: dired_(dirname)
1.16 mickey 419: 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.15 mickey 442: if (snprintf(line, sizeof(line), "ls -al %s", dirname) >= sizeof(line)){
443: ewprintf("Path too long");
444: return NULL;
445: }
1.7 millert 446: if ((dirpipe = popen(line, "r")) == NULL) {
447: ewprintf("Problem opening pipe to ls");
448: return NULL;
449: }
450: line[0] = line[1] = ' ';
1.15 mickey 451: while (fgets(&line[2], sizeof(line) - 2, dirpipe) != NULL) {
1.7 millert 452: line[strlen(line) - 1] = '\0'; /* remove ^J */
1.13 art 453: (void) addline(bp, line);
1.7 millert 454: }
455: if (pclose(dirpipe) == -1) {
456: ewprintf("Problem closing pipe to ls");
457: return NULL;
458: }
459: bp->b_dotp = lforw(bp->b_linep); /* go to first line */
1.21 vincent 460: (void) strlcpy(bp->b_fname, dirname, sizeof bp->b_fname);
1.7 millert 461: if ((bp->b_modes[0] = name_mode("dired")) == NULL) {
1.17 art 462: bp->b_modes[0] = name_mode("fundamental");
1.7 millert 463: ewprintf("Could not find mode dired");
464: return NULL;
465: }
466: bp->b_nmodes = 0;
467: return bp;
1.1 deraadt 468: }
469:
1.23 vincent 470: #define NAME_FIELD 8
471:
1.7 millert 472: int
1.23 vincent 473: d_makename(LINE *lp, char *fn, int len)
1.1 deraadt 474: {
1.23 vincent 475: int i;
476: char *p, *np;
477:
478: strlcpy(fn, curbp->b_fname, len);
479: p = lp->l_text;
480: for (i = 0; i < NAME_FIELD; i++) {
481: np = strpbrk(p, "\t ");
482: if (np == NULL)
483: return ABORT;
484: p = np + 1;
485: while (*p != '\0' && strchr("\t ", *p))
486: p++;
487: }
488: strlcat(fn, p, len);
1.7 millert 489: return lgetc(lp, 2) == 'd';
1.1 deraadt 490: }
1.7 millert 491: #endif /* NO_DIRED */
1.1 deraadt 492:
493: struct filelist {
1.16 mickey 494: LIST fl_l;
495: char fl_name[NFILEN + 2];
1.1 deraadt 496: };
497:
1.7 millert 498: /*
1.1 deraadt 499: * return list of file names that match the name in buf.
500: */
501:
1.7 millert 502: LIST *
1.25 ! vincent 503: make_file_list(char *buf)
1.7 millert 504: {
1.16 mickey 505: char *dir, *file, *cp;
506: int len, preflen;
507: DIR *dirp;
508: struct dirent *dent;
509: LIST *last;
1.7 millert 510: struct filelist *current;
1.16 mickey 511: char prefixx[NFILEN + 1];
1.7 millert 512:
513: /*
514: * We need three different strings: dir - the name of the directory
515: * containing what the user typed. Must be a real unix file name,
516: * e.g. no ~user, etc.. Must not end in /. prefix - the portion of
517: * what the user typed that is before the names we are going to find
518: * in the directory. Must have a trailing / if the user typed it.
519: * names from the directory. we open dir, and return prefix
520: * concatenated with names.
521: */
522:
523: /* first we get a directory name we can look up */
524: /*
525: * Names ending in . are potentially odd, because adjustname will
526: * treat foo/.. as a reference to another directory, whereas we are
527: * interested in names starting with ..
528: */
529: len = strlen(buf);
530: if (buf[len - 1] == '.') {
531: buf[len - 1] = 'x';
532: dir = adjustname(buf);
533: buf[len - 1] = '.';
534: } else
535: dir = adjustname(buf);
536: /*
537: * If the user typed a trailing / or the empty string
538: * he wants us to use his file spec as a directory name.
539: */
540: if (buf[0] && buf[strlen(buf) - 1] != '/') {
541: file = strrchr(dir, '/');
542: if (file) {
543: *file = 0;
544: if (*dir == 0)
545: dir = "/";
546: } else {
547: return (NULL);
548: }
1.1 deraadt 549: }
1.7 millert 550: /* Now we get the prefix of the name the user typed. */
1.21 vincent 551: strlcpy(prefixx, buf, sizeof prefixx);
1.7 millert 552: cp = strrchr(prefixx, '/');
553: if (cp == NULL)
554: prefixx[0] = 0;
555: else
556: cp[1] = 0;
557:
558: preflen = strlen(prefixx);
559: /* cp is the tail of buf that really needs to be compared */
560: cp = buf + preflen;
561: len = strlen(cp);
562:
563: /*
564: * Now make sure that file names will fit in the buffers allocated.
565: * SV files are fairly short. For BSD, something more general would
566: * be required.
567: */
568: if ((preflen + MAXNAMLEN) > NFILEN)
569: return (NULL);
570:
571: /* loop over the specified directory, making up the list of files */
572:
573: /*
574: * Note that it is worth our time to filter out names that don't
575: * match, even though our caller is going to do so again, and to
576: * avoid doing the stat if completion is being done, because stat'ing
577: * every file in the directory is relatively expensive.
578: */
1.1 deraadt 579:
1.11 art 580: dirp = opendir(dir);
581: if (dirp == NULL)
1.7 millert 582: return (NULL);
583: last = NULL;
1.11 art 584:
585: while ((dent = readdir(dirp)) != NULL) {
1.18 art 586: int isdir;
587:
1.11 art 588: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
589: continue;
590:
1.18 art 591: isdir = 0;
592: if (dent->d_type == DT_DIR) {
593: isdir = 1;
594: } else if (dent->d_type == DT_LNK ||
595: dent->d_type == DT_UNKNOWN) {
596: struct stat statbuf;
597: char statname[NFILEN + 2];
598:
599: statbuf.st_mode = 0;
600: if (snprintf(statname, sizeof(statname), "%s/%s",
601: dir, dent->d_name) > sizeof(statname) - 1) {
602: continue;
603: }
604: if (stat(statname, &statbuf) < 0)
605: continue;
606: if (statbuf.st_mode & S_IFDIR)
607: isdir = 1;
608: }
609:
610: current = malloc(sizeof(struct filelist));
1.12 art 611: if (current == NULL)
612: break;
1.18 art 613:
1.11 art 614: if (snprintf(current->fl_name, sizeof(current->fl_name),
1.18 art 615: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "")
616: >= sizeof(current->fl_name)) {
1.11 art 617: free(current);
1.7 millert 618: continue;
619: }
620: current->fl_l.l_next = last;
621: current->fl_l.l_name = current->fl_name;
622: last = (LIST *) current;
1.1 deraadt 623: }
1.11 art 624: closedir(dirp);
1.1 deraadt 625:
1.7 millert 626: return (last);
1.1 deraadt 627: }