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

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