Annotation of src/usr.bin/mg/fileio.c, Revision 1.106
1.106 ! lum 1: /* $OpenBSD: fileio.c,v 1.105 2018/04/13 14:11:37 florian 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.106 ! lum 153: ffputbuf(FILE *ffp, struct buffer *bp, int eobnl)
1.1 deraadt 154: {
1.106 ! lum 155: struct line *lp, *lpend;
1.7 millert 156:
1.77 kjell 157: lpend = bp->b_headp;
1.106 ! lum 158:
1.41 vincent 159: for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
160: if (fwrite(ltext(lp), 1, llength(lp), ffp) != llength(lp)) {
1.97 lum 161: dobeep();
1.41 vincent 162: ewprintf("Write I/O error");
1.49 db 163: return (FIOERR);
1.7 millert 164: }
1.41 vincent 165: if (lforw(lp) != lpend) /* no implied \n on last line */
166: putc('\n', ffp);
1.48 deraadt 167: }
1.106 ! lum 168: if (eobnl) {
! 169: lnewline_at(lback(lpend), llength(lback(lpend)));
! 170: putc('\n', ffp);
1.42 vincent 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;
1.104 florian 214: struct timespec new_times[2];
1.49 db 215: int from, to, serrno;
216: ssize_t nread;
217: char buf[BUFSIZ];
1.91 lum 218: char *nname, *tname, *bkpth;
1.1 deraadt 219:
1.35 vincent 220: if (stat(fn, &sb) == -1) {
1.97 lum 221: dobeep();
1.35 vincent 222: ewprintf("Can't stat %s : %s", fn, strerror(errno));
223: return (FALSE);
224: }
225:
1.91 lum 226: if ((bkpth = bkuplocation(fn)) == NULL)
227: return (FALSE);
228:
229: if (asprintf(&nname, "%s~", bkpth) == -1) {
1.97 lum 230: dobeep();
1.90 lum 231: ewprintf("Can't allocate backup file name : %s", strerror(errno));
1.91 lum 232: free(bkpth);
1.44 millert 233: return (ABORT);
234: }
1.91 lum 235: if (asprintf(&tname, "%s.XXXXXXXXXX", bkpth) == -1) {
1.97 lum 236: dobeep();
1.44 millert 237: ewprintf("Can't allocate temp file name : %s", strerror(errno));
1.91 lum 238: free(bkpth);
1.44 millert 239: free(nname);
1.1 deraadt 240: return (ABORT);
1.9 millert 241: }
1.91 lum 242: free(bkpth);
1.9 millert 243:
1.21 vincent 244: if ((from = open(fn, O_RDONLY)) == -1) {
245: free(nname);
1.44 millert 246: free(tname);
1.1 deraadt 247: return (FALSE);
1.21 vincent 248: }
1.44 millert 249: to = mkstemp(tname);
1.9 millert 250: if (to == -1) {
251: serrno = errno;
252: close(from);
1.21 vincent 253: free(nname);
1.44 millert 254: free(tname);
1.9 millert 255: errno = serrno;
256: return (FALSE);
257: }
258: while ((nread = read(from, buf, sizeof(buf))) > 0) {
1.70 deraadt 259: if (write(to, buf, (size_t)nread) != nread) {
1.40 vincent 260: nread = -1;
261: break;
1.9 millert 262: }
1.1 deraadt 263: }
1.9 millert 264: serrno = errno;
1.44 millert 265: (void) fchmod(to, (sb.st_mode & 0777));
1.100 jasper 266:
267: /* copy the mtime to the backupfile */
268: new_times[0] = sb.st_atim;
269: new_times[1] = sb.st_mtim;
270: futimens(to, new_times);
271:
1.9 millert 272: close(from);
273: close(to);
1.21 vincent 274: if (nread == -1) {
1.44 millert 275: if (unlink(tname) == -1)
1.21 vincent 276: ewprintf("Can't unlink temp : %s", strerror(errno));
1.44 millert 277: } else {
278: if (rename(tname, nname) == -1) {
279: ewprintf("Can't rename temp : %s", strerror(errno));
280: (void) unlink(tname);
1.45 henning 281: nread = -1;
1.44 millert 282: }
1.21 vincent 283: }
1.1 deraadt 284: free(nname);
1.44 millert 285: free(tname);
1.9 millert 286: errno = serrno;
1.22 deraadt 287:
1.9 millert 288: return (nread == -1 ? FALSE : TRUE);
1.1 deraadt 289: }
290:
291: /*
1.73 kjell 292: * Convert "fn" to a canonicalized absolute filename, replacing
293: * a leading ~/ with the user's home dir, following symlinks, and
1.96 lum 294: * remove all occurrences of /./ and /../
1.1 deraadt 295: */
1.7 millert 296: char *
1.74 jason 297: adjustname(const char *fn, int slashslash)
1.1 deraadt 298: {
1.98 guenther 299: static char fnb[PATH_MAX];
1.74 jason 300: const char *cp, *ep = NULL;
1.92 lum 301: char *path;
1.74 jason 302:
1.75 kjell 303: if (slashslash == TRUE) {
1.74 jason 304: cp = fn + strlen(fn) - 1;
305: for (; cp >= fn; cp--) {
306: if (ep && (*cp == '/')) {
307: fn = ep;
308: break;
309: }
310: if (*cp == '/' || *cp == '~')
311: ep = cp;
312: else
313: ep = NULL;
314: }
315: }
1.92 lum 316: if ((path = expandtilde(fn)) == NULL)
1.57 kjell 317: return (NULL);
1.33 vincent 318:
1.47 henning 319: if (realpath(path, fnb) == NULL)
1.57 kjell 320: (void)strlcpy(fnb, path, sizeof(fnb));
1.47 henning 321:
1.92 lum 322: free(path);
1.47 henning 323: return (fnb);
1.1 deraadt 324: }
325:
326: /*
327: * Find a startup file for the user and return its name. As a service
328: * to other pieces of code that may want to find a startup file (like
329: * the terminal driver in particular), accepts a suffix to be appended
330: * to the startup file name.
331: */
332: char *
1.27 millert 333: startupfile(char *suffix)
1.1 deraadt 334: {
1.49 db 335: static char file[NFILEN];
1.16 mickey 336: char *home;
1.61 kjell 337: int ret;
1.12 art 338:
339: if ((home = getenv("HOME")) == NULL || *home == '\0')
340: goto nohome;
1.1 deraadt 341:
1.12 art 342: if (suffix == NULL) {
1.93 jasper 343: ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
1.61 kjell 344: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 345: return (NULL);
1.12 art 346: } else {
1.93 jasper 347: ret = snprintf(file, sizeof(file), _PATH_MG_TERM, home, suffix);
1.61 kjell 348: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 349: return (NULL);
1.1 deraadt 350: }
1.12 art 351:
352: if (access(file, R_OK) == 0)
1.32 vincent 353: return (file);
1.12 art 354: nohome:
355: #ifdef STARTUPFILE
1.27 millert 356: if (suffix == NULL) {
1.61 kjell 357: ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
358: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 359: return (NULL);
1.12 art 360: } else {
1.61 kjell 361: ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
362: suffix);
363: if (ret < 0 || ret >= sizeof(file))
1.32 vincent 364: return (NULL);
1.12 art 365: }
366:
367: if (access(file, R_OK) == 0)
1.32 vincent 368: return (file);
1.61 kjell 369: #endif /* STARTUPFILE */
1.32 vincent 370: return (NULL);
1.1 deraadt 371: }
372:
1.7 millert 373: int
1.27 millert 374: copy(char *frname, char *toname)
1.1 deraadt 375: {
1.70 deraadt 376: int ifd, ofd;
1.49 db 377: char buf[BUFSIZ];
1.66 kjell 378: mode_t fmode = DEFFILEMODE; /* XXX?? */
1.49 db 379: struct stat orig;
1.70 deraadt 380: ssize_t sr;
1.30 vincent 381:
382: if ((ifd = open(frname, O_RDONLY)) == -1)
383: return (FALSE);
384: if (fstat(ifd, &orig) == -1) {
1.97 lum 385: dobeep();
1.30 vincent 386: ewprintf("fstat: %s", strerror(errno));
387: close(ifd);
388: return (FALSE);
389: }
390:
1.66 kjell 391: if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30 vincent 392: close(ifd);
393: return (FALSE);
394: }
1.70 deraadt 395: while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
396: if (write(ofd, buf, (size_t)sr) != sr) {
1.30 vincent 397: ewprintf("write error : %s", strerror(errno));
398: break;
399: }
400: }
401: if (fchmod(ofd, orig.st_mode) == -1)
402: ewprintf("Cannot set original mode : %s", strerror(errno));
1.1 deraadt 403:
1.70 deraadt 404: if (sr == -1) {
1.30 vincent 405: ewprintf("Read error : %s", strerror(errno));
406: close(ifd);
407: close(ofd);
408: return (FALSE);
1.7 millert 409: }
1.30 vincent 410: /*
1.32 vincent 411: * It is "normal" for this to fail since we can't guarantee that
1.49 db 412: * we will be running as root.
1.30 vincent 413: */
414: if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
415: ewprintf("Cannot set owner : %s", strerror(errno));
416:
417: (void) close(ifd);
418: (void) close(ofd);
419:
420: return (TRUE);
1.7 millert 421: }
1.1 deraadt 422:
1.7 millert 423: /*
1.1 deraadt 424: * return list of file names that match the name in buf.
425: */
1.62 deraadt 426: struct list *
1.25 vincent 427: make_file_list(char *buf)
1.7 millert 428: {
1.16 mickey 429: char *dir, *file, *cp;
1.71 kjell 430: size_t len, preflen;
431: int ret;
1.16 mickey 432: DIR *dirp;
433: struct dirent *dent;
1.68 kjell 434: struct list *last, *current;
435: char fl_name[NFILEN + 2];
1.49 db 436: char prefixx[NFILEN + 1];
1.7 millert 437:
438: /*
1.79 deraadt 439: * We need three different strings:
1.72 kjell 440:
441: * dir - the name of the directory containing what the user typed.
442: * Must be a real unix file name, e.g. no ~user, etc..
443: * Must not end in /.
444: * prefix - the portion of what the user typed that is before the
445: * names we are going to find in the directory. Must have a
446: * trailing / if the user typed it.
447: * names from the directory - We open dir, and return prefix
1.7 millert 448: * concatenated with names.
449: */
450:
451: /* first we get a directory name we can look up */
452: /*
453: * Names ending in . are potentially odd, because adjustname will
1.71 kjell 454: * treat foo/bar/.. as a foo/, whereas we are
1.7 millert 455: * interested in names starting with ..
456: */
457: len = strlen(buf);
1.71 kjell 458: if (len && buf[len - 1] == '.') {
1.7 millert 459: buf[len - 1] = 'x';
1.74 jason 460: dir = adjustname(buf, TRUE);
1.7 millert 461: buf[len - 1] = '.';
462: } else
1.74 jason 463: dir = adjustname(buf, TRUE);
1.33 vincent 464: if (dir == NULL)
1.36 vincent 465: return (NULL);
1.7 millert 466: /*
467: * If the user typed a trailing / or the empty string
468: * he wants us to use his file spec as a directory name.
469: */
1.71 kjell 470: if (len && buf[len - 1] != '/') {
1.7 millert 471: file = strrchr(dir, '/');
472: if (file) {
1.69 kjell 473: *file = '\0';
474: if (*dir == '\0')
1.7 millert 475: dir = "/";
1.49 db 476: } else
1.7 millert 477: return (NULL);
1.1 deraadt 478: }
1.7 millert 479: /* Now we get the prefix of the name the user typed. */
1.67 kjell 480: if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
481: return (NULL);
1.7 millert 482: cp = strrchr(prefixx, '/');
483: if (cp == NULL)
1.67 kjell 484: prefixx[0] = '\0';
1.7 millert 485: else
1.67 kjell 486: cp[1] = '\0';
1.7 millert 487:
488: preflen = strlen(prefixx);
1.49 db 489: /* cp is the tail of buf that really needs to be compared. */
1.7 millert 490: cp = buf + preflen;
491: len = strlen(cp);
492:
493: /*
494: * Now make sure that file names will fit in the buffers allocated.
495: * SV files are fairly short. For BSD, something more general would
496: * be required.
497: */
1.71 kjell 498: if (preflen > NFILEN - MAXNAMLEN)
1.7 millert 499: return (NULL);
500:
501: /* loop over the specified directory, making up the list of files */
502:
503: /*
504: * Note that it is worth our time to filter out names that don't
505: * match, even though our caller is going to do so again, and to
506: * avoid doing the stat if completion is being done, because stat'ing
507: * every file in the directory is relatively expensive.
508: */
1.1 deraadt 509:
1.11 art 510: dirp = opendir(dir);
511: if (dirp == NULL)
1.7 millert 512: return (NULL);
513: last = NULL;
1.11 art 514:
515: while ((dent = readdir(dirp)) != NULL) {
1.18 art 516: int isdir;
1.83 kjell 517: if (strncmp(cp, dent->d_name, len) != 0)
1.11 art 518: continue;
1.18 art 519: isdir = 0;
520: if (dent->d_type == DT_DIR) {
521: isdir = 1;
522: } else if (dent->d_type == DT_LNK ||
523: dent->d_type == DT_UNKNOWN) {
524: struct stat statbuf;
525:
1.101 guenther 526: if (fstatat(dirfd(dirp), dent->d_name, &statbuf, 0) < 0)
1.18 art 527: continue;
1.78 otto 528: if (S_ISDIR(statbuf.st_mode))
1.18 art 529: isdir = 1;
530: }
531:
1.68 kjell 532: if ((current = malloc(sizeof(struct list))) == NULL) {
533: free_file_list(last);
1.86 lum 534: closedir(dirp);
1.68 kjell 535: return (NULL);
536: }
537: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 538: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 539: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 540: free(current);
1.7 millert 541: continue;
542: }
1.68 kjell 543: current->l_next = last;
544: current->l_name = strdup(fl_name);
545: last = current;
1.1 deraadt 546: }
1.11 art 547: closedir(dirp);
1.1 deraadt 548:
1.7 millert 549: return (last);
1.54 kjell 550: }
551:
552: /*
553: * Test if a supplied filename refers to a directory
554: * Returns ABORT on error, TRUE if directory. FALSE otherwise
555: */
556: int
557: fisdir(const char *fname)
558: {
559: struct stat statbuf;
560:
561: if (stat(fname, &statbuf) != 0)
562: return (ABORT);
563:
564: if (S_ISDIR(statbuf.st_mode))
565: return (TRUE);
566:
567: return (FALSE);
1.82 kjell 568: }
569:
570: /*
571: * Check the mtime of the supplied filename.
572: * Return TRUE if last mtime matches, FALSE if not,
573: * If the stat fails, return TRUE and try the save anyway
574: */
575: int
576: fchecktime(struct buffer *bp)
577: {
578: struct stat sb;
579:
580: if (stat(bp->b_fname, &sb) == -1)
581: return (TRUE);
582:
583: if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
584: bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
585: return (FALSE);
586:
587: return (TRUE);
1.93 jasper 588:
1.91 lum 589: }
590:
591: /*
592: * Location of backup file. This function creates the correct path.
593: */
594: static char *
595: bkuplocation(const char *fn)
596: {
597: struct stat sb;
598: char *ret;
599:
600: if (bkupdir != NULL && (stat(bkupdir, &sb) == 0) &&
601: S_ISDIR(sb.st_mode) && !bkupleavetmp(fn)) {
602: char fname[NFILEN];
603: const char *c;
604: int i = 0, len;
605:
606: c = fn;
607: len = strlen(bkupdir);
608:
609: while (*c != '\0') {
610: /* Make sure we don't go over combined:
611: * strlen(bkupdir + '/' + fname + '\0')
612: */
613: if (i >= NFILEN - len - 1)
614: return (NULL);
615: if (*c == '/') {
616: fname[i] = '!';
617: } else if (*c == '!') {
618: if (i >= NFILEN - len - 2)
619: return (NULL);
620: fname[i++] = '!';
621: fname[i] = '!';
622: } else
623: fname[i] = *c;
624: i++;
625: c++;
626: }
627: fname[i] = '\0';
628: if (asprintf(&ret, "%s/%s", bkupdir, fname) == -1)
629: return (NULL);
630:
631: } else if ((ret = strndup(fn, NFILEN)) == NULL)
632: return (NULL);
633:
634: return (ret);
635: }
636:
637: int
638: backuptohomedir(int f, int n)
639: {
1.93 jasper 640: const char *c = _PATH_MG_DIR;
1.91 lum 641: char *p;
642:
643: if (bkupdir == NULL) {
644: p = adjustname(c, TRUE);
645: bkupdir = strndup(p, NFILEN);
646: if (bkupdir == NULL)
647: return(FALSE);
648:
649: if (mkdir(bkupdir, 0700) == -1 && errno != EEXIST) {
650: free(bkupdir);
651: bkupdir = NULL;
652: }
653: } else {
654: free(bkupdir);
655: bkupdir = NULL;
656: }
657:
658: return (TRUE);
659: }
660:
661: /*
662: * For applications that use mg as the editor and have a desire to keep
1.102 tedu 663: * '~' files in /tmp, toggle the location: /tmp | ~/.mg.d
1.91 lum 664: */
665: int
666: toggleleavetmp(int f, int n)
667: {
668: leavetmp = !leavetmp;
669:
670: return (TRUE);
671: }
672:
673: /*
674: * Returns TRUE if fn is located in the temp directory and we want to save
675: * those backups there.
676: */
677: int
678: bkupleavetmp(const char *fn)
679: {
680: if (!leavetmp)
681: return(FALSE);
682:
1.103 tedu 683: if (strncmp(fn, "/tmp", 4) == 0)
1.91 lum 684: return (TRUE);
685:
686: return (FALSE);
1.92 lum 687: }
688:
1.93 jasper 689: /*
1.92 lum 690: * Expand file names beginning with '~' if appropriate:
691: * 1, if ./~fn exists, continue without expanding tilde.
692: * 2, else, if username 'fn' exists, expand tilde with home directory path.
693: * 3, otherwise, continue and create new buffer called ~fn.
694: */
695: char *
696: expandtilde(const char *fn)
697: {
698: struct passwd *pw;
699: struct stat statbuf;
700: const char *cp;
1.94 lum 701: char user[LOGIN_NAME_MAX], path[NFILEN];
1.105 florian 702: char *ret;
1.92 lum 703: size_t ulen, plen;
704:
705: path[0] = '\0';
706:
707: if (fn[0] != '~' || stat(fn, &statbuf) == 0) {
708: if ((ret = strndup(fn, NFILEN)) == NULL)
709: return (NULL);
710: return(ret);
711: }
712: cp = strchr(fn, '/');
713: if (cp == NULL)
714: cp = fn + strlen(fn); /* point to the NUL byte */
715: ulen = cp - &fn[1];
716: if (ulen >= sizeof(user)) {
717: if ((ret = strndup(fn, NFILEN)) == NULL)
718: return (NULL);
719: return(ret);
720: }
1.105 florian 721: if (ulen == 0) /* ~/ or ~ */
722: pw = getpwuid(geteuid());
723: else { /* ~user/ or ~user */
1.92 lum 724: memcpy(user, &fn[1], ulen);
725: user[ulen] = '\0';
1.105 florian 726: pw = getpwnam(user);
1.92 lum 727: }
728: if (pw != NULL) {
729: plen = strlcpy(path, pw->pw_dir, sizeof(path));
730: if (plen == 0 || path[plen - 1] != '/') {
731: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
1.97 lum 732: dobeep();
1.92 lum 733: ewprintf("Path too long");
734: return (NULL);
735: }
736: }
737: fn = cp;
738: if (*fn == '/')
739: fn++;
740: }
741: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
1.97 lum 742: dobeep();
1.92 lum 743: ewprintf("Path too long");
744: return (NULL);
745: }
746: if ((ret = strndup(path, NFILEN)) == NULL)
747: return (NULL);
748:
749: return (ret);
1.1 deraadt 750: }