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