Annotation of src/usr.bin/mg/fileio.c, Revision 1.102
1.102 ! tedu 1: /* $OpenBSD: fileio.c,v 1.101 2016/07/04 03:24:48 guenther 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:
1.101 guenther 530: if (fstatat(dirfd(dirp), dent->d_name, &statbuf, 0) < 0)
1.18 art 531: continue;
1.78 otto 532: if (S_ISDIR(statbuf.st_mode))
1.18 art 533: isdir = 1;
534: }
535:
1.68 kjell 536: if ((current = malloc(sizeof(struct list))) == NULL) {
537: free_file_list(last);
1.86 lum 538: closedir(dirp);
1.68 kjell 539: return (NULL);
540: }
541: ret = snprintf(fl_name, sizeof(fl_name),
1.61 kjell 542: "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68 kjell 543: if (ret < 0 || ret >= sizeof(fl_name)) {
1.11 art 544: free(current);
1.7 millert 545: continue;
546: }
1.68 kjell 547: current->l_next = last;
548: current->l_name = strdup(fl_name);
549: last = current;
1.1 deraadt 550: }
1.11 art 551: closedir(dirp);
1.1 deraadt 552:
1.7 millert 553: return (last);
1.54 kjell 554: }
555:
556: /*
557: * Test if a supplied filename refers to a directory
558: * Returns ABORT on error, TRUE if directory. FALSE otherwise
559: */
560: int
561: fisdir(const char *fname)
562: {
563: struct stat statbuf;
564:
565: if (stat(fname, &statbuf) != 0)
566: return (ABORT);
567:
568: if (S_ISDIR(statbuf.st_mode))
569: return (TRUE);
570:
571: return (FALSE);
1.82 kjell 572: }
573:
574: /*
575: * Check the mtime of the supplied filename.
576: * Return TRUE if last mtime matches, FALSE if not,
577: * If the stat fails, return TRUE and try the save anyway
578: */
579: int
580: fchecktime(struct buffer *bp)
581: {
582: struct stat sb;
583:
584: if (stat(bp->b_fname, &sb) == -1)
585: return (TRUE);
586:
587: if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
588: bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
589: return (FALSE);
590:
591: return (TRUE);
1.93 jasper 592:
1.91 lum 593: }
594:
595: /*
596: * Location of backup file. This function creates the correct path.
597: */
598: static char *
599: bkuplocation(const char *fn)
600: {
601: struct stat sb;
602: char *ret;
603:
604: if (bkupdir != NULL && (stat(bkupdir, &sb) == 0) &&
605: S_ISDIR(sb.st_mode) && !bkupleavetmp(fn)) {
606: char fname[NFILEN];
607: const char *c;
608: int i = 0, len;
609:
610: c = fn;
611: len = strlen(bkupdir);
612:
613: while (*c != '\0') {
614: /* Make sure we don't go over combined:
615: * strlen(bkupdir + '/' + fname + '\0')
616: */
617: if (i >= NFILEN - len - 1)
618: return (NULL);
619: if (*c == '/') {
620: fname[i] = '!';
621: } else if (*c == '!') {
622: if (i >= NFILEN - len - 2)
623: return (NULL);
624: fname[i++] = '!';
625: fname[i] = '!';
626: } else
627: fname[i] = *c;
628: i++;
629: c++;
630: }
631: fname[i] = '\0';
632: if (asprintf(&ret, "%s/%s", bkupdir, fname) == -1)
633: return (NULL);
634:
635: } else if ((ret = strndup(fn, NFILEN)) == NULL)
636: return (NULL);
637:
638: return (ret);
639: }
640:
641: int
642: backuptohomedir(int f, int n)
643: {
1.93 jasper 644: const char *c = _PATH_MG_DIR;
1.91 lum 645: char *p;
646:
647: if (bkupdir == NULL) {
648: p = adjustname(c, TRUE);
649: bkupdir = strndup(p, NFILEN);
650: if (bkupdir == NULL)
651: return(FALSE);
652:
653: if (mkdir(bkupdir, 0700) == -1 && errno != EEXIST) {
654: free(bkupdir);
655: bkupdir = NULL;
656: }
657: } else {
658: free(bkupdir);
659: bkupdir = NULL;
660: }
661:
662: return (TRUE);
663: }
664:
665: /*
666: * For applications that use mg as the editor and have a desire to keep
1.102 ! tedu 667: * '~' files in /tmp, toggle the location: /tmp | ~/.mg.d
1.91 lum 668: */
669: int
670: toggleleavetmp(int f, int n)
671: {
672: leavetmp = !leavetmp;
673:
674: return (TRUE);
675: }
676:
677: /*
678: * Returns TRUE if fn is located in the temp directory and we want to save
679: * those backups there.
680: */
681: int
682: bkupleavetmp(const char *fn)
683: {
1.102 ! tedu 684: char *tmp = NULL;
1.91 lum 685:
686: if (!leavetmp)
687: return(FALSE);
688:
689: tmp = strstr(fn, "/tmp");
690: if (tmp == fn)
691: return (TRUE);
692:
693: return (FALSE);
1.92 lum 694: }
695:
1.93 jasper 696: /*
1.92 lum 697: * Expand file names beginning with '~' if appropriate:
698: * 1, if ./~fn exists, continue without expanding tilde.
699: * 2, else, if username 'fn' exists, expand tilde with home directory path.
700: * 3, otherwise, continue and create new buffer called ~fn.
701: */
702: char *
703: expandtilde(const char *fn)
704: {
705: struct passwd *pw;
706: struct stat statbuf;
707: const char *cp;
1.94 lum 708: char user[LOGIN_NAME_MAX], path[NFILEN];
709: char *un, *ret;
1.92 lum 710: size_t ulen, plen;
711:
712: path[0] = '\0';
713:
714: if (fn[0] != '~' || stat(fn, &statbuf) == 0) {
715: if ((ret = strndup(fn, NFILEN)) == NULL)
716: return (NULL);
717: return(ret);
718: }
719: cp = strchr(fn, '/');
720: if (cp == NULL)
721: cp = fn + strlen(fn); /* point to the NUL byte */
722: ulen = cp - &fn[1];
723: if (ulen >= sizeof(user)) {
724: if ((ret = strndup(fn, NFILEN)) == NULL)
725: return (NULL);
726: return(ret);
727: }
1.94 lum 728: if (ulen == 0) { /* ~/ or ~ */
729: if ((un = getlogin()) != NULL)
730: (void)strlcpy(user, un, sizeof(user));
731: else
732: user[0] = '\0';
733: } else { /* ~user/ or ~user */
1.92 lum 734: memcpy(user, &fn[1], ulen);
735: user[ulen] = '\0';
736: }
737: pw = getpwnam(user);
738: if (pw != NULL) {
739: plen = strlcpy(path, pw->pw_dir, sizeof(path));
740: if (plen == 0 || path[plen - 1] != '/') {
741: if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
1.97 lum 742: dobeep();
1.92 lum 743: ewprintf("Path too long");
744: return (NULL);
745: }
746: }
747: fn = cp;
748: if (*fn == '/')
749: fn++;
750: }
751: if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
1.97 lum 752: dobeep();
1.92 lum 753: ewprintf("Path too long");
754: return (NULL);
755: }
756: if ((ret = strndup(path, NFILEN)) == NULL)
757: return (NULL);
758:
759: return (ret);
1.1 deraadt 760: }