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

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