[BACK]Return to fileio.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/fileio.c, Revision 1.110

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