Annotation of src/usr.bin/mg/fileio.c, Revision 1.26
1.26 ! vincent 1: /* $OpenBSD: fileio.c,v 1.25 2002/03/11 13:02:56 vincent 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:
1.26 ! vincent 417: /*
! 418: * dirname needs to have enough place to store an additional '/'.
! 419: */
1.7 millert 420: BUFFER *
421: dired_(dirname)
1.16 mickey 422: char *dirname;
1.7 millert 423: {
1.16 mickey 424: BUFFER *bp;
425: FILE *dirpipe;
426: char line[256];
1.15 mickey 427: int len;
1.7 millert 428:
429: if ((dirname = adjustname(dirname)) == NULL) {
430: ewprintf("Bad directory name");
431: return NULL;
432: }
1.15 mickey 433: /* this should not be done, instead adjustname() should get a flag */
434: len = strlen(dirname);
435: if (dirname[len - 1] != '/') {
436: dirname[len++] = '/';
437: dirname[len] = '\0';
438: }
1.7 millert 439: if ((bp = findbuffer(dirname)) == NULL) {
440: ewprintf("Could not create buffer");
441: return NULL;
442: }
443: if (bclear(bp) != TRUE)
444: return FALSE;
1.26 ! vincent 445: bp->b_flag |= BFREADONLY;
1.15 mickey 446: if (snprintf(line, sizeof(line), "ls -al %s", dirname) >= sizeof(line)){
447: ewprintf("Path too long");
448: return NULL;
449: }
1.7 millert 450: if ((dirpipe = popen(line, "r")) == NULL) {
451: ewprintf("Problem opening pipe to ls");
452: return NULL;
453: }
454: line[0] = line[1] = ' ';
1.15 mickey 455: while (fgets(&line[2], sizeof(line) - 2, dirpipe) != NULL) {
1.7 millert 456: line[strlen(line) - 1] = '\0'; /* remove ^J */
1.13 art 457: (void) addline(bp, line);
1.7 millert 458: }
459: if (pclose(dirpipe) == -1) {
460: ewprintf("Problem closing pipe to ls");
461: return NULL;
462: }
463: bp->b_dotp = lforw(bp->b_linep); /* go to first line */
1.21 vincent 464: (void) strlcpy(bp->b_fname, dirname, sizeof bp->b_fname);
1.7 millert 465: if ((bp->b_modes[0] = name_mode("dired")) == NULL) {
1.17 art 466: bp->b_modes[0] = name_mode("fundamental");
1.7 millert 467: ewprintf("Could not find mode dired");
468: return NULL;
469: }
470: bp->b_nmodes = 0;
471: return bp;
1.1 deraadt 472: }
473:
1.23 vincent 474: #define NAME_FIELD 8
475:
1.7 millert 476: int
1.23 vincent 477: d_makename(LINE *lp, char *fn, int len)
1.1 deraadt 478: {
1.23 vincent 479: int i;
480: char *p, *np;
481:
482: strlcpy(fn, curbp->b_fname, len);
483: p = lp->l_text;
484: for (i = 0; i < NAME_FIELD; i++) {
485: np = strpbrk(p, "\t ");
486: if (np == NULL)
487: return ABORT;
488: p = np + 1;
489: while (*p != '\0' && strchr("\t ", *p))
490: p++;
491: }
492: strlcat(fn, p, len);
1.7 millert 493: return lgetc(lp, 2) == 'd';
1.1 deraadt 494: }
1.7 millert 495: #endif /* NO_DIRED */
1.1 deraadt 496:
497: struct filelist {
1.16 mickey 498: LIST fl_l;
499: char fl_name[NFILEN + 2];
1.1 deraadt 500: };
501:
1.7 millert 502: /*
1.1 deraadt 503: * return list of file names that match the name in buf.
504: */
505:
1.7 millert 506: LIST *
1.25 vincent 507: make_file_list(char *buf)
1.7 millert 508: {
1.16 mickey 509: char *dir, *file, *cp;
510: int len, preflen;
511: DIR *dirp;
512: struct dirent *dent;
513: LIST *last;
1.7 millert 514: struct filelist *current;
1.16 mickey 515: char prefixx[NFILEN + 1];
1.7 millert 516:
517: /*
518: * We need three different strings: dir - the name of the directory
519: * containing what the user typed. Must be a real unix file name,
520: * e.g. no ~user, etc.. Must not end in /. prefix - the portion of
521: * what the user typed that is before the names we are going to find
522: * in the directory. Must have a trailing / if the user typed it.
523: * names from the directory. we open dir, and return prefix
524: * concatenated with names.
525: */
526:
527: /* first we get a directory name we can look up */
528: /*
529: * Names ending in . are potentially odd, because adjustname will
530: * treat foo/.. as a reference to another directory, whereas we are
531: * interested in names starting with ..
532: */
533: len = strlen(buf);
534: if (buf[len - 1] == '.') {
535: buf[len - 1] = 'x';
536: dir = adjustname(buf);
537: buf[len - 1] = '.';
538: } else
539: dir = adjustname(buf);
540: /*
541: * If the user typed a trailing / or the empty string
542: * he wants us to use his file spec as a directory name.
543: */
544: if (buf[0] && buf[strlen(buf) - 1] != '/') {
545: file = strrchr(dir, '/');
546: if (file) {
547: *file = 0;
548: if (*dir == 0)
549: dir = "/";
550: } else {
551: return (NULL);
552: }
1.1 deraadt 553: }
1.7 millert 554: /* Now we get the prefix of the name the user typed. */
1.21 vincent 555: strlcpy(prefixx, buf, sizeof prefixx);
1.7 millert 556: cp = strrchr(prefixx, '/');
557: if (cp == NULL)
558: prefixx[0] = 0;
559: else
560: cp[1] = 0;
561:
562: preflen = strlen(prefixx);
563: /* cp is the tail of buf that really needs to be compared */
564: cp = buf + preflen;
565: len = strlen(cp);
566:
567: /*
568: * Now make sure that file names will fit in the buffers allocated.
569: * SV files are fairly short. For BSD, something more general would
570: * be required.
571: */
572: if ((preflen + MAXNAMLEN) > NFILEN)
573: return (NULL);
574:
575: /* loop over the specified directory, making up the list of files */
576:
577: /*
578: * Note that it is worth our time to filter out names that don't
579: * match, even though our caller is going to do so again, and to
580: * avoid doing the stat if completion is being done, because stat'ing
581: * every file in the directory is relatively expensive.
582: */
1.1 deraadt 583:
1.11 art 584: dirp = opendir(dir);
585: if (dirp == NULL)
1.7 millert 586: return (NULL);
587: last = NULL;
1.11 art 588:
589: while ((dent = readdir(dirp)) != NULL) {
1.18 art 590: int isdir;
591:
1.11 art 592: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
593: continue;
594:
1.18 art 595: isdir = 0;
596: if (dent->d_type == DT_DIR) {
597: isdir = 1;
598: } else if (dent->d_type == DT_LNK ||
599: dent->d_type == DT_UNKNOWN) {
600: struct stat statbuf;
601: char statname[NFILEN + 2];
602:
603: statbuf.st_mode = 0;
604: if (snprintf(statname, sizeof(statname), "%s/%s",
605: dir, dent->d_name) > sizeof(statname) - 1) {
606: continue;
607: }
608: if (stat(statname, &statbuf) < 0)
609: continue;
610: if (statbuf.st_mode & S_IFDIR)
611: isdir = 1;
612: }
613:
614: current = malloc(sizeof(struct filelist));
1.12 art 615: if (current == NULL)
616: break;
1.18 art 617:
1.11 art 618: if (snprintf(current->fl_name, sizeof(current->fl_name),
1.18 art 619: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "")
620: >= sizeof(current->fl_name)) {
1.11 art 621: free(current);
1.7 millert 622: continue;
623: }
624: current->fl_l.l_next = last;
625: current->fl_l.l_name = current->fl_name;
626: last = (LIST *) current;
1.1 deraadt 627: }
1.11 art 628: closedir(dirp);
1.1 deraadt 629:
1.7 millert 630: return (last);
1.1 deraadt 631: }