Annotation of src/usr.bin/mg/fileio.c, Revision 1.98
1.98 ! guenther 1: /* $OpenBSD: fileio.c,v 1.97 2014/03/20 07:47:29 lum 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.56 deraadt 9:
1.82 kjell 10: #include <sys/types.h>
1.72 kjell 11: #include <sys/stat.h>
1.56 deraadt 12: #include <sys/time.h>
1.76 jason 13: #include <sys/resource.h>
1.53 kjell 14: #include <sys/wait.h>
1.72 kjell 15:
16: #include <fcntl.h>
1.33 vincent 17: #include <limits.h>
1.76 jason 18: #include <dirent.h>
1.72 kjell 19: #include <pwd.h>
1.21 vincent 20: #include <string.h>
1.9 millert 21: #include <unistd.h>
1.1 deraadt 22:
1.72 kjell 23: #include "kbd.h"
1.93 jasper 24: #include "pathnames.h"
1.72 kjell 25:
1.91 lum 26: static char *bkuplocation(const char *);
27: static int bkupleavetmp(const char *);
28:
29: static char *bkupdir;
1.93 jasper 30: static int leavetmp = 0; /* 1 = leave any '~' files in tmp dir */
1.91 lum 31:
1.1 deraadt 32: /*
33: * Open a file for reading.
34: */
1.7 millert 35: int
1.89 lum 36: ffropen(FILE ** ffp, const char *fn, struct buffer *bp)
1.7 millert 37: {
1.89 lum 38: if ((*ffp = fopen(fn, "r")) == NULL) {
1.46 jfb 39: if (errno == ENOENT)
40: return (FIOFNF);
41: return (FIOERR);
42: }
1.53 kjell 43:
44: /* If 'fn' is a directory open it with dired. */
1.54 kjell 45: if (fisdir(fn) == TRUE)
1.53 kjell 46: return (FIODIR);
47:
1.89 lum 48: ffstat(*ffp, bp);
1.93 jasper 49:
1.82 kjell 50: return (FIOSUC);
51: }
52:
53: /*
54: * Update stat/dirty info
55: */
56: void
1.89 lum 57: ffstat(FILE *ffp, struct buffer *bp)
1.82 kjell 58: {
59: struct stat sb;
60:
61: if (bp && fstat(fileno(ffp), &sb) == 0) {
1.7 millert 62: /* set highorder bit to make sure this isn't all zero */
1.82 kjell 63: bp->b_fi.fi_mode = sb.st_mode | 0x8000;
64: bp->b_fi.fi_uid = sb.st_uid;
65: bp->b_fi.fi_gid = sb.st_gid;
66: bp->b_fi.fi_mtime = sb.st_mtimespec;
67: /* Clear the ignore flag */
68: bp->b_flag &= ~(BFIGNDIRTY | BFDIRTY);
1.1 deraadt 69: }
1.82 kjell 70: }
1.53 kjell 71:
1.82 kjell 72: /*
73: * Update the status/dirty info. If there is an error,
74: * there's not a lot we can do.
75: */
76: int
77: fupdstat(struct buffer *bp)
78: {
1.89 lum 79: FILE *ffp;
80:
1.82 kjell 81: if ((ffp = fopen(bp->b_fname, "r")) == NULL) {
82: if (errno == ENOENT)
83: return (FIOFNF);
84: return (FIOERR);
85: }
1.89 lum 86: ffstat(ffp, bp);
87: (void)ffclose(ffp, bp);
1.1 deraadt 88: return (FIOSUC);
89: }
90:
91: /*
92: * Open a file for writing.
93: */
1.7 millert 94: int
1.89 lum 95: ffwopen(FILE ** ffp, const char *fn, struct buffer *bp)
1.7 millert 96: {
1.49 db 97: int fd;
1.66 kjell 98: mode_t fmode = DEFFILEMODE;
1.7 millert 99:
1.24 deraadt 100: if (bp && bp->b_fi.fi_mode)
1.66 kjell 101: fmode = bp->b_fi.fi_mode & 07777;
1.24 deraadt 102:
1.66 kjell 103: fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, fmode);
1.24 deraadt 104: if (fd == -1) {
105: ffp = NULL;
1.97 lum 106: dobeep();
1.21 vincent 107: ewprintf("Cannot open file for writing : %s", strerror(errno));
1.1 deraadt 108: return (FIOERR);
1.34 deraadt 109: }
1.24 deraadt 110:
1.89 lum 111: if ((*ffp = fdopen(fd, "w")) == NULL) {
1.97 lum 112: dobeep();
1.24 deraadt 113: ewprintf("Cannot open file for writing : %s", strerror(errno));
114: close(fd);
1.34 deraadt 115: return (FIOERR);
1.1 deraadt 116: }
1.7 millert 117:
118: /*
119: * If we have file information, use it. We don't bother to check for
120: * errors, because there's no a lot we can do about it. Certainly
1.24 deraadt 121: * trying to change ownership will fail if we aren't root. That's
1.7 millert 122: * probably OK. If we don't have info, no need to get it, since any
123: * future writes will do the same thing.
1.1 deraadt 124: */
125: if (bp && bp->b_fi.fi_mode) {
1.28 deraadt 126: fchmod(fd, bp->b_fi.fi_mode & 07777);
127: fchown(fd, bp->b_fi.fi_uid, bp->b_fi.fi_gid);
1.1 deraadt 128: }
129: return (FIOSUC);
130: }
131:
132: /*
133: * Close a file.
134: */
1.7 millert 135: /* ARGSUSED */
136: int
1.89 lum 137: ffclose(FILE *ffp, struct buffer *bp)
1.7 millert 138: {
1.85 lum 139: if (fclose(ffp) == 0)
140: return (FIOSUC);
1.93 jasper 141: return (FIOERR);
1.1 deraadt 142: }
143:
144: /*
1.41 vincent 145: * Write a buffer to the already opened file. bp points to the
1.1 deraadt 146: * buffer. Return the status.
147: */
1.7 millert 148: int
1.89 lum 149: ffputbuf(FILE *ffp, struct buffer *bp)
1.1 deraadt 150: {
1.62 deraadt 151: struct line *lp, *lpend;
1.7 millert 152:
1.77 kjell 153: lpend = bp->b_headp;
1.41 vincent 154: for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
155: if (fwrite(ltext(lp), 1, llength(lp), ffp) != llength(lp)) {
1.97 lum 156: dobeep();
1.41 vincent 157: ewprintf("Write I/O error");
1.49 db 158: return (FIOERR);
1.7 millert 159: }
1.41 vincent 160: if (lforw(lp) != lpend) /* no implied \n on last line */
161: putc('\n', ffp);
1.48 deraadt 162: }
1.42 vincent 163: /*
164: * XXX should be variable controlled (once we have variables)
165: */
166: if (llength(lback(lpend)) != 0) {
167: if (eyorn("No newline at end of file, add one") == TRUE) {
168: lnewline_at(lback(lpend), llength(lback(lpend)));
169: putc('\n', ffp);
170: }
171: }
1.7 millert 172: return (FIOSUC);
1.1 deraadt 173: }
174:
175: /*
176: * Read a line from a file, and store the bytes
177: * in the supplied buffer. Stop on end of file or end of
178: * line. When FIOEOF is returned, there is a valid line
179: * of data without the normally implied \n.
1.80 kjell 180: * If the line length exceeds nbuf, FIOLONG is returned.
1.1 deraadt 181: */
1.7 millert 182: int
1.89 lum 183: ffgetline(FILE *ffp, char *buf, int nbuf, int *nbytes)
1.1 deraadt 184: {
1.16 mickey 185: int c, i;
1.1 deraadt 186:
187: i = 0;
1.7 millert 188: while ((c = getc(ffp)) != EOF && c != '\n') {
1.1 deraadt 189: buf[i++] = c;
1.7 millert 190: if (i >= nbuf)
1.49 db 191: return (FIOLONG);
1.1 deraadt 192: }
1.7 millert 193: if (c == EOF && ferror(ffp) != FALSE) {
1.97 lum 194: dobeep();
1.1 deraadt 195: ewprintf("File read error");
1.49 db 196: return (FIOERR);
1.1 deraadt 197: }
198: *nbytes = i;
1.49 db 199: return (c == EOF ? FIOEOF : FIOSUC);
1.1 deraadt 200: }
201:
202: /*
1.9 millert 203: * Make a backup copy of "fname". On Unix the backup has the same
204: * name as the original file, with a "~" on the end; this seems to
205: * be newest of the new-speak. The error handling is all in "file.c".
206: * We do a copy instead of a rename since otherwise another process
207: * with an open fd will get the backup, not the new file. This is
208: * a problem when using mg with things like crontab and vipw.
1.1 deraadt 209: */
1.7 millert 210: int
1.25 vincent 211: fbackupfile(const char *fn)
1.7 millert 212: {
1.49 db 213: struct stat sb;
214: int from, to, serrno;
215: ssize_t nread;
216: char buf[BUFSIZ];
1.91 lum 217: char *nname, *tname, *bkpth;
1.1 deraadt 218:
1.35 vincent 219: if (stat(fn, &sb) == -1) {
1.97 lum 220: dobeep();
1.35 vincent 221: ewprintf("Can't stat %s : %s", fn, strerror(errno));
222: return (FALSE);
223: }
224:
1.91 lum 225: if ((bkpth = bkuplocation(fn)) == NULL)
226: return (FALSE);
227:
228: if (asprintf(&nname, "%s~", bkpth) == -1) {
1.97 lum 229: dobeep();
1.90 lum 230: ewprintf("Can't allocate backup file name : %s", strerror(errno));
1.91 lum 231: free(bkpth);
1.44 millert 232: return (ABORT);
233: }
1.91 lum 234: if (asprintf(&tname, "%s.XXXXXXXXXX", bkpth) == -1) {
1.97 lum 235: dobeep();
1.44 millert 236: ewprintf("Can't allocate temp file name : %s", strerror(errno));
1.91 lum 237: free(bkpth);
1.44 millert 238: free(nname);
1.1 deraadt 239: return (ABORT);
1.9 millert 240: }
1.91 lum 241: free(bkpth);
1.9 millert 242:
1.21 vincent 243: if ((from = open(fn, O_RDONLY)) == -1) {
244: free(nname);
1.44 millert 245: free(tname);
1.1 deraadt 246: return (FALSE);
1.21 vincent 247: }
1.44 millert 248: to = mkstemp(tname);
1.9 millert 249: if (to == -1) {
250: serrno = errno;
251: close(from);
1.21 vincent 252: free(nname);
1.44 millert 253: free(tname);
1.9 millert 254: errno = serrno;
255: return (FALSE);
256: }
257: while ((nread = read(from, buf, sizeof(buf))) > 0) {
1.70 deraadt 258: if (write(to, buf, (size_t)nread) != nread) {
1.40 vincent 259: nread = -1;
260: break;
1.9 millert 261: }
1.1 deraadt 262: }
1.9 millert 263: serrno = errno;
1.44 millert 264: (void) fchmod(to, (sb.st_mode & 0777));
1.9 millert 265: close(from);
266: close(to);
1.21 vincent 267: if (nread == -1) {
1.44 millert 268: if (unlink(tname) == -1)
1.21 vincent 269: ewprintf("Can't unlink temp : %s", strerror(errno));
1.44 millert 270: } else {
271: if (rename(tname, nname) == -1) {
272: ewprintf("Can't rename temp : %s", strerror(errno));
273: (void) unlink(tname);
1.45 henning 274: nread = -1;
1.44 millert 275: }
1.21 vincent 276: }
1.1 deraadt 277: free(nname);
1.44 millert 278: free(tname);
1.9 millert 279: errno = serrno;
1.22 deraadt 280:
1.9 millert 281: return (nread == -1 ? FALSE : TRUE);
1.1 deraadt 282: }
283:
284: /*
1.73 kjell 285: * Convert "fn" to a canonicalized absolute filename, replacing
286: * a leading ~/ with the user's home dir, following symlinks, and
1.96 lum 287: * remove all occurrences of /./ and /../
1.1 deraadt 288: */
1.7 millert 289: char *
1.74 jason 290: adjustname(const char *fn, int slashslash)
1.1 deraadt 291: {
1.98 ! guenther 292: static char fnb[PATH_MAX];
1.74 jason 293: const char *cp, *ep = NULL;
1.92 lum 294: char *path;
1.74 jason 295:
1.75 kjell 296: if (slashslash == TRUE) {
1.74 jason 297: cp = fn + strlen(fn) - 1;
298: for (; cp >= fn; cp--) {
299: if (ep && (*cp == '/')) {
300: fn = ep;
301: break;
302: }
303: if (*cp == '/' || *cp == '~')
304: ep = cp;
305: else
306: ep = NULL;
307: }
308: }
1.92 lum 309: if ((path = expandtilde(fn)) == NULL)
1.57 kjell 310: return (NULL);
1.33 vincent 311:
1.47 henning 312: if (realpath(path, fnb) == NULL)
1.57 kjell 313: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 314:
1.92 lum 315: free(path);
1.47 henning 316: return (fnb);
1.1 deraadt 317: }
318:
319: /*
320: * Find a startup file for the user and return its name. As a service
321: * to other pieces of code that may want to find a startup file (like
322: * the terminal driver in particular), accepts a suffix to be appended
323: * to the startup file name.
324: */
325: char *
1.27 millert 326: startupfile(char *suffix)
1.1 deraadt 327: {
1.49 db 328: static char file[NFILEN];
1.16 mickey 329: char *home;
1.61 kjell 330: int ret;
1.12 art 331:
332: if ((home = getenv("HOME")) == NULL || *home == '\0')
333: goto nohome;
1.1 deraadt 334:
1.12 art 335: if (suffix == NULL) {
1.93 jasper 336: ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
1.61 kjell 337: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 338: return (NULL);
1.12 art 339: } else {
1.93 jasper 340: ret = snprintf(file, sizeof(file), _PATH_MG_TERM, home, suffix);
1.61 kjell 341: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 342: return (NULL);
1.1 deraadt 343: }
1.12 art 344:
345: if (access(file, R_OK) == 0)
1.32 vincent 346: return (file);
1.12 art 347: nohome:
348: #ifdef STARTUPFILE
1.27 millert 349: if (suffix == NULL) {
1.61 kjell 350: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
351: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 352: return (NULL);
1.12 art 353: } else {
1.61 kjell 354: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
355: suffix);
356: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 357: return (NULL);
1.12 art 358: }
359:
360: if (access(file, R_OK) == 0)
1.32 vincent 361: return (file);
1.61 kjell 362: #endif /* STARTUPFILE */
1.32 vincent 363: return (NULL);
1.1 deraadt 364: }
365:
1.7 millert 366: int
1.27 millert 367: copy(char *frname, char *toname)
1.1 deraadt 368: {
1.70 deraadt 369: int ifd, ofd;
1.49 db 370: char buf[BUFSIZ];
1.66 kjell 371: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 372: struct stat orig;
1.70 deraadt 373: ssize_t sr;
1.30 vincent 374:
375: if ((ifd = open(frname, O_RDONLY)) == -1)
376: return (FALSE);
377: if (fstat(ifd, &orig) == -1) {
1.97 lum 378: dobeep();
1.30 vincent 379: ewprintf("fstat: %s", strerror(errno));
380: close(ifd);
381: return (FALSE);
382: }
383:
1.66 kjell 384: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 385: close(ifd);
386: return (FALSE);
387: }
1.70 deraadt 388: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
389: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 390: ewprintf("write error : %s", strerror(errno));
391: break;
392: }
393: }
394: if (fchmod(ofd, orig.st_mode) == -1)
395: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 396:
1.70 deraadt 397: if (sr == -1) {
1.30 vincent 398: ewprintf("Read error : %s", strerror(errno));
399: close(ifd);
400: close(ofd);
401: return (FALSE);
1.7 millert 402: }
1.30 vincent 403: /*
1.32 vincent 404: * It is "normal" for this to fail since we can't guarantee that
1.49 db 405: * we will be running as root.
1.30 vincent 406: */
407: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
408: ewprintf("Cannot set owner : %s", strerror(errno));
409:
410: (void) close(ifd);
411: (void) close(ofd);
412:
413: return (TRUE);
1.7 millert 414: }
1.1 deraadt 415:
1.7 millert 416: /*
1.1 deraadt 417: * return list of file names that match the name in buf.
418: */
1.62 deraadt 419: struct list *
1.25 vincent 420: make_file_list(char *buf)
1.7 millert 421: {
1.16 mickey 422: char *dir, *file, *cp;
1.71 kjell 423: size_t len, preflen;
424: int ret;
1.16 mickey 425: DIR *dirp;
426: struct dirent *dent;
1.68 kjell 427: struct list *last, *current;
428: char fl_name[NFILEN + 2];
1.49 db 429: char prefixx[NFILEN + 1];
1.7 millert 430:
431: /*
1.79 deraadt 432: * We need three different strings:
1.72 kjell 433:
434: * dir - the name of the directory containing what the user typed.
435: * Must be a real unix file name, e.g. no ~user, etc..
436: * Must not end in /.
437: * prefix - the portion of what the user typed that is before the
438: * names we are going to find in the directory. Must have a
439: * trailing / if the user typed it.
440: * names from the directory - We open dir, and return prefix
1.7 millert 441: * concatenated with names.
442: */
443:
444: /* first we get a directory name we can look up */
445: /*
446: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 447: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 448: * interested in names starting with ..
449: */
450: len = strlen(buf);
1.71 kjell 451: if (len && buf[len - 1] == '.') {
1.7 millert 452: buf[len - 1] = 'x';
1.74 jason 453: dir = adjustname(buf, TRUE);
1.7 millert 454: buf[len - 1] = '.';
455: } else
1.74 jason 456: dir = adjustname(buf, TRUE);
1.33 vincent 457: if (dir == NULL)
1.36 vincent 458: return (NULL);
1.7 millert 459: /*
460: * If the user typed a trailing / or the empty string
461: * he wants us to use his file spec as a directory name.
462: */
1.71 kjell 463: if (len && buf[len - 1] != '/') {
1.7 millert 464: file = strrchr(dir, '/');
465: if (file) {
1.69 kjell 466: *file = '\0';
467: if (*dir == '\0')
1.7 millert 468: dir = "/";
1.49 db 469: } else
1.7 millert 470: return (NULL);
1.1 deraadt 471: }
1.7 millert 472: /* Now we get the prefix of the name the user typed. */
1.67 kjell 473: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
474: return (NULL);
1.7 millert 475: cp = strrchr(prefixx, '/');
476: if (cp == NULL)
1.67 kjell 477: prefixx[0] = '\0';
1.7 millert 478: else
1.67 kjell 479: cp[1] = '\0';
1.7 millert 480:
481: preflen = strlen(prefixx);
1.49 db 482: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 483: cp = buf + preflen;
484: len = strlen(cp);
485:
486: /*
487: * Now make sure that file names will fit in the buffers allocated.
488: * SV files are fairly short. For BSD, something more general would
489: * be required.
490: */
1.71 kjell 491: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 492: return (NULL);
493:
494: /* loop over the specified directory, making up the list of files */
495:
496: /*
497: * Note that it is worth our time to filter out names that don't
498: * match, even though our caller is going to do so again, and to
499: * avoid doing the stat if completion is being done, because stat'ing
500: * every file in the directory is relatively expensive.
501: */
1.1 deraadt 502:
1.11 art 503: dirp = opendir(dir);
504: if (dirp == NULL)
1.7 millert 505: return (NULL);
506: last = NULL;
1.11 art 507:
508: while ((dent = readdir(dirp)) != NULL) {
1.18 art 509: int isdir;
1.83 kjell 510: if (strncmp(cp, dent->d_name, len) != 0)
1.11 art 511: continue;
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;
1.78 otto 527: if (S_ISDIR(statbuf.st_mode))
1.18 art 528: isdir = 1;
529: }
530:
1.68 kjell 531: if ((current = malloc(sizeof(struct list))) == NULL) {
532: free_file_list(last);
1.86 lum 533: closedir(dirp);
1.68 kjell 534: return (NULL);
535: }
536: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 537: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 538: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 539: free(current);
1.7 millert 540: continue;
541: }
1.68 kjell 542: current->l_next = last;
543: current->l_name = strdup(fl_name);
544: last = current;
1.1 deraadt 545: }
1.11 art 546: closedir(dirp);
1.1 deraadt 547:
1.7 millert 548: return (last);
1.54 kjell 549: }
550:
551: /*
552: * Test if a supplied filename refers to a directory
553: * Returns ABORT on error, TRUE if directory. FALSE otherwise
554: */
555: int
556: fisdir(const char *fname)
557: {
558: struct stat statbuf;
559:
560: if (stat(fname, &statbuf) != 0)
561: return (ABORT);
562:
563: if (S_ISDIR(statbuf.st_mode))
564: return (TRUE);
565:
566: return (FALSE);
1.82 kjell 567: }
568:
569: /*
570: * Check the mtime of the supplied filename.
571: * Return TRUE if last mtime matches, FALSE if not,
572: * If the stat fails, return TRUE and try the save anyway
573: */
574: int
575: fchecktime(struct buffer *bp)
576: {
577: struct stat sb;
578:
579: if (stat(bp->b_fname, &sb) == -1)
580: return (TRUE);
581:
582: if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
583: bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
584: return (FALSE);
585:
586: return (TRUE);
1.93 jasper 587:
1.91 lum 588: }
589:
590: /*
591: * Location of backup file. This function creates the correct path.
592: */
593: static char *
594: bkuplocation(const char *fn)
595: {
596: struct stat sb;
597: char *ret;
598:
599: if (bkupdir != NULL && (stat(bkupdir, &sb) == 0) &&
600: S_ISDIR(sb.st_mode) && !bkupleavetmp(fn)) {
601: char fname[NFILEN];
602: const char *c;
603: int i = 0, len;
604:
605: c = fn;
606: len = strlen(bkupdir);
607:
608: while (*c != '\0') {
609: /* Make sure we don't go over combined:
610: * strlen(bkupdir + '/' + fname + '\0')
611: */
612: if (i >= NFILEN - len - 1)
613: return (NULL);
614: if (*c == '/') {
615: fname[i] = '!';
616: } else if (*c == '!') {
617: if (i >= NFILEN - len - 2)
618: return (NULL);
619: fname[i++] = '!';
620: fname[i] = '!';
621: } else
622: fname[i] = *c;
623: i++;
624: c++;
625: }
626: fname[i] = '\0';
627: if (asprintf(&ret, "%s/%s", bkupdir, fname) == -1)
628: return (NULL);
629:
630: } else if ((ret = strndup(fn, NFILEN)) == NULL)
631: return (NULL);
632:
633: return (ret);
634: }
635:
636: int
637: backuptohomedir(int f, int n)
638: {
1.93 jasper 639: const char *c = _PATH_MG_DIR;
1.91 lum 640: char *p;
641:
642: if (bkupdir == NULL) {
643: p = adjustname(c, TRUE);
644: bkupdir = strndup(p, NFILEN);
645: if (bkupdir == NULL)
646: return(FALSE);
647:
648: if (mkdir(bkupdir, 0700) == -1 && errno != EEXIST) {
649: free(bkupdir);
650: bkupdir = NULL;
651: }
652: } else {
653: free(bkupdir);
654: bkupdir = NULL;
655: }
656:
657: return (TRUE);
658: }
659:
660: /*
661: * For applications that use mg as the editor and have a desire to keep
662: * '~' files in the TMPDIR, toggle the location: /tmp | ~/.mg.d
663: */
664: int
665: toggleleavetmp(int f, int n)
666: {
667: leavetmp = !leavetmp;
668:
669: return (TRUE);
670: }
671:
672: /*
673: * Returns TRUE if fn is located in the temp directory and we want to save
674: * those backups there.
675: */
676: int
677: bkupleavetmp(const char *fn)
678: {
679: char *tmpdir, *tmp = NULL;
680:
681: if (!leavetmp)
682: return(FALSE);
683:
684: if((tmpdir = getenv("TMPDIR")) != NULL && *tmpdir != '\0') {
685: tmp = strstr(fn, tmpdir);
686: if (tmp == fn)
687: return (TRUE);
688:
689: return (FALSE);
690: }
691:
692: tmp = strstr(fn, "/tmp");
693: if (tmp == fn)
694: return (TRUE);
695:
696: return (FALSE);
1.92 lum 697: }
698:
1.93 jasper 699: /*
1.92 lum 700: * Expand file names beginning with '~' if appropriate:
701: * 1, if ./~fn exists, continue without expanding tilde.
702: * 2, else, if username 'fn' exists, expand tilde with home directory path.
703: * 3, otherwise, continue and create new buffer called ~fn.
704: */
705: char *
706: expandtilde(const char *fn)
707: {
708: struct passwd *pw;
709: struct stat statbuf;
710: const char *cp;
1.94 lum 711: char user[LOGIN_NAME_MAX], path[NFILEN];
712: char *un, *ret;
1.92 lum 713: size_t ulen, plen;
714:
715: path[0] = '\0';
716:
717: if (fn[0] != '~' || stat(fn, &statbuf) == 0) {
718: if ((ret = strndup(fn, NFILEN)) == NULL)
719: return (NULL);
720: return(ret);
721: }
722: cp = strchr(fn, '/');
723: if (cp == NULL)
724: cp = fn + strlen(fn); /* point to the NUL byte */
725: ulen = cp - &fn[1];
726: if (ulen >= sizeof(user)) {
727: if ((ret = strndup(fn, NFILEN)) == NULL)
728: return (NULL);
729: return(ret);
730: }
1.94 lum 731: if (ulen == 0) { /* ~/ or ~ */
732: if ((un = getlogin()) != NULL)
733: (void)strlcpy(user, un, sizeof(user));
734: else
735: user[0] = '\0';
736: } else { /* ~user/ or ~user */
1.92 lum 737: memcpy(user, &fn[1], ulen);
738: user[ulen] = '\0';
739: }
740: pw = getpwnam(user);
741: if (pw != NULL) {
742: plen = strlcpy(path, pw->pw_dir, sizeof(path));
743: if (plen == 0 || path[plen - 1] != '/') {
744: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
1.97 lum 745: dobeep();
1.92 lum 746: ewprintf("Path too long");
747: return (NULL);
748: }
749: }
750: fn = cp;
751: if (*fn == '/')
752: fn++;
753: }
754: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
1.97 lum 755: dobeep();
1.92 lum 756: ewprintf("Path too long");
757: return (NULL);
758: }
759: if ((ret = strndup(path, NFILEN)) == NULL)
760: return (NULL);
761:
762: return (ret);
1.1 deraadt 763: }