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

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