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

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