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