[BACK]Return to file.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/file.c, Revision 1.10

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/types.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <pwd.h>
                     32: #include <errno.h>
                     33: #include <stdio.h>
                     34: #include <fcntl.h>
                     35: #include <dirent.h>
                     36: #include <stdlib.h>
                     37: #include <unistd.h>
                     38: #include <string.h>
                     39: #include <fnmatch.h>
                     40:
                     41: #include "cvs.h"
                     42: #include "log.h"
                     43:
                     44:
                     45: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     46:
                     47:
                     48:
                     49: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     50:
                     51:
                     52:
                     53: /* ignore pattern */
                     54: struct cvs_ignpat {
                     55:        char  ip_pat[MAXNAMLEN];
                     56:        int   ip_flags;
                     57:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     58: };
                     59:
                     60:
                     61: /*
                     62:  * Standard patterns to ignore.
                     63:  */
                     64:
                     65: static const char *cvs_ign_std[] = {
                     66:        ".",
                     67:        "..",
                     68:        "*.o",
                     69:        "*.so",
                     70:        "*.bak",
                     71:        "*.orig",
                     72:        "*.rej",
                     73:        "*.exe",
                     74:        "*.depend",
                     75:        "CVS",
                     76:        "core",
1.8       jfb        77:        ".#*",
1.1       jfb        78: #ifdef OLD_SMELLY_CRUFT
                     79:        "RCSLOG",
                     80:        "tags",
                     81:        "TAGS",
                     82:        "RCS",
                     83:        "SCCS",
                     84:        "#*",
                     85:        ",*",
                     86: #endif
                     87: };
                     88:
                     89:
1.4       jfb        90: static RCSNUM *cvs_addedrev;
                     91:
                     92:
1.1       jfb        93: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                     94:
                     95:
1.6       jfb        96: static int        cvs_file_getdir  (struct cvs_file *, int);
                     97: static void       cvs_file_freedir (struct cvs_dir *);
                     98: static int        cvs_file_sort    (struct cvs_flist *);
                     99: static int        cvs_file_cmp     (const void *, const void *);
                    100: static CVSFILE*   cvs_file_alloc   (const char *, u_int);
1.3       jfb       101:
                    102:
                    103:
1.1       jfb       104: /*
                    105:  * cvs_file_init()
                    106:  *
                    107:  */
                    108:
                    109: int
                    110: cvs_file_init(void)
                    111: {
                    112:        int i;
                    113:        size_t len;
                    114:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    115:        FILE *ifp;
                    116:        struct passwd *pwd;
                    117:
                    118:        TAILQ_INIT(&cvs_ign_pats);
                    119:
1.4       jfb       120:        cvs_addedrev = rcsnum_alloc();
                    121:        rcsnum_aton("0", NULL, cvs_addedrev);
                    122:
1.1       jfb       123:        /* standard patterns to ignore */
1.10    ! jfb       124:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.1       jfb       125:                cvs_file_ignore(cvs_ign_std[i]);
                    126:
                    127:        /* read the cvsignore file in the user's home directory, if any */
                    128:        pwd = getpwuid(getuid());
                    129:        if (pwd != NULL) {
                    130:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    131:                ifp = fopen(path, "r");
                    132:                if (ifp == NULL) {
                    133:                        if (errno != ENOENT)
                    134:                                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    135:                }
                    136:                else {
                    137:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    138:                                len = strlen(buf);
                    139:                                if (len == 0)
                    140:                                        continue;
                    141:                                if (buf[len - 1] != '\n') {
                    142:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    143:                                            path);
                    144:                                }
                    145:                                buf[--len] = '\0';
                    146:                                cvs_file_ignore(buf);
                    147:                        }
                    148:                        (void)fclose(ifp);
                    149:                }
                    150:        }
                    151:
                    152:        return (0);
                    153: }
                    154:
                    155:
                    156: /*
                    157:  * cvs_file_ignore()
                    158:  *
                    159:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    160:  * Returns 0 on success, or -1 on failure.
                    161:  */
                    162:
                    163: int
                    164: cvs_file_ignore(const char *pat)
                    165: {
                    166:        char *cp;
                    167:        struct cvs_ignpat *ip;
                    168:
                    169:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    170:        if (ip == NULL) {
                    171:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    172:                return (-1);
                    173:        }
                    174:
                    175:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    176:
                    177:        /* check if we will need globbing for that pattern */
                    178:        ip->ip_flags = CVS_IGN_STATIC;
                    179:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    180:                if (CVS_CHAR_ISMETA(*cp)) {
                    181:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    182:                        break;
                    183:                }
                    184:        }
                    185:
                    186:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    187:
                    188:        return (0);
                    189: }
                    190:
                    191:
                    192: /*
1.5       jfb       193:  * cvs_file_chkign()
1.1       jfb       194:  *
                    195:  * Returns 1 if the filename <file> is matched by one of the ignore
                    196:  * patterns, or 0 otherwise.
                    197:  */
                    198:
                    199: int
1.5       jfb       200: cvs_file_chkign(const char *file)
1.1       jfb       201: {
                    202:        struct cvs_ignpat *ip;
                    203:
                    204:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    205:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    206:                        if (strcmp(file, ip->ip_pat) == 0)
                    207:                                return (1);
                    208:                }
                    209:                else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
                    210:                        return (1);
                    211:        }
                    212:
                    213:        return (0);
                    214: }
                    215:
                    216:
                    217: /*
1.6       jfb       218:  * cvs_file_create()
1.1       jfb       219:  *
1.6       jfb       220:  * Create a new file whose path is specified in <path> and of type <type>.
1.1       jfb       221:  */
                    222:
1.6       jfb       223: CVSFILE*
                    224: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1       jfb       225: {
1.6       jfb       226:        int fd;
                    227:        CVSFILE *cfp;
1.1       jfb       228:
1.6       jfb       229:        cfp = cvs_file_alloc(path, type);
                    230:        if (cfp == NULL)
                    231:                return (NULL);
                    232:        cfp->cf_type = type;
1.1       jfb       233:
1.6       jfb       234:        if (type == DT_DIR) {
                    235:                if (mkdir(path, mode) == -1) {
                    236:                        cvs_file_free(cfp);
                    237:                        return (NULL);
                    238:                }
1.1       jfb       239:        }
1.6       jfb       240:        else {
                    241:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    242:                if (fd == -1) {
                    243:                        cvs_file_free(cfp);
1.1       jfb       244:                        return (NULL);
                    245:                }
1.6       jfb       246:                (void)close(fd);
1.1       jfb       247:        }
                    248:
1.6       jfb       249:        return (cfp);
1.3       jfb       250: }
                    251:
                    252:
                    253: /*
                    254:  * cvs_file_get()
                    255:  *
                    256:  * Load a cvs_file structure with all the information pertaining to the file
                    257:  * <path>.
1.4       jfb       258:  * The <flags> parameter specifies various flags that alter the behaviour of
                    259:  * the function.  The CF_STAT flag is used to keep stat information of the
                    260:  * file in the structure after it is used (it is lost otherwise).  The
                    261:  * CF_RECURSE flag causes the function to recursively load subdirectories
                    262:  * when <path> is a directory.  The CF_SORT flag causes the files to be
                    263:  * sorted in alphabetical order upon loading.
                    264:  * The special case of "." as a path specification generates recursion for
                    265:  * a single level and is equivalent to calling cvs_file_get() on all files
                    266:  * of that directory.
1.3       jfb       267:  * Returns a pointer to the cvs file structure, which must later be freed
                    268:  * with cvs_file_free().
                    269:  */
                    270:
                    271: struct cvs_file*
                    272: cvs_file_get(const char *path, int flags)
                    273: {
1.4       jfb       274:        int cwd;
1.9       jfb       275:        size_t len;
1.5       jfb       276:        char buf[32];
1.3       jfb       277:        struct stat st;
1.5       jfb       278:        struct tm lmtm;
1.3       jfb       279:        struct cvs_file *cfp;
1.4       jfb       280:        struct cvs_ent *ent;
1.3       jfb       281:
1.4       jfb       282:        if (strcmp(path, ".") == 0)
                    283:                cwd = 1;
                    284:        else
                    285:                cwd = 0;
                    286:
1.3       jfb       287:        if (stat(path, &st) == -1) {
1.4       jfb       288:                cvs_log(LP_ERRNO, "failed to stat %s", path);
1.3       jfb       289:                return (NULL);
                    290:        }
                    291:
1.6       jfb       292:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
1.3       jfb       293:        if (cfp == NULL) {
                    294:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    295:                return (NULL);
                    296:        }
                    297:
1.4       jfb       298:        ent = cvs_ent_getent(path);
                    299:        if (ent == NULL)
                    300:                cfp->cf_cvstat = (cwd == 1) ?
                    301:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
                    302:        else {
1.9       jfb       303:                /* always show directories as up-to-date */
                    304:                if (ent->ce_type == CVS_ENT_DIR)
                    305:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    306:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1.4       jfb       307:                        cfp->cf_cvstat = CVS_FST_ADDED;
1.5       jfb       308:                else {
                    309:                        /* check last modified time */
1.9       jfb       310:                        if ((gmtime_r((time_t *)&(st.st_mtime), &lmtm) == NULL) ||
1.5       jfb       311:                            (asctime_r(&lmtm, buf) == NULL)) {
                    312:                                cvs_log(LP_ERR,
                    313:                                    "failed to generate file timestamp");
                    314:                                /* fake an up to date file */
                    315:                                strlcpy(buf, ent->ce_timestamp, sizeof(buf));
                    316:                        }
1.9       jfb       317:                        len = strlen(buf);
                    318:                        if ((len > 0) && (buf[len - 1] == '\n'))
                    319:                                buf[--len] = '\0';
                    320:
1.5       jfb       321:                        if (strcmp(buf, ent->ce_timestamp) == 0)
                    322:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    323:                        else
                    324:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    325:                }
1.4       jfb       326:
                    327:                cvs_ent_free(ent);
                    328:        }
                    329:
1.3       jfb       330:        /* convert from stat mode to dirent values */
                    331:        cfp->cf_type = IFTODT(st.st_mode);
1.4       jfb       332:        if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
1.6       jfb       333:                if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
                    334:                        free(cfp->cf_ddat);
1.4       jfb       335:                        cfp->cf_ddat = NULL;
1.6       jfb       336:                }
                    337:                else if (cvs_file_getdir(cfp, flags) < 0) {
                    338:                        cvs_file_free(cfp);
                    339:                        return (NULL);
1.3       jfb       340:                }
                    341:        }
                    342:
                    343:        if (flags & CF_STAT) {
                    344:                cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
                    345:                if (cfp->cf_stat == NULL) {
                    346:                        cvs_log(LP_ERRNO, "failed to allocate stat structure");
                    347:                        cvs_file_free(cfp);
                    348:                        return (NULL);
                    349:                }
                    350:
                    351:                memcpy(cfp->cf_stat, &st, sizeof(struct stat));
1.9       jfb       352:        }
                    353:
                    354:        return (cfp);
                    355: }
                    356:
                    357:
                    358: /*
                    359:  * cvs_file_getspec()
                    360:  *
                    361:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    362:  * whose size is given in <fsn>.
                    363:  * Returns a pointer to the lowest common subdirectory to all specified
                    364:  * files.
                    365:  */
                    366:
                    367: CVSFILE*
                    368: cvs_file_getspec(char **fspec, int fsn, int flags)
                    369: {
                    370:        int i, c;
                    371:        char common[MAXPATHLEN];
                    372:        struct cvs_file *cfp;
                    373:
                    374:        /* first find the common subdir */
                    375:        strlcpy(common, fspec[0], sizeof(common));
                    376:        for (i = 1; i < fsn; i++) {
                    377:                for (c = 0; ; c++) {
                    378:                        if (common[c] != fspec[i][c]) {
                    379:                                printf("backtracking!\n");
                    380:                                /* go back to last dir */
                    381:                                while ((c > 0) && (common[--c] != '/'))
                    382:                                        common[c] = '\0';
                    383:                                break;
                    384:                        }
                    385:                }
                    386:        }
                    387:        printf("common part = `%s'\n", common);
                    388:        if (*common == '\0')
                    389:                strlcpy(common, ".", sizeof(common));
                    390:
                    391:        /* first load the common subdirectory */
                    392:        cfp = cvs_file_get(common, flags);
                    393:        for (i = 0; i < fsn; i++) {
1.3       jfb       394:        }
                    395:
                    396:        return (cfp);
                    397: }
                    398:
                    399:
                    400: /*
                    401:  * cvs_file_getdir()
                    402:  *
                    403:  * Get a cvs directory structure for the directory whose path is <dir>.
                    404:  */
                    405:
1.6       jfb       406: static int
1.3       jfb       407: cvs_file_getdir(struct cvs_file *cf, int flags)
                    408: {
                    409:        int nf, ret, fd;
                    410:        long base;
                    411:        void *dp, *ep, *tmp;
                    412:        char fbuf[1024], pbuf[MAXPATHLEN];
                    413:        struct dirent *ent;
                    414:        struct cvs_file *cfp;
                    415:        struct cvs_dir *cdp;
1.10    ! jfb       416:        struct cvs_flist dirs;
1.3       jfb       417:
1.10    ! jfb       418:        TAILQ_INIT(&dirs);
1.7       jfb       419:        cdp = cf->cf_ddat;
                    420:
1.4       jfb       421:        if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
                    422:                cdp->cd_repo = strdup(pbuf);
                    423:                if (cdp->cd_repo == NULL) {
                    424:                        free(cdp);
1.6       jfb       425:                        return (-1);
1.4       jfb       426:                }
1.3       jfb       427:        }
                    428:
                    429:        cdp->cd_root = cvsroot_get(cf->cf_path);
                    430:        if (cdp->cd_root == NULL) {
                    431:                cvs_file_freedir(cdp);
1.6       jfb       432:                return (-1);
1.3       jfb       433:        }
                    434:
                    435:        fd = open(cf->cf_path, O_RDONLY);
                    436:        if (fd == -1) {
                    437:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    438:                cvs_file_freedir(cdp);
1.6       jfb       439:                return (-1);
1.3       jfb       440:        }
                    441:        ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    442:        if (ret == -1) {
                    443:                cvs_log(LP_ERRNO, "failed to get directory entries");
                    444:                (void)close(fd);
                    445:                cvs_file_freedir(cdp);
1.6       jfb       446:                return (-1);
1.3       jfb       447:        }
                    448:
                    449:        dp = fbuf;
                    450:        ep = fbuf + (size_t)ret;
                    451:        while (dp < ep) {
                    452:                ent = (struct dirent *)dp;
                    453:                dp += ent->d_reclen;
                    454:
1.5       jfb       455:                if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.3       jfb       456:                        continue;
                    457:
                    458:                snprintf(pbuf, sizeof(pbuf), "%s/%s", cf->cf_path, ent->d_name);
                    459:                cfp = cvs_file_get(pbuf, flags);
1.4       jfb       460:                if (cfp != NULL) {
                    461:                        cfp->cf_parent = cf;
1.10    ! jfb       462:
        !           463:                        if (cfp->cf_type == DT_DIR)
        !           464:                                TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
        !           465:                        else
        !           466:                                TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
        !           467:                                    cf_list);
1.4       jfb       468:                }
1.3       jfb       469:        }
                    470:
1.10    ! jfb       471:        if (flags & CF_SORT) {
1.3       jfb       472:                cvs_file_sort(&(cdp->cd_files));
1.10    ! jfb       473:                cvs_file_sort(&dirs);
        !           474:        }
        !           475:        TAILQ_FOREACH(cfp, &dirs, cf_list)
        !           476:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.3       jfb       477:
                    478:        (void)close(fd);
1.6       jfb       479:        cf->cf_ddat = cdp;
1.3       jfb       480:
1.6       jfb       481:        return (0);
1.3       jfb       482: }
                    483:
                    484:
                    485: /*
                    486:  * cvs_file_free()
                    487:  *
                    488:  * Free a cvs_file structure and its contents.
                    489:  */
                    490:
                    491: void
                    492: cvs_file_free(struct cvs_file *cf)
                    493: {
                    494:        if (cf->cf_path != NULL)
                    495:                free(cf->cf_path);
                    496:        if (cf->cf_stat != NULL)
                    497:                free(cf->cf_stat);
                    498:        if (cf->cf_ddat != NULL)
                    499:                cvs_file_freedir(cf->cf_ddat);
                    500:        free(cf);
1.5       jfb       501: }
                    502:
                    503:
                    504: /*
                    505:  * cvs_file_examine()
                    506:  *
                    507:  * Examine the contents of the CVS file structure <cf> with the function
                    508:  * <exam>.  The function is called for all subdirectories and files of the
                    509:  * root file.
                    510:  */
                    511:
                    512: int
                    513: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    514: {
                    515:        int ret;
                    516:        struct cvs_file *fp;
                    517:
                    518:        if (cf->cf_type == DT_DIR) {
                    519:                ret = (*exam)(cf, arg);
1.10    ! jfb       520:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       521:                        ret = cvs_file_examine(fp, exam, arg);
                    522:                        if (ret == -1)
                    523:                                return (-1);
                    524:                }
                    525:        }
                    526:        else
                    527:                return (*exam)(cf, arg);
1.3       jfb       528: }
                    529:
                    530:
                    531: /*
                    532:  * cvs_file_freedir()
                    533:  *
                    534:  * Free a cvs_dir structure and its contents.
                    535:  */
                    536:
                    537: static void
                    538: cvs_file_freedir(struct cvs_dir *cd)
                    539: {
                    540:        struct cvs_file *cfp;
                    541:
                    542:        if (cd->cd_root != NULL)
                    543:                cvsroot_free(cd->cd_root);
                    544:        if (cd->cd_repo != NULL)
                    545:                free(cd->cd_repo);
                    546:
1.10    ! jfb       547:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
        !           548:                cfp = TAILQ_FIRST(&(cd->cd_files));
        !           549:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       550:                cvs_file_free(cfp);
                    551:        }
                    552: }
                    553:
                    554:
                    555: /*
                    556:  * cvs_file_sort()
                    557:  *
                    558:  * Sort a list of cvs file structures according to their filename.
                    559:  */
                    560:
                    561: static int
                    562: cvs_file_sort(struct cvs_flist *flp)
                    563: {
                    564:        int i;
                    565:        size_t nb;
                    566:        struct cvs_file *cf, *cfvec[256];
                    567:
                    568:        i = 0;
1.10    ! jfb       569:        TAILQ_FOREACH(cf, flp, cf_list) {
1.3       jfb       570:                cfvec[i++] = cf;
                    571:                if (i == sizeof(cfvec)/sizeof(struct cvs_file *)) {
                    572:                        cvs_log(LP_WARN, "too many files to sort");
                    573:                        return (-1);
                    574:                }
                    575:
                    576:                /* now unlink it from the list,
                    577:                 * we'll put it back in order later
                    578:                 */
1.10    ! jfb       579:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       580:        }
                    581:
                    582:        /* clear the list just in case */
1.10    ! jfb       583:        TAILQ_INIT(flp);
1.3       jfb       584:        nb = (size_t)i;
                    585:
                    586:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    587:
                    588:        /* rebuild the list from the bottom up */
                    589:        for (i = (int)nb - 1; i >= 0; i--)
1.10    ! jfb       590:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       591:
                    592:        return (0);
                    593: }
                    594:
                    595:
                    596: static int
                    597: cvs_file_cmp(const void *f1, const void *f2)
                    598: {
                    599:        struct cvs_file *cf1, *cf2;
                    600:        cf1 = *(struct cvs_file **)f1;
                    601:        cf2 = *(struct cvs_file **)f2;
                    602:        return strcmp(cf1->cf_name, cf2->cf_name);
1.6       jfb       603: }
                    604:
                    605:
                    606: CVSFILE*
                    607: cvs_file_alloc(const char *path, u_int type)
                    608: {
                    609:        size_t len;
                    610:        char pbuf[MAXPATHLEN];
                    611:        CVSFILE *cfp;
                    612:        struct cvs_dir *ddat;
                    613:
                    614:        cfp = (struct cvs_file *)malloc(sizeof(*cfp));
                    615:        if (cfp == NULL) {
                    616:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    617:                return (NULL);
                    618:        }
                    619:        memset(cfp, 0, sizeof(*cfp));
                    620:
                    621:        /* ditch trailing slashes */
                    622:        strlcpy(pbuf, path, sizeof(pbuf));
                    623:        len = strlen(pbuf);
                    624:        while (pbuf[len - 1] == '/')
                    625:                pbuf[--len] = '\0';
                    626:
                    627:        cfp->cf_path = strdup(pbuf);
                    628:        if (cfp->cf_path == NULL) {
                    629:                free(cfp);
                    630:                return (NULL);
                    631:        }
                    632:
                    633:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    634:        if (cfp->cf_name == NULL)
                    635:                cfp->cf_name = cfp->cf_path;
                    636:        else
                    637:                cfp->cf_name++;
                    638:
                    639:        cfp->cf_type = type;
                    640:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    641:
                    642:        if (type == DT_DIR) {
                    643:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    644:                if (ddat == NULL) {
                    645:                        cvs_file_free(cfp);
                    646:                        return (NULL);
                    647:                }
                    648:                memset(ddat, 0, sizeof(*ddat));
1.10    ! jfb       649:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       650:                cfp->cf_ddat = ddat;
                    651:        }
                    652:        return (cfp);
1.1       jfb       653: }