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

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