Annotation of src/usr.bin/mg/fileio.c, Revision 1.70
1.70 ! deraadt 1: /* $OpenBSD: fileio.c,v 1.69 2006/04/02 20:21:19 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>
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.61 kjell 407: int len, preflen, ret;
1.16 mickey 408: DIR *dirp;
409: struct dirent *dent;
1.68 kjell 410: struct list *last, *current;
411: char fl_name[NFILEN + 2];
1.49 db 412: char prefixx[NFILEN + 1];
1.7 millert 413:
414: /*
415: * We need three different strings: dir - the name of the directory
416: * containing what the user typed. Must be a real unix file name,
417: * e.g. no ~user, etc.. Must not end in /. prefix - the portion of
418: * what the user typed that is before the names we are going to find
419: * in the directory. Must have a trailing / if the user typed it.
1.49 db 420: * names from the directory. We open dir, and return prefix
1.7 millert 421: * concatenated with names.
422: */
423:
424: /* first we get a directory name we can look up */
425: /*
426: * Names ending in . are potentially odd, because adjustname will
427: * treat foo/.. as a reference to another directory, whereas we are
428: * interested in names starting with ..
429: */
430: len = strlen(buf);
431: if (buf[len - 1] == '.') {
432: buf[len - 1] = 'x';
433: dir = adjustname(buf);
434: buf[len - 1] = '.';
435: } else
436: dir = adjustname(buf);
1.33 vincent 437: if (dir == NULL)
1.36 vincent 438: return (NULL);
1.7 millert 439: /*
440: * If the user typed a trailing / or the empty string
441: * he wants us to use his file spec as a directory name.
442: */
443: if (buf[0] && buf[strlen(buf) - 1] != '/') {
444: file = strrchr(dir, '/');
445: if (file) {
1.69 kjell 446: *file = '\0';
447: if (*dir == '\0')
1.7 millert 448: dir = "/";
1.49 db 449: } else
1.7 millert 450: return (NULL);
1.1 deraadt 451: }
1.7 millert 452: /* Now we get the prefix of the name the user typed. */
1.67 kjell 453: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
454: return (NULL);
1.7 millert 455: cp = strrchr(prefixx, '/');
456: if (cp == NULL)
1.67 kjell 457: prefixx[0] = '\0';
1.7 millert 458: else
1.67 kjell 459: cp[1] = '\0';
1.7 millert 460:
461: preflen = strlen(prefixx);
1.49 db 462: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 463: cp = buf + preflen;
464: len = strlen(cp);
465:
466: /*
467: * Now make sure that file names will fit in the buffers allocated.
468: * SV files are fairly short. For BSD, something more general would
469: * be required.
470: */
471: if ((preflen + MAXNAMLEN) > NFILEN)
472: return (NULL);
473:
474: /* loop over the specified directory, making up the list of files */
475:
476: /*
477: * Note that it is worth our time to filter out names that don't
478: * match, even though our caller is going to do so again, and to
479: * avoid doing the stat if completion is being done, because stat'ing
480: * every file in the directory is relatively expensive.
481: */
1.1 deraadt 482:
1.11 art 483: dirp = opendir(dir);
484: if (dirp == NULL)
1.7 millert 485: return (NULL);
486: last = NULL;
1.11 art 487:
488: while ((dent = readdir(dirp)) != NULL) {
1.18 art 489: int isdir;
490:
1.11 art 491: if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
492: continue;
493:
1.18 art 494: isdir = 0;
495: if (dent->d_type == DT_DIR) {
496: isdir = 1;
497: } else if (dent->d_type == DT_LNK ||
498: dent->d_type == DT_UNKNOWN) {
499: struct stat statbuf;
500: char statname[NFILEN + 2];
501:
502: statbuf.st_mode = 0;
1.61 kjell 503: ret = snprintf(statname, sizeof(statname), "%s/%s",
504: dir, dent->d_name);
505: if (ret < 0 || ret > sizeof(statname) - 1)
1.18 art 506: continue;
507: if (stat(statname, &statbuf) < 0)
508: continue;
509: if (statbuf.st_mode & S_IFDIR)
510: isdir = 1;
511: }
512:
1.68 kjell 513: if ((current = malloc(sizeof(struct list))) == NULL) {
514: free_file_list(last);
515: return (NULL);
516: }
517: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 518: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 519: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 520: free(current);
1.7 millert 521: continue;
522: }
1.68 kjell 523: current->l_next = last;
524: current->l_name = strdup(fl_name);
525: last = current;
1.1 deraadt 526: }
1.11 art 527: closedir(dirp);
1.1 deraadt 528:
1.7 millert 529: return (last);
1.54 kjell 530: }
531:
532: /*
533: * Test if a supplied filename refers to a directory
534: * Returns ABORT on error, TRUE if directory. FALSE otherwise
535: */
536: int
537: fisdir(const char *fname)
538: {
539: struct stat statbuf;
540:
541: if (stat(fname, &statbuf) != 0)
542: return (ABORT);
543:
544: if (S_ISDIR(statbuf.st_mode))
545: return (TRUE);
546:
547: return (FALSE);
1.1 deraadt 548: }