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

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