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