Annotation of src/usr.bin/mg/fileio.c, Revision 1.62
1.62 ! deraadt 1: /* $OpenBSD: fileio.c,v 1.61 2005/11/13 07:49:02 kjell 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.62 ! deraadt 31: ffropen(const char *fn, struct 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.62 ! deraadt 63: ffwopen(const char *fn, struct 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.62 ! deraadt 104: ffclose(struct 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.62 ! deraadt 115: ffputbuf(struct buffer *bp)
1.1 deraadt 116: {
1.62 ! deraadt 117: struct 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];
1.60 kjell 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));
1.59 deraadt 282: else { /* ~user/ or ~user */
1.57 kjell 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.61 kjell 325: int ret;
1.12 art 326:
327: if ((home = getenv("HOME")) == NULL || *home == '\0')
328: goto nohome;
1.1 deraadt 329:
1.12 art 330: if (suffix == NULL) {
1.61 kjell 331: ret = snprintf(file, sizeof(file), "%s/.mg", home);
332: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 333: return (NULL);
1.12 art 334: } else {
1.61 kjell 335: ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
336: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 337: return (NULL);
1.1 deraadt 338: }
1.12 art 339:
340: if (access(file, R_OK) == 0)
1.32 vincent 341: return (file);
1.12 art 342: nohome:
343: #ifdef STARTUPFILE
1.27 millert 344: if (suffix == NULL) {
1.61 kjell 345: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
346: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 347: return (NULL);
1.12 art 348: } else {
1.61 kjell 349: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
350: suffix);
351: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 352: return (NULL);
1.12 art 353: }
354:
355: if (access(file, R_OK) == 0)
1.32 vincent 356: return (file);
1.61 kjell 357: #endif /* STARTUPFILE */
1.32 vincent 358: return (NULL);
1.1 deraadt 359: }
1.61 kjell 360: #endif /* !NO_STARTUP */
1.1 deraadt 361:
362: #ifndef NO_DIRED
363:
1.7 millert 364: int
1.27 millert 365: copy(char *frname, char *toname)
1.1 deraadt 366: {
1.49 db 367: int ifd, ofd, n;
368: char buf[BUFSIZ];
369: mode_t mode = DEFFILEMODE; /* XXX?? */
370: struct stat orig;
1.30 vincent 371:
372: if ((ifd = open(frname, O_RDONLY)) == -1)
373: return (FALSE);
374: if (fstat(ifd, &orig) == -1) {
375: ewprintf("fstat: %s", strerror(errno));
376: close(ifd);
377: return (FALSE);
378: }
379:
380: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
381: close(ifd);
382: return (FALSE);
383: }
1.49 db 384: while ((n = read(ifd, buf, sizeof(buf))) > 0) {
1.30 vincent 385: if (write(ofd, buf, n) != n) {
386: ewprintf("write error : %s", strerror(errno));
387: break;
388: }
389: }
390: if (fchmod(ofd, orig.st_mode) == -1)
391: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 392:
1.30 vincent 393: if (n == -1) {
394: ewprintf("Read error : %s", strerror(errno));
395: close(ifd);
396: close(ofd);
397: return (FALSE);
1.7 millert 398: }
1.30 vincent 399: /*
1.32 vincent 400: * It is "normal" for this to fail since we can't guarantee that
1.49 db 401: * we will be running as root.
1.30 vincent 402: */
403: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
404: ewprintf("Cannot set owner : %s", strerror(errno));
405:
406: (void) close(ifd);
407: (void) close(ofd);
408:
409: return (TRUE);
1.7 millert 410: }
411:
412: #endif /* NO_DIRED */
1.1 deraadt 413:
414: struct filelist {
1.62 ! deraadt 415: struct list fl_l;
1.16 mickey 416: char fl_name[NFILEN + 2];
1.1 deraadt 417: };
418:
1.7 millert 419: /*
1.1 deraadt 420: * return list of file names that match the name in buf.
421: */
1.62 ! deraadt 422: struct list *
1.25 vincent 423: make_file_list(char *buf)
1.7 millert 424: {
1.16 mickey 425: char *dir, *file, *cp;
1.61 kjell 426: int len, preflen, ret;
1.16 mickey 427: DIR *dirp;
428: struct dirent *dent;
1.62 ! deraadt 429: struct list *last;
1.7 millert 430: struct filelist *current;
1.49 db 431: char prefixx[NFILEN + 1];
1.7 millert 432:
433: /*
434: * We need three different strings: dir - the name of the directory
435: * containing what the user typed. Must be a real unix file name,
436: * e.g. no ~user, etc.. Must not end in /. prefix - the portion of
437: * what the user typed that is before the names we are going to find
438: * in the directory. Must have a trailing / if the user typed it.
1.49 db 439: * names from the directory. We open dir, and return prefix
1.7 millert 440: * concatenated with names.
441: */
442:
443: /* first we get a directory name we can look up */
444: /*
445: * Names ending in . are potentially odd, because adjustname will
446: * treat foo/.. as a reference to another directory, whereas we are
447: * interested in names starting with ..
448: */
449: len = strlen(buf);
450: if (buf[len - 1] == '.') {
451: buf[len - 1] = 'x';
452: dir = adjustname(buf);
453: buf[len - 1] = '.';
454: } else
455: dir = adjustname(buf);
1.33 vincent 456: if (dir == NULL)
1.36 vincent 457: return (NULL);
1.7 millert 458: /*
459: * If the user typed a trailing / or the empty string
460: * he wants us to use his file spec as a directory name.
461: */
462: if (buf[0] && buf[strlen(buf) - 1] != '/') {
463: file = strrchr(dir, '/');
464: if (file) {
465: *file = 0;
466: if (*dir == 0)
467: dir = "/";
1.49 db 468: } else
1.7 millert 469: return (NULL);
1.1 deraadt 470: }
1.7 millert 471: /* Now we get the prefix of the name the user typed. */
1.49 db 472: strlcpy(prefixx, buf, sizeof(prefixx));
1.7 millert 473: cp = strrchr(prefixx, '/');
474: if (cp == NULL)
475: prefixx[0] = 0;
476: else
477: cp[1] = 0;
478:
479: preflen = strlen(prefixx);
1.49 db 480: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 481: cp = buf + preflen;
482: len = strlen(cp);
483:
484: /*
485: * Now make sure that file names will fit in the buffers allocated.
486: * SV files are fairly short. For BSD, something more general would
487: * be required.
488: */
489: if ((preflen + MAXNAMLEN) > NFILEN)
490: return (NULL);
491:
492: /* loop over the specified directory, making up the list of files */
493:
494: /*
495: * Note that it is worth our time to filter out names that don't
496: * match, even though our caller is going to do so again, and to
497: * avoid doing the stat if completion is being done, because stat'ing
498: * every file in the directory is relatively expensive.
499: */
1.1 deraadt 500:
1.11 art 501: dirp = opendir(dir);
502: if (dirp == NULL)
1.7 millert 503: return (NULL);
504: last = NULL;
1.11 art 505:
506: while ((dent = readdir(dirp)) != NULL) {
1.18 art 507: int isdir;
508:
1.11 art 509: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
510: continue;
511:
1.18 art 512: isdir = 0;
513: if (dent->d_type == DT_DIR) {
514: isdir = 1;
515: } else if (dent->d_type == DT_LNK ||
516: dent->d_type == DT_UNKNOWN) {
517: struct stat statbuf;
518: char statname[NFILEN + 2];
519:
520: statbuf.st_mode = 0;
1.61 kjell 521: ret = snprintf(statname, sizeof(statname), "%s/%s",
522: dir, dent->d_name);
523: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 524: continue;
525: if (stat(statname, &statbuf) < 0)
526: continue;
527: if (statbuf.st_mode & S_IFDIR)
528: isdir = 1;
529: }
530:
531: current = malloc(sizeof(struct filelist));
1.12 art 532: if (current == NULL)
533: break;
1.18 art 534:
1.61 kjell 535: ret = snprintf(current->fl_name, sizeof(current->fl_name),
536: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
537: if (ret < 0 || ret >= sizeof(current->fl_name)) {
1.11 art 538: free(current);
1.7 millert 539: continue;
540: }
541: current->fl_l.l_next = last;
542: current->fl_l.l_name = current->fl_name;
1.62 ! deraadt 543: last = (struct list *) current;
1.1 deraadt 544: }
1.11 art 545: closedir(dirp);
1.1 deraadt 546:
1.7 millert 547: return (last);
1.54 kjell 548: }
549:
550: /*
551: * Test if a supplied filename refers to a directory
552: * Returns ABORT on error, TRUE if directory. FALSE otherwise
553: */
554: int
555: fisdir(const char *fname)
556: {
557: struct stat statbuf;
558:
559: if (stat(fname, &statbuf) != 0)
560: return (ABORT);
561:
562: if (S_ISDIR(statbuf.st_mode))
563: return (TRUE);
564:
565: return (FALSE);
1.1 deraadt 566: }