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