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