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

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