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

Annotation of src/usr.bin/cvs/util.c, Revision 1.133

1.133   ! joris       1: /*     $OpenBSD: util.c,v 1.132 2008/02/04 19:08:32 joris Exp $        */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.71      xsa         4:  * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
                      5:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.9       jfb         6:  * All rights reserved.
1.1       jfb         7:  *
1.9       jfb         8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
1.1       jfb        11:  *
1.9       jfb        12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        14:  * 2. The name of the author may not be used to endorse or promote products
1.9       jfb        15:  *    derived from this software without specific prior written permission.
1.1       jfb        16:  *
                     17:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     18:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     19:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     20:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     21:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     22:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     23:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     24:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     25:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.9       jfb        26:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        27:  */
                     28:
1.107     otto       29: #include <sys/stat.h>
                     30: #include <sys/wait.h>
                     31:
                     32: #include <errno.h>
                     33: #include <md5.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
1.132     joris      36: #include <paths.h>
1.107     otto       37: #include <unistd.h>
1.1       jfb        38:
                     39: #include "cvs.h"
1.112     joris      40: #include "remote.h"
1.1       jfb        41:
1.116     tobias     42: extern int print_stdout;
                     43:
1.1       jfb        44: /* letter -> mode type map */
                     45: static const int cvs_modetypes[26] = {
                     46:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     47:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     48: };
                     49:
                     50: /* letter -> mode map */
                     51: static const mode_t cvs_modes[3][26] = {
                     52:        {
                     53:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     54:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     55:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     56:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     57:        },
                     58:        {
                     59:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     60:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     61:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     62:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     63:        },
                     64:        {
                     65:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     66:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     67:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     68:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     69:        }
                     70: };
                     71:
                     72:
                     73: /* octal -> string */
                     74: static const char *cvs_modestr[8] = {
                     75:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     76: };
                     77:
1.9       jfb        78: /*
1.1       jfb        79:  * cvs_strtomode()
                     80:  *
                     81:  * Read the contents of the string <str> and generate a permission mode from
                     82:  * the contents of <str>, which is assumed to have the mode format of CVS.
                     83:  * The CVS protocol specification states that any modes or mode types that are
                     84:  * not recognized should be silently ignored.  This function does not return
                     85:  * an error in such cases, but will issue warnings.
                     86:  */
1.64      joris      87: void
1.1       jfb        88: cvs_strtomode(const char *str, mode_t *mode)
                     89: {
1.2       jfb        90:        char type;
1.64      joris      91:        size_t l;
1.1       jfb        92:        mode_t m;
                     93:        char buf[32], ms[4], *sp, *ep;
                     94:
                     95:        m = 0;
1.64      joris      96:        l = strlcpy(buf, str, sizeof(buf));
                     97:        if (l >= sizeof(buf))
                     98:                fatal("cvs_strtomode: string truncation");
                     99:
1.1       jfb       100:        sp = buf;
                    101:        ep = sp;
                    102:
                    103:        for (sp = buf; ep != NULL; sp = ep + 1) {
                    104:                ep = strchr(sp, ',');
                    105:                if (ep != NULL)
1.2       jfb       106:                        *ep = '\0';
1.1       jfb       107:
1.14      weingart  108:                memset(ms, 0, sizeof ms);
                    109:                if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
                    110:                        sscanf(sp, "%c=", &type) != 1) {
1.89      joris     111:                        fatal("failed to scan mode string `%s'", sp);
1.1       jfb       112:                }
                    113:
1.78      deraadt   114:                if (type <= 'a' || type >= 'z' ||
                    115:                    cvs_modetypes[type - 'a'] == -1) {
1.79      joris     116:                        cvs_log(LP_ERR,
1.1       jfb       117:                            "invalid mode type `%c'"
                    118:                            " (`u', `g' or `o' expected), ignoring", type);
                    119:                        continue;
                    120:                }
                    121:
                    122:                /* make type contain the actual mode index */
                    123:                type = cvs_modetypes[type - 'a'];
                    124:
                    125:                for (sp = ms; *sp != '\0'; sp++) {
1.78      deraadt   126:                        if (*sp <= 'a' || *sp >= 'z' ||
                    127:                            cvs_modes[(int)type][*sp - 'a'] == 0) {
1.89      joris     128:                                fatal("invalid permission bit `%c'", *sp);
1.15      deraadt   129:                        } else
1.5       jfb       130:                                m |= cvs_modes[(int)type][*sp - 'a'];
1.1       jfb       131:                }
                    132:        }
                    133:
                    134:        *mode = m;
                    135: }
                    136:
                    137: /*
                    138:  * cvs_modetostr()
                    139:  *
1.30      jfb       140:  * Generate a CVS-format string to represent the permissions mask on a file
                    141:  * from the mode <mode> and store the result in <buf>, which can accept up to
                    142:  * <len> bytes (including the terminating NUL byte).  The result is guaranteed
                    143:  * to be NUL-terminated.
1.1       jfb       144:  */
1.65      joris     145: void
1.1       jfb       146: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    147: {
                    148:        char tmp[16], *bp;
                    149:        mode_t um, gm, om;
                    150:
                    151:        um = (mode & S_IRWXU) >> 6;
                    152:        gm = (mode & S_IRWXG) >> 3;
                    153:        om = mode & S_IRWXO;
                    154:
                    155:        bp = buf;
                    156:        *bp = '\0';
                    157:
                    158:        if (um) {
1.68      xsa       159:                if (strlcpy(tmp, "u=", sizeof(tmp)) >= sizeof(tmp) ||
                    160:                    strlcat(tmp, cvs_modestr[um], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     161:                        fatal("cvs_modetostr: overflow for user mode");
                    162:
1.68      xsa       163:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     164:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       165:        }
1.65      joris     166:
1.1       jfb       167:        if (gm) {
1.65      joris     168:                if (um) {
1.68      xsa       169:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     170:                                fatal("cvs_modetostr: string truncation");
                    171:                }
                    172:
1.68      xsa       173:                if (strlcpy(tmp, "g=", sizeof(tmp)) >= sizeof(tmp) ||
                    174:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     175:                        fatal("cvs_modetostr: overflow for group mode");
                    176:
1.68      xsa       177:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     178:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       179:        }
1.65      joris     180:
1.1       jfb       181:        if (om) {
1.65      joris     182:                if (um || gm) {
1.68      xsa       183:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     184:                                fatal("cvs_modetostr: string truncation");
                    185:                }
                    186:
1.68      xsa       187:                if (strlcpy(tmp, "o=", sizeof(tmp)) >= sizeof(tmp) ||
                    188:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     189:                        fatal("cvs_modetostr: overflow for others mode");
                    190:
1.68      xsa       191:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     192:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       193:        }
                    194: }
                    195:
                    196: /*
                    197:  * cvs_cksum()
                    198:  *
                    199:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    200:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    201:  * given in <len> and must be at least 33.
                    202:  * Returns 0 on success, or -1 on failure.
                    203:  */
                    204: int
                    205: cvs_cksum(const char *file, char *dst, size_t len)
                    206: {
                    207:        if (len < CVS_CKSUM_LEN) {
1.79      joris     208:                cvs_log(LP_ERR, "buffer too small for checksum");
1.1       jfb       209:                return (-1);
                    210:        }
                    211:        if (MD5File(file, dst) == NULL) {
1.79      joris     212:                cvs_log(LP_ERR, "failed to generate checksum for %s", file);
1.1       jfb       213:                return (-1);
                    214:        }
                    215:
                    216:        return (0);
                    217: }
                    218:
                    219: /*
                    220:  * cvs_getargv()
                    221:  *
                    222:  * Parse a line contained in <line> and generate an argument vector by
                    223:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    224:  * <argv>, which can accept up to <argvlen> entries.
1.20      david     225:  * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1       jfb       226:  */
                    227: int
                    228: cvs_getargv(const char *line, char **argv, int argvlen)
                    229: {
                    230:        u_int i;
1.67      xsa       231:        int argc, error;
1.118     tobias    232:        char *linebuf, *lp, *cp;
1.1       jfb       233:
1.117     tobias    234:        linebuf = xstrdup(line);
1.65      joris     235:
1.27      pat       236:        memset(argv, 0, argvlen * sizeof(char *));
1.1       jfb       237:        argc = 0;
                    238:
                    239:        /* build the argument vector */
1.67      xsa       240:        error = 0;
1.1       jfb       241:        for (lp = linebuf; lp != NULL;) {
1.118     tobias    242:                cp = strsep(&lp, " \t");
                    243:                if (cp == NULL)
                    244:                        break;
                    245:                else if (*cp == '\0')
                    246:                        continue;
1.1       jfb       247:
1.16      jfb       248:                if (argc == argvlen) {
1.67      xsa       249:                        error++;
1.16      jfb       250:                        break;
                    251:                }
                    252:
1.118     tobias    253:                argv[argc] = xstrdup(cp);
1.1       jfb       254:                argc++;
                    255:        }
                    256:
1.67      xsa       257:        if (error != 0) {
1.1       jfb       258:                /* ditch the argument vector */
                    259:                for (i = 0; i < (u_int)argc; i++)
1.59      joris     260:                        xfree(argv[i]);
1.1       jfb       261:                argc = -1;
                    262:        }
                    263:
1.117     tobias    264:        xfree(linebuf);
1.1       jfb       265:        return (argc);
1.17      jfb       266: }
                    267:
                    268: /*
                    269:  * cvs_makeargv()
                    270:  *
1.20      david     271:  * Allocate an argument vector large enough to accommodate for all the
1.17      jfb       272:  * arguments found in <line> and return it.
                    273:  */
1.42      xsa       274: char **
1.17      jfb       275: cvs_makeargv(const char *line, int *argc)
                    276: {
                    277:        int i, ret;
                    278:        char *argv[1024], **copy;
                    279:
                    280:        ret = cvs_getargv(line, argv, 1024);
                    281:        if (ret == -1)
                    282:                return (NULL);
                    283:
1.77      ray       284:        copy = xcalloc(ret + 1, sizeof(char *));
1.17      jfb       285:
                    286:        for (i = 0; i < ret; i++)
                    287:                copy[i] = argv[i];
                    288:        copy[ret] = NULL;
                    289:
                    290:        *argc = ret;
                    291:        return (copy);
1.1       jfb       292: }
                    293:
                    294: /*
                    295:  * cvs_freeargv()
                    296:  *
                    297:  * Free an argument vector previously generated by cvs_getargv().
                    298:  */
                    299: void
                    300: cvs_freeargv(char **argv, int argc)
                    301: {
                    302:        int i;
                    303:
                    304:        for (i = 0; i < argc; i++)
1.16      jfb       305:                if (argv[i] != NULL)
1.59      joris     306:                        xfree(argv[i]);
1.38      xsa       307: }
                    308:
                    309: /*
                    310:  * cvs_chdir()
                    311:  *
1.63      xsa       312:  * Change to directory <path>.
                    313:  * If <rm> is equal to `1', <path> is removed if chdir() fails so we
                    314:  * do not have temporary directories leftovers.
1.60      xsa       315:  * Returns 0 on success.
1.50      xsa       316:  */
1.38      xsa       317: int
1.63      xsa       318: cvs_chdir(const char *path, int rm)
1.38      xsa       319: {
1.63      xsa       320:        if (chdir(path) == -1) {
                    321:                if (rm == 1)
                    322:                        cvs_unlink(path);
1.60      xsa       323:                fatal("cvs_chdir: `%s': %s", path, strerror(errno));
1.63      xsa       324:        }
1.49      xsa       325:
                    326:        return (0);
                    327: }
                    328:
                    329: /*
                    330:  * cvs_rename()
                    331:  * Change the name of a file.
                    332:  * rename() wrapper with an error message.
1.60      xsa       333:  * Returns 0 on success.
1.49      xsa       334:  */
                    335: int
                    336: cvs_rename(const char *from, const char *to)
                    337: {
1.90      joris     338:        if (cvs_server_active == 0)
                    339:                cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
1.49      xsa       340:
                    341:        if (cvs_noexec == 1)
                    342:                return (0);
                    343:
1.60      xsa       344:        if (rename(from, to) == -1)
                    345:                fatal("cvs_rename: `%s'->`%s': %s", from, to, strerror(errno));
1.40      xsa       346:
                    347:        return (0);
                    348: }
                    349:
                    350: /*
                    351:  * cvs_unlink()
                    352:  *
                    353:  * Removes the link named by <path>.
                    354:  * unlink() wrapper with an error message.
                    355:  * Returns 0 on success, or -1 on failure.
                    356:  */
                    357: int
                    358: cvs_unlink(const char *path)
                    359: {
1.90      joris     360:        if (cvs_server_active == 0)
                    361:                cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
1.40      xsa       362:
                    363:        if (cvs_noexec == 1)
                    364:                return (0);
                    365:
1.78      deraadt   366:        if (unlink(path) == -1 && errno != ENOENT) {
1.94      xsa       367:                cvs_log(LP_ERRNO, "%s", path);
1.38      xsa       368:                return (-1);
                    369:        }
                    370:
                    371:        return (0);
1.1       jfb       372: }
1.24      joris     373:
                    374: /*
1.45      xsa       375:  * cvs_rmdir()
1.30      jfb       376:  *
                    377:  * Remove a directory tree from disk.
                    378:  * Returns 0 on success, or -1 on failure.
1.24      joris     379:  */
                    380: int
1.45      xsa       381: cvs_rmdir(const char *path)
1.24      joris     382: {
1.99      todd      383:        int type, ret = -1;
1.24      joris     384:        DIR *dirp;
                    385:        struct dirent *ent;
1.99      todd      386:        struct stat st;
1.24      joris     387:        char fpath[MAXPATHLEN];
1.46      xsa       388:
1.90      joris     389:        if (cvs_server_active == 0)
                    390:                cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
1.46      xsa       391:
                    392:        if (cvs_noexec == 1)
                    393:                return (0);
1.24      joris     394:
                    395:        if ((dirp = opendir(path)) == NULL) {
1.79      joris     396:                cvs_log(LP_ERR, "failed to open '%s'", path);
1.30      jfb       397:                return (-1);
1.24      joris     398:        }
                    399:
                    400:        while ((ent = readdir(dirp)) != NULL) {
                    401:                if (!strcmp(ent->d_name, ".") ||
                    402:                    !strcmp(ent->d_name, ".."))
                    403:                        continue;
                    404:
1.105     xsa       405:                (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
                    406:                    path, ent->d_name);
1.24      joris     407:
1.99      todd      408:                if (ent->d_type == DT_UNKNOWN) {
1.104     todd      409:                        if (lstat(fpath, &st) == -1)
1.99      todd      410:                                fatal("'%s': %s", fpath, strerror(errno));
                    411:
                    412:                        switch (st.st_mode & S_IFMT) {
                    413:                        case S_IFDIR:
                    414:                                type = CVS_DIR;
                    415:                                break;
                    416:                        case S_IFREG:
                    417:                                type = CVS_FILE;
                    418:                                break;
                    419:                        default:
                    420:                                fatal("'%s': Unknown file type in copy",
                    421:                                    fpath);
                    422:                        }
                    423:                } else {
                    424:                        switch (ent->d_type) {
                    425:                        case DT_DIR:
                    426:                                type = CVS_DIR;
                    427:                                break;
                    428:                        case DT_REG:
                    429:                                type = CVS_FILE;
                    430:                                break;
                    431:                        default:
                    432:                                fatal("'%s': Unknown file type in copy",
                    433:                                    fpath);
                    434:                        }
                    435:                }
                    436:                switch (type) {
1.128     xsa       437:                case CVS_DIR:
1.45      xsa       438:                        if (cvs_rmdir(fpath) == -1)
1.33      pat       439:                                goto done;
1.99      todd      440:                        break;
                    441:                case CVS_FILE:
                    442:                        if (cvs_unlink(fpath) == -1 && errno != ENOENT)
                    443:                                goto done;
                    444:                        break;
                    445:                default:
                    446:                        fatal("type %d unknown, shouldn't happen", type);
                    447:                }
1.24      joris     448:        }
                    449:
                    450:
1.78      deraadt   451:        if (rmdir(path) == -1 && errno != ENOENT) {
1.93      xsa       452:                cvs_log(LP_ERRNO, "%s", path);
1.33      pat       453:                goto done;
1.30      jfb       454:        }
1.24      joris     455:
1.33      pat       456:        ret = 0;
                    457: done:
                    458:        closedir(dirp);
1.34      joris     459:        return (ret);
                    460: }
                    461:
1.51      xsa       462: void
1.81      joris     463: cvs_get_repository_path(const char *dir, char *dst, size_t len)
                    464: {
                    465:        char buf[MAXPATHLEN];
                    466:
                    467:        cvs_get_repository_name(dir, buf, sizeof(buf));
1.105     xsa       468:        (void)xsnprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
1.81      joris     469: }
                    470:
                    471: void
                    472: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51      xsa       473: {
                    474:        FILE *fp;
1.123     tobias    475:        char fpath[MAXPATHLEN];
1.116     tobias    476:
1.127     tobias    477:        dst[0] = '\0';
                    478:
                    479:        if (!(cmdp->cmd_flags & CVS_USE_WDIR)) {
                    480:                if (strlcpy(dst, dir, len) >= len)
1.116     tobias    481:                        fatal("cvs_get_repository_name: truncation");
                    482:                return;
                    483:        }
1.79      joris     484:
1.127     tobias    485:        switch (cvs_cmdop) {
                    486:        case CVS_OP_EXPORT:
                    487:                if (strcmp(dir, "."))
                    488:                        if (strlcpy(dst, dir, len) >= len)
                    489:                                fatal("cvs_get_repository_name: truncation");
                    490:                break;
                    491:        case CVS_OP_IMPORT:
                    492:                if (strlcpy(dst, import_repository, len) >= len)
                    493:                        fatal("cvs_get_repository_name: truncation");
                    494:                if (strlcat(dst, "/", len) >= len)
                    495:                        fatal("cvs_get_repository_name: truncation");
1.84      joris     496:
1.127     tobias    497:                if (strcmp(dir, "."))
                    498:                        if (strlcat(dst, dir, len) >= len)
1.85      joris     499:                                fatal("cvs_get_repository_name: truncation");
1.127     tobias    500:                break;
                    501:        default:
                    502:                (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
                    503:                    dir, CVS_PATH_REPOSITORY);
                    504:                if ((fp = fopen(fpath, "r")) != NULL) {
                    505:                        if ((fgets(dst, len, fp)) == NULL)
                    506:                                fatal("%s: bad repository file", fpath);
                    507:                        dst[strcspn(dst, "\n")] = '\0';
                    508:                        (void)fclose(fp);
                    509:                } else if (cvs_cmdop != CVS_OP_CHECKOUT)
                    510:                        fatal("%s is missing", fpath);
                    511:                break;
1.51      xsa       512:        }
                    513: }
                    514:
                    515: void
1.87      xsa       516: cvs_mkadmin(const char *path, const char *root, const char *repo,
1.131     tobias    517:     char *tag, char *date)
1.51      xsa       518: {
                    519:        FILE *fp;
1.79      joris     520:        struct stat st;
                    521:        char buf[MAXPATHLEN];
1.51      xsa       522:
1.90      joris     523:        if (cvs_server_active == 0)
1.131     tobias    524:                cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s)",
1.90      joris     525:                    path, root, repo, (tag != NULL) ? tag : "",
1.131     tobias    526:                    (date != NULL) ? date : "");
1.51      xsa       527:
1.105     xsa       528:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_CVSDIR);
1.51      xsa       529:
1.79      joris     530:        if (mkdir(buf, 0755) == -1 && errno != EEXIST)
                    531:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    532:
1.105     xsa       533:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ROOTSPEC);
1.79      joris     534:
                    535:        if ((fp = fopen(buf, "w")) == NULL)
                    536:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    537:
                    538:        fprintf(fp, "%s\n", root);
                    539:        (void)fclose(fp);
1.51      xsa       540:
1.105     xsa       541:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_REPOSITORY);
1.51      xsa       542:
1.79      joris     543:        if ((fp = fopen(buf, "w")) == NULL)
                    544:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51      xsa       545:
1.79      joris     546:        fprintf(fp, "%s\n", repo);
1.51      xsa       547:        (void)fclose(fp);
1.87      xsa       548:
1.131     tobias    549:        cvs_write_tagfile(path, tag, date);
1.54      joris     550: }
                    551:
1.79      joris     552: void
1.112     joris     553: cvs_mkpath(const char *path, char *tag)
1.72      niallo    554: {
1.122     tobias    555:        CVSENTRIES *ent;
1.79      joris     556:        FILE *fp;
                    557:        size_t len;
1.122     tobias    558:        char entry[CVS_ENT_MAXLINELEN], sticky[CVS_REV_BUFSZ];
                    559:        char *sp, *dp, *dir, *p, rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.79      joris     560:
                    561:        dir = xstrdup(path);
                    562:
                    563:        STRIP_SLASH(dir);
1.90      joris     564:
                    565:        if (cvs_server_active == 0)
                    566:                cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72      niallo    567:
1.79      joris     568:        repo[0] = '\0';
                    569:        rpath[0] = '\0';
1.72      niallo    570:
1.79      joris     571:        if (cvs_cmdop == CVS_OP_UPDATE) {
                    572:                if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
1.91      thib      573:                        if ((fgets(repo, sizeof(repo), fp)) == NULL)
                    574:                                fatal("cvs_mkpath: bad repository file");
1.120     gilles    575:                        repo[strcspn(repo, "\n")] = '\0';
1.79      joris     576:                        (void)fclose(fp);
                    577:                }
1.72      niallo    578:        }
                    579:
1.79      joris     580:        for (sp = dir; sp != NULL; sp = dp) {
                    581:                dp = strchr(sp, '/');
                    582:                if (dp != NULL)
                    583:                        *(dp++) = '\0';
                    584:
1.130     joris     585:                if (sp == dir && module_repo_root != NULL) {
                    586:                        len = strlcpy(repo, module_repo_root, sizeof(repo));
                    587:                        if (len >= (int)sizeof(repo))
                    588:                                fatal("cvs_mkpath: overflow");
                    589:                } else {
                    590:                        if (repo[0] != '\0') {
                    591:                                len = strlcat(repo, "/", sizeof(repo));
                    592:                                if (len >= (int)sizeof(repo))
                    593:                                        fatal("cvs_mkpath: overflow");
                    594:                        }
                    595:
                    596:                        len = strlcat(repo, sp, sizeof(repo));
1.79      joris     597:                        if (len >= (int)sizeof(repo))
                    598:                                fatal("cvs_mkpath: overflow");
                    599:                }
                    600:
                    601:                if (rpath[0] != '\0') {
                    602:                        len = strlcat(rpath, "/", sizeof(rpath));
                    603:                        if (len >= (int)sizeof(rpath))
                    604:                                fatal("cvs_mkpath: overflow");
                    605:                }
                    606:
                    607:                len = strlcat(rpath, sp, sizeof(rpath));
                    608:                if (len >= (int)sizeof(rpath))
                    609:                        fatal("cvs_mkpath: overflow");
                    610:
                    611:                if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
                    612:                        fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.125     tobias    613:
                    614:                if (cvs_cmdop == CVS_OP_EXPORT && !cvs_server_active)
                    615:                        continue;
1.72      niallo    616:
1.98      joris     617:                cvs_mkadmin(rpath, current_cvsroot->cr_str, repo,
1.131     tobias    618:                    tag, NULL);
1.122     tobias    619:
                    620:                if (dp != NULL) {
                    621:                        if ((p = strchr(dp, '/')) != NULL)
                    622:                                *p = '\0';
                    623:                        ent = cvs_ent_open(rpath);
1.126     tobias    624:                        xsnprintf(entry, sizeof(entry), "D/%s////", dp);
1.122     tobias    625:                        cvs_ent_add(ent, entry);
                    626:                        cvs_ent_close(ent, ENT_SYNC);
                    627:                        if (p != NULL)
                    628:                                *p = '/';
                    629:                }
1.112     joris     630:
                    631:                if (cvs_server_active == 1 && strcmp(rpath, ".")) {
1.119     joris     632:                        if (tag != NULL) {
                    633:                                (void)xsnprintf(sticky, sizeof(sticky),
                    634:                                    "T%s", tag);
                    635:                                cvs_server_set_sticky(rpath, sticky);
                    636:                        }
1.112     joris     637:                }
1.79      joris     638:        }
1.72      niallo    639:
1.79      joris     640:        xfree(dir);
1.72      niallo    641: }
1.54      joris     642:
                    643: /*
                    644:  * Split the contents of a file into a list of lines.
                    645:  */
                    646: struct cvs_lines *
1.106     otto      647: cvs_splitlines(u_char *data, size_t len)
1.54      joris     648: {
1.97      niallo    649:        u_char *p, *c;
                    650:        size_t i, tlen;
1.54      joris     651:        struct cvs_lines *lines;
                    652:        struct cvs_line *lp;
                    653:
1.124     tobias    654:        lines = xcalloc(1, sizeof(*lines));
1.54      joris     655:        TAILQ_INIT(&(lines->l_lines));
                    656:
1.124     tobias    657:        lp = xcalloc(1, sizeof(*lp));
1.54      joris     658:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    659:
1.97      niallo    660:        p = c = data;
                    661:        for (i = 0; i < len; i++) {
                    662:                if (*p == '\n' || (i == len - 1)) {
                    663:                        tlen = p - c + 1;
1.124     tobias    664:                        lp = xcalloc(1, sizeof(*lp));
1.97      niallo    665:                        lp->l_line = c;
                    666:                        lp->l_len = tlen;
                    667:                        lp->l_lineno = ++(lines->l_nblines);
                    668:                        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    669:                        c = p + 1;
                    670:                }
                    671:                p++;
1.54      joris     672:        }
                    673:
                    674:        return (lines);
                    675: }
                    676:
                    677: void
                    678: cvs_freelines(struct cvs_lines *lines)
                    679: {
                    680:        struct cvs_line *lp;
                    681:
                    682:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    683:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.102     niallo    684:                if (lp->l_needsfree == 1)
                    685:                        xfree(lp->l_line);
1.59      joris     686:                xfree(lp);
1.54      joris     687:        }
                    688:
1.59      joris     689:        xfree(lines);
1.70      niallo    690: }
                    691:
1.71      xsa       692: /*
                    693:  * cvs_strsplit()
                    694:  *
                    695:  * Split a string <str> of <sep>-separated values and allocate
                    696:  * an argument vector for the values found.
                    697:  */
1.73      pat       698: struct cvs_argvector *
1.71      xsa       699: cvs_strsplit(char *str, const char *sep)
                    700: {
1.73      pat       701:        struct cvs_argvector *av;
1.76      ray       702:        size_t i = 0;
1.71      xsa       703:        char *cp, *p;
                    704:
                    705:        cp = xstrdup(str);
1.77      ray       706:        av = xmalloc(sizeof(*av));
1.73      pat       707:        av->str = cp;
1.113     ray       708:        av->argv = xmalloc(sizeof(*(av->argv)));
1.71      xsa       709:
                    710:        while ((p = strsep(&cp, sep)) != NULL) {
1.73      pat       711:                av->argv[i++] = p;
1.111     ray       712:                av->argv = xrealloc(av->argv,
1.77      ray       713:                    i + 1, sizeof(*(av->argv)));
1.71      xsa       714:        }
1.73      pat       715:        av->argv[i] = NULL;
                    716:
                    717:        return (av);
                    718: }
1.71      xsa       719:
1.73      pat       720: /*
                    721:  * cvs_argv_destroy()
                    722:  *
                    723:  * Free an argument vector previously allocated by cvs_strsplit().
                    724:  */
                    725: void
                    726: cvs_argv_destroy(struct cvs_argvector *av)
                    727: {
                    728:        xfree(av->str);
                    729:        xfree(av->argv);
                    730:        xfree(av);
1.82      joris     731: }
                    732:
                    733: u_int
                    734: cvs_revision_select(RCSFILE *file, char *range)
                    735: {
                    736:        int i;
                    737:        u_int nrev;
                    738:        char *lstr, *rstr;
                    739:        struct rcs_delta *rdp;
                    740:        struct cvs_argvector *revargv, *revrange;
                    741:        RCSNUM *lnum, *rnum;
                    742:
                    743:        nrev = 0;
                    744:        lnum = rnum = NULL;
                    745:
                    746:        revargv = cvs_strsplit(range, ",");
                    747:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    748:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    749:                if (revrange->argv[0] == NULL)
                    750:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    751:                else if (revrange->argv[1] == NULL)
                    752:                        lstr = rstr = revrange->argv[0];
                    753:                else {
                    754:                        if (revrange->argv[2] != NULL)
                    755:                                fatal("invalid revision range: %s",
                    756:                                    revargv->argv[i]);
                    757:
                    758:                        lstr = revrange->argv[0];
                    759:                        rstr = revrange->argv[1];
                    760:
                    761:                        if (strcmp(lstr, "") == 0)
                    762:                                lstr = NULL;
                    763:                        if (strcmp(rstr, "") == 0)
                    764:                                rstr = NULL;
                    765:                }
                    766:
                    767:                if (lstr == NULL)
                    768:                        lstr = RCS_HEAD_INIT;
                    769:
1.100     niallo    770:                if ((lnum = rcs_translate_tag(lstr, file)) == NULL)
                    771:                        fatal("cvs_revision_select: could not translate tag `%s'", lstr);
1.82      joris     772:
                    773:                if (rstr != NULL) {
1.100     niallo    774:                        if ((rnum = rcs_translate_tag(rstr, file)) == NULL)
                    775:                                fatal("cvs_revision_select: could not translate tag `%s'", rstr);
1.82      joris     776:                } else {
                    777:                        rnum = rcsnum_alloc();
                    778:                        rcsnum_cpy(file->rf_head, rnum, 0);
                    779:                }
                    780:
                    781:                cvs_argv_destroy(revrange);
                    782:
                    783:                TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    784:                        if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
                    785:                            rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
                    786:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    787:                                rdp->rd_flags |= RCS_RD_SELECT;
                    788:                                nrev++;
                    789:                        }
                    790:                }
1.129     joris     791:
                    792:                rcsnum_free(lnum);
                    793:                rcsnum_free(rnum);
1.82      joris     794:        }
                    795:
                    796:        cvs_argv_destroy(revargv);
                    797:
                    798:        return (nrev);
1.95      xsa       799: }
                    800:
                    801: int
                    802: cvs_yesno(void)
                    803: {
                    804:        int c, ret;
                    805:
                    806:        ret = 0;
                    807:
                    808:        fflush(stderr);
                    809:        fflush(stdout);
                    810:
                    811:        if ((c = getchar()) != 'y' && c != 'Y')
                    812:                ret = -1;
                    813:        else
                    814:                while (c != EOF && c != '\n')
                    815:                        c = getchar();
                    816:
                    817:        return (ret);
1.132     joris     818: }
                    819:
                    820: void
                    821: cvs_exec(const char *prog)
                    822: {
                    823:        pid_t pid;
                    824:        char *argp[] = { "sh", "-c", NULL, NULL };
                    825:
                    826:        argp[2] = prog;
                    827:
                    828:        if ((pid = fork()) == -1) {
                    829:                cvs_log(LP_ERR, "cvs_exec: fork failed");
                    830:                return;
                    831:        } else if (pid == 0) {
                    832:                execv(_PATH_BSHELL, argp);
                    833:                cvs_log(LP_ERR, "failed to run '%s'", prog);
                    834:                _exit(127);
                    835:        }
1.71      xsa       836: }