[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.107

1.107   ! lum         1: /*     $OpenBSD: fileio.c,v 1.106 2019/06/22 10:21:57 lum Exp $        */
1.50      kjell       2:
                      3: /* This file is in the public domain. */
1.10      niklas      4:
1.1       deraadt     5: /*
1.7       millert     6:  *     POSIX fileio.c
1.1       deraadt     7:  */
1.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.107   ! lum       333: startupfile(char *suffix, char *conffile)
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.107   ! lum       342:        if (conffile != NULL) {
        !           343:                (void)strncpy(file, conffile, NFILEN);
        !           344:        } else if (suffix == NULL) {
1.93      jasper    345:                ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
1.61      kjell     346:                if (ret < 0 || ret >= sizeof(file))
1.32      vincent   347:                        return (NULL);
1.12      art       348:        } else {
1.93      jasper    349:                ret = snprintf(file, sizeof(file), _PATH_MG_TERM, home, suffix);
1.61      kjell     350:                if (ret < 0 || ret >= sizeof(file))
1.32      vincent   351:                        return (NULL);
1.1       deraadt   352:        }
1.12      art       353:
                    354:        if (access(file, R_OK) == 0)
1.32      vincent   355:                return (file);
1.12      art       356: nohome:
                    357: #ifdef STARTUPFILE
1.27      millert   358:        if (suffix == NULL) {
1.61      kjell     359:                ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
                    360:                if (ret < 0 || ret >= sizeof(file))
1.32      vincent   361:                        return (NULL);
1.12      art       362:        } else {
1.61      kjell     363:                ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
                    364:                    suffix);
                    365:                if (ret < 0 || ret >= sizeof(file))
1.32      vincent   366:                        return (NULL);
1.12      art       367:        }
                    368:
                    369:        if (access(file, R_OK) == 0)
1.32      vincent   370:                return (file);
1.61      kjell     371: #endif /* STARTUPFILE */
1.32      vincent   372:        return (NULL);
1.1       deraadt   373: }
                    374:
1.7       millert   375: int
1.27      millert   376: copy(char *frname, char *toname)
1.1       deraadt   377: {
1.70      deraadt   378:        int     ifd, ofd;
1.49      db        379:        char    buf[BUFSIZ];
1.66      kjell     380:        mode_t  fmode = DEFFILEMODE;    /* XXX?? */
1.49      db        381:        struct  stat orig;
1.70      deraadt   382:        ssize_t sr;
1.30      vincent   383:
                    384:        if ((ifd = open(frname, O_RDONLY)) == -1)
                    385:                return (FALSE);
                    386:        if (fstat(ifd, &orig) == -1) {
1.97      lum       387:                dobeep();
1.30      vincent   388:                ewprintf("fstat: %s", strerror(errno));
                    389:                close(ifd);
                    390:                return (FALSE);
                    391:        }
                    392:
1.66      kjell     393:        if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, fmode)) == -1) {
1.30      vincent   394:                close(ifd);
                    395:                return (FALSE);
                    396:        }
1.70      deraadt   397:        while ((sr = read(ifd, buf, sizeof(buf))) > 0) {
                    398:                if (write(ofd, buf, (size_t)sr) != sr) {
1.30      vincent   399:                        ewprintf("write error : %s", strerror(errno));
                    400:                        break;
                    401:                }
                    402:        }
                    403:        if (fchmod(ofd, orig.st_mode) == -1)
                    404:                ewprintf("Cannot set original mode : %s", strerror(errno));
1.1       deraadt   405:
1.70      deraadt   406:        if (sr == -1) {
1.30      vincent   407:                ewprintf("Read error : %s", strerror(errno));
                    408:                close(ifd);
                    409:                close(ofd);
                    410:                return (FALSE);
1.7       millert   411:        }
1.30      vincent   412:        /*
1.32      vincent   413:         * It is "normal" for this to fail since we can't guarantee that
1.49      db        414:         * we will be running as root.
1.30      vincent   415:         */
                    416:        if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
                    417:                ewprintf("Cannot set owner : %s", strerror(errno));
                    418:
                    419:        (void) close(ifd);
                    420:        (void) close(ofd);
                    421:
                    422:        return (TRUE);
1.7       millert   423: }
1.1       deraadt   424:
1.7       millert   425: /*
1.1       deraadt   426:  * return list of file names that match the name in buf.
                    427:  */
1.62      deraadt   428: struct list *
1.25      vincent   429: make_file_list(char *buf)
1.7       millert   430: {
1.16      mickey    431:        char            *dir, *file, *cp;
1.71      kjell     432:        size_t           len, preflen;
                    433:        int              ret;
1.16      mickey    434:        DIR             *dirp;
                    435:        struct dirent   *dent;
1.68      kjell     436:        struct list     *last, *current;
                    437:        char             fl_name[NFILEN + 2];
1.49      db        438:        char             prefixx[NFILEN + 1];
1.7       millert   439:
                    440:        /*
1.79      deraadt   441:         * We need three different strings:
1.72      kjell     442:
                    443:         * dir - the name of the directory containing what the user typed.
                    444:         *  Must be a real unix file name, e.g. no ~user, etc..
                    445:         *  Must not end in /.
                    446:         * prefix - the portion of what the user typed that is before the
                    447:         *  names we are going to find in the directory.  Must have a
                    448:         * trailing / if the user typed it.
                    449:         * names from the directory - We open dir, and return prefix
1.7       millert   450:         * concatenated with names.
                    451:         */
                    452:
                    453:        /* first we get a directory name we can look up */
                    454:        /*
                    455:         * Names ending in . are potentially odd, because adjustname will
1.71      kjell     456:         * treat foo/bar/.. as a foo/, whereas we are
1.7       millert   457:         * interested in names starting with ..
                    458:         */
                    459:        len = strlen(buf);
1.71      kjell     460:        if (len && buf[len - 1] == '.') {
1.7       millert   461:                buf[len - 1] = 'x';
1.74      jason     462:                dir = adjustname(buf, TRUE);
1.7       millert   463:                buf[len - 1] = '.';
                    464:        } else
1.74      jason     465:                dir = adjustname(buf, TRUE);
1.33      vincent   466:        if (dir == NULL)
1.36      vincent   467:                return (NULL);
1.7       millert   468:        /*
                    469:         * If the user typed a trailing / or the empty string
                    470:         * he wants us to use his file spec as a directory name.
                    471:         */
1.71      kjell     472:        if (len && buf[len - 1] != '/') {
1.7       millert   473:                file = strrchr(dir, '/');
                    474:                if (file) {
1.69      kjell     475:                        *file = '\0';
                    476:                        if (*dir == '\0')
1.7       millert   477:                                dir = "/";
1.49      db        478:                } else
1.7       millert   479:                        return (NULL);
1.1       deraadt   480:        }
1.7       millert   481:        /* Now we get the prefix of the name the user typed. */
1.67      kjell     482:        if (strlcpy(prefixx, buf, sizeof(prefixx)) >= sizeof(prefixx))
                    483:                return (NULL);
1.7       millert   484:        cp = strrchr(prefixx, '/');
                    485:        if (cp == NULL)
1.67      kjell     486:                prefixx[0] = '\0';
1.7       millert   487:        else
1.67      kjell     488:                cp[1] = '\0';
1.7       millert   489:
                    490:        preflen = strlen(prefixx);
1.49      db        491:        /* cp is the tail of buf that really needs to be compared. */
1.7       millert   492:        cp = buf + preflen;
                    493:        len = strlen(cp);
                    494:
                    495:        /*
                    496:         * Now make sure that file names will fit in the buffers allocated.
                    497:         * SV files are fairly short.  For BSD, something more general would
                    498:         * be required.
                    499:         */
1.71      kjell     500:        if (preflen > NFILEN - MAXNAMLEN)
1.7       millert   501:                return (NULL);
                    502:
                    503:        /* loop over the specified directory, making up the list of files */
                    504:
                    505:        /*
                    506:         * Note that it is worth our time to filter out names that don't
                    507:         * match, even though our caller is going to do so again, and to
                    508:         * avoid doing the stat if completion is being done, because stat'ing
                    509:         * every file in the directory is relatively expensive.
                    510:         */
1.1       deraadt   511:
1.11      art       512:        dirp = opendir(dir);
                    513:        if (dirp == NULL)
1.7       millert   514:                return (NULL);
                    515:        last = NULL;
1.11      art       516:
                    517:        while ((dent = readdir(dirp)) != NULL) {
1.18      art       518:                int isdir;
1.83      kjell     519:                if (strncmp(cp, dent->d_name, len) != 0)
1.11      art       520:                        continue;
1.18      art       521:                isdir = 0;
                    522:                if (dent->d_type == DT_DIR) {
                    523:                        isdir = 1;
                    524:                } else if (dent->d_type == DT_LNK ||
                    525:                            dent->d_type == DT_UNKNOWN) {
                    526:                        struct stat     statbuf;
                    527:
1.101     guenther  528:                        if (fstatat(dirfd(dirp), dent->d_name, &statbuf, 0) < 0)
1.18      art       529:                                continue;
1.78      otto      530:                        if (S_ISDIR(statbuf.st_mode))
1.18      art       531:                                isdir = 1;
                    532:                }
                    533:
1.68      kjell     534:                if ((current = malloc(sizeof(struct list))) == NULL) {
                    535:                        free_file_list(last);
1.86      lum       536:                        closedir(dirp);
1.68      kjell     537:                        return (NULL);
                    538:                }
                    539:                ret = snprintf(fl_name, sizeof(fl_name),
1.61      kjell     540:                    "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
1.68      kjell     541:                if (ret < 0 || ret >= sizeof(fl_name)) {
1.11      art       542:                        free(current);
1.7       millert   543:                        continue;
                    544:                }
1.68      kjell     545:                current->l_next = last;
                    546:                current->l_name = strdup(fl_name);
                    547:                last = current;
1.1       deraadt   548:        }
1.11      art       549:        closedir(dirp);
1.1       deraadt   550:
1.7       millert   551:        return (last);
1.54      kjell     552: }
                    553:
                    554: /*
                    555:  * Test if a supplied filename refers to a directory
                    556:  * Returns ABORT on error, TRUE if directory. FALSE otherwise
                    557:  */
                    558: int
                    559: fisdir(const char *fname)
                    560: {
                    561:        struct stat     statbuf;
                    562:
                    563:        if (stat(fname, &statbuf) != 0)
                    564:                return (ABORT);
                    565:
                    566:        if (S_ISDIR(statbuf.st_mode))
                    567:                return (TRUE);
                    568:
                    569:        return (FALSE);
1.82      kjell     570: }
                    571:
                    572: /*
                    573:  * Check the mtime of the supplied filename.
                    574:  * Return TRUE if last mtime matches, FALSE if not,
                    575:  * If the stat fails, return TRUE and try the save anyway
                    576:  */
                    577: int
                    578: fchecktime(struct buffer *bp)
                    579: {
                    580:        struct stat sb;
                    581:
                    582:        if (stat(bp->b_fname, &sb) == -1)
                    583:                return (TRUE);
                    584:
                    585:        if (bp->b_fi.fi_mtime.tv_sec != sb.st_mtimespec.tv_sec ||
                    586:            bp->b_fi.fi_mtime.tv_nsec != sb.st_mtimespec.tv_nsec)
                    587:                return (FALSE);
                    588:
                    589:        return (TRUE);
1.93      jasper    590:
1.91      lum       591: }
                    592:
                    593: /*
                    594:  * Location of backup file. This function creates the correct path.
                    595:  */
                    596: static char *
                    597: bkuplocation(const char *fn)
                    598: {
                    599:        struct stat sb;
                    600:        char *ret;
                    601:
                    602:        if (bkupdir != NULL && (stat(bkupdir, &sb) == 0) &&
                    603:            S_ISDIR(sb.st_mode) && !bkupleavetmp(fn)) {
                    604:                char fname[NFILEN];
                    605:                const char *c;
                    606:                int i = 0, len;
                    607:
                    608:                c = fn;
                    609:                len = strlen(bkupdir);
                    610:
                    611:                while (*c != '\0') {
                    612:                        /* Make sure we don't go over combined:
                    613:                        * strlen(bkupdir + '/' + fname + '\0')
                    614:                        */
                    615:                        if (i >= NFILEN - len - 1)
                    616:                                return (NULL);
                    617:                        if (*c == '/') {
                    618:                                fname[i] = '!';
                    619:                        } else if (*c == '!') {
                    620:                                if (i >= NFILEN - len - 2)
                    621:                                        return (NULL);
                    622:                                fname[i++] = '!';
                    623:                                fname[i] = '!';
                    624:                        } else
                    625:                                fname[i] = *c;
                    626:                        i++;
                    627:                        c++;
                    628:                }
                    629:                fname[i] = '\0';
                    630:                if (asprintf(&ret, "%s/%s", bkupdir, fname) == -1)
                    631:                        return (NULL);
                    632:
                    633:        } else if ((ret = strndup(fn, NFILEN)) == NULL)
                    634:                return (NULL);
                    635:
                    636:        return (ret);
                    637: }
                    638:
                    639: int
                    640: backuptohomedir(int f, int n)
                    641: {
1.93      jasper    642:        const char      *c = _PATH_MG_DIR;
1.91      lum       643:        char            *p;
                    644:
                    645:        if (bkupdir == NULL) {
                    646:                p = adjustname(c, TRUE);
                    647:                bkupdir = strndup(p, NFILEN);
                    648:                if (bkupdir == NULL)
                    649:                        return(FALSE);
                    650:
                    651:                if (mkdir(bkupdir, 0700) == -1 && errno != EEXIST) {
                    652:                        free(bkupdir);
                    653:                        bkupdir = NULL;
                    654:                }
                    655:        } else {
                    656:                free(bkupdir);
                    657:                bkupdir = NULL;
                    658:        }
                    659:
                    660:        return (TRUE);
                    661: }
                    662:
                    663: /*
                    664:  * For applications that use mg as the editor and have a desire to keep
1.102     tedu      665:  * '~' files in /tmp, toggle the location: /tmp | ~/.mg.d
1.91      lum       666:  */
                    667: int
                    668: toggleleavetmp(int f, int n)
                    669: {
                    670:        leavetmp = !leavetmp;
                    671:
                    672:        return (TRUE);
                    673: }
                    674:
                    675: /*
                    676:  * Returns TRUE if fn is located in the temp directory and we want to save
                    677:  * those backups there.
                    678:  */
                    679: int
                    680: bkupleavetmp(const char *fn)
                    681: {
                    682:        if (!leavetmp)
                    683:                return(FALSE);
                    684:
1.103     tedu      685:        if (strncmp(fn, "/tmp", 4) == 0)
1.91      lum       686:                return (TRUE);
                    687:
                    688:        return (FALSE);
1.92      lum       689: }
                    690:
1.93      jasper    691: /*
1.92      lum       692:  * Expand file names beginning with '~' if appropriate:
                    693:  *   1, if ./~fn exists, continue without expanding tilde.
                    694:  *   2, else, if username 'fn' exists, expand tilde with home directory path.
                    695:  *   3, otherwise, continue and create new buffer called ~fn.
                    696:  */
                    697: char *
                    698: expandtilde(const char *fn)
                    699: {
                    700:        struct passwd   *pw;
                    701:        struct stat      statbuf;
                    702:        const char      *cp;
1.94      lum       703:        char             user[LOGIN_NAME_MAX], path[NFILEN];
1.105     florian   704:        char            *ret;
1.92      lum       705:        size_t           ulen, plen;
                    706:
                    707:        path[0] = '\0';
                    708:
                    709:        if (fn[0] != '~' || stat(fn, &statbuf) == 0) {
                    710:                if ((ret = strndup(fn, NFILEN)) == NULL)
                    711:                        return (NULL);
                    712:                return(ret);
                    713:        }
                    714:        cp = strchr(fn, '/');
                    715:        if (cp == NULL)
                    716:                cp = fn + strlen(fn); /* point to the NUL byte */
                    717:        ulen = cp - &fn[1];
                    718:        if (ulen >= sizeof(user)) {
                    719:                if ((ret = strndup(fn, NFILEN)) == NULL)
                    720:                        return (NULL);
                    721:                return(ret);
                    722:        }
1.105     florian   723:        if (ulen == 0) /* ~/ or ~ */
                    724:                pw = getpwuid(geteuid());
                    725:        else { /* ~user/ or ~user */
1.92      lum       726:                memcpy(user, &fn[1], ulen);
                    727:                user[ulen] = '\0';
1.105     florian   728:                pw = getpwnam(user);
1.92      lum       729:        }
                    730:        if (pw != NULL) {
                    731:                plen = strlcpy(path, pw->pw_dir, sizeof(path));
                    732:                if (plen == 0 || path[plen - 1] != '/') {
                    733:                        if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
1.97      lum       734:                                dobeep();
1.92      lum       735:                                ewprintf("Path too long");
                    736:                                return (NULL);
                    737:                        }
                    738:                }
                    739:                fn = cp;
                    740:                if (*fn == '/')
                    741:                        fn++;
                    742:        }
                    743:        if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
1.97      lum       744:                dobeep();
1.92      lum       745:                ewprintf("Path too long");
                    746:                return (NULL);
                    747:        }
                    748:        if ((ret = strndup(path, NFILEN)) == NULL)
                    749:                return (NULL);
                    750:
                    751:        return (ret);
1.1       deraadt   752: }