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