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