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