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