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

1.85    ! joris       1: /*     $OpenBSD: util.c,v 1.84 2006/05/30 07:00:30 joris Exp $ */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.71      xsa         4:  * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
                      5:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.9       jfb         6:  * All rights reserved.
1.1       jfb         7:  *
1.9       jfb         8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
1.1       jfb        11:  *
1.9       jfb        12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        14:  * 2. The name of the author may not be used to endorse or promote products
1.9       jfb        15:  *    derived from this software without specific prior written permission.
1.1       jfb        16:  *
                     17:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     18:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     19:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     20:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     21:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     22:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     23:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     24:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     25:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.9       jfb        26:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        27:  */
                     28:
1.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.79      joris     102:                        cvs_log(LP_ERR, "failed to scan mode string `%s'", sp);
1.1       jfb       103:                        continue;
                    104:                }
                    105:
1.78      deraadt   106:                if (type <= 'a' || type >= 'z' ||
                    107:                    cvs_modetypes[type - 'a'] == -1) {
1.79      joris     108:                        cvs_log(LP_ERR,
1.1       jfb       109:                            "invalid mode type `%c'"
                    110:                            " (`u', `g' or `o' expected), ignoring", type);
                    111:                        continue;
                    112:                }
                    113:
                    114:                /* make type contain the actual mode index */
                    115:                type = cvs_modetypes[type - 'a'];
                    116:
                    117:                for (sp = ms; *sp != '\0'; sp++) {
1.78      deraadt   118:                        if (*sp <= 'a' || *sp >= 'z' ||
                    119:                            cvs_modes[(int)type][*sp - 'a'] == 0) {
1.79      joris     120:                                cvs_log(LP_ERR,
1.1       jfb       121:                                    "invalid permission bit `%c'", *sp);
1.15      deraadt   122:                        } else
1.5       jfb       123:                                m |= cvs_modes[(int)type][*sp - 'a'];
1.1       jfb       124:                }
                    125:        }
                    126:
                    127:        *mode = m;
                    128: }
                    129:
                    130: /*
                    131:  * cvs_modetostr()
                    132:  *
1.30      jfb       133:  * Generate a CVS-format string to represent the permissions mask on a file
                    134:  * from the mode <mode> and store the result in <buf>, which can accept up to
                    135:  * <len> bytes (including the terminating NUL byte).  The result is guaranteed
                    136:  * to be NUL-terminated.
1.1       jfb       137:  */
1.65      joris     138: void
1.1       jfb       139: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    140: {
                    141:        char tmp[16], *bp;
                    142:        mode_t um, gm, om;
                    143:
                    144:        um = (mode & S_IRWXU) >> 6;
                    145:        gm = (mode & S_IRWXG) >> 3;
                    146:        om = mode & S_IRWXO;
                    147:
                    148:        bp = buf;
                    149:        *bp = '\0';
                    150:
                    151:        if (um) {
1.68      xsa       152:                if (strlcpy(tmp, "u=", sizeof(tmp)) >= sizeof(tmp) ||
                    153:                    strlcat(tmp, cvs_modestr[um], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     154:                        fatal("cvs_modetostr: overflow for user mode");
                    155:
1.68      xsa       156:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     157:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       158:        }
1.65      joris     159:
1.1       jfb       160:        if (gm) {
1.65      joris     161:                if (um) {
1.68      xsa       162:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     163:                                fatal("cvs_modetostr: string truncation");
                    164:                }
                    165:
1.68      xsa       166:                if (strlcpy(tmp, "g=", sizeof(tmp)) >= sizeof(tmp) ||
                    167:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     168:                        fatal("cvs_modetostr: overflow for group mode");
                    169:
1.68      xsa       170:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     171:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       172:        }
1.65      joris     173:
1.1       jfb       174:        if (om) {
1.65      joris     175:                if (um || gm) {
1.68      xsa       176:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     177:                                fatal("cvs_modetostr: string truncation");
                    178:                }
                    179:
1.68      xsa       180:                if (strlcpy(tmp, "o=", sizeof(tmp)) >= sizeof(tmp) ||
                    181:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     182:                        fatal("cvs_modetostr: overflow for others mode");
                    183:
1.68      xsa       184:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     185:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       186:        }
                    187: }
                    188:
                    189: /*
                    190:  * cvs_cksum()
                    191:  *
                    192:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    193:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    194:  * given in <len> and must be at least 33.
                    195:  * Returns 0 on success, or -1 on failure.
                    196:  */
                    197: int
                    198: cvs_cksum(const char *file, char *dst, size_t len)
                    199: {
                    200:        if (len < CVS_CKSUM_LEN) {
1.79      joris     201:                cvs_log(LP_ERR, "buffer too small for checksum");
1.1       jfb       202:                return (-1);
                    203:        }
                    204:        if (MD5File(file, dst) == NULL) {
1.79      joris     205:                cvs_log(LP_ERR, "failed to generate checksum for %s", file);
1.1       jfb       206:                return (-1);
                    207:        }
                    208:
                    209:        return (0);
                    210: }
                    211:
                    212: /*
                    213:  * cvs_splitpath()
                    214:  *
1.7       jfb       215:  * Split a path <path> into the base portion and the filename portion.
                    216:  * The path is copied in <base> and the last delimiter is replaced by a NUL
                    217:  * byte.  The <file> pointer is set to point to the first character after
                    218:  * that delimiter.
1.1       jfb       219:  * Returns 0 on success, or -1 on failure.
                    220:  */
1.65      joris     221: void
1.7       jfb       222: cvs_splitpath(const char *path, char *base, size_t blen, char **file)
1.1       jfb       223: {
                    224:        size_t rlen;
1.7       jfb       225:        char *sp;
1.1       jfb       226:
1.7       jfb       227:        if ((rlen = strlcpy(base, path, blen)) >= blen)
1.60      xsa       228:                fatal("cvs_splitpath: path truncation");
1.6       jfb       229:
1.78      deraadt   230:        while (rlen > 0 && base[rlen - 1] == '/')
1.7       jfb       231:                base[--rlen] = '\0';
                    232:
                    233:        sp = strrchr(base, '/');
1.1       jfb       234:        if (sp == NULL) {
1.65      joris     235:                rlen = strlcpy(base, "./", blen);
                    236:                if (rlen >= blen)
                    237:                        fatal("cvs_splitpath: path truncation");
                    238:
                    239:                rlen = strlcat(base, path, blen);
                    240:                if (rlen >= blen)
                    241:                        fatal("cvs_splitpath: path truncation");
                    242:
1.7       jfb       243:                sp = base + 1;
1.1       jfb       244:        }
                    245:
1.7       jfb       246:        *sp = '\0';
                    247:        if (file != NULL)
                    248:                *file = sp + 1;
1.1       jfb       249: }
                    250:
                    251: /*
                    252:  * cvs_getargv()
                    253:  *
                    254:  * Parse a line contained in <line> and generate an argument vector by
                    255:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    256:  * <argv>, which can accept up to <argvlen> entries.
1.20      david     257:  * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1       jfb       258:  */
                    259: int
                    260: cvs_getargv(const char *line, char **argv, int argvlen)
                    261: {
1.65      joris     262:        size_t l;
1.1       jfb       263:        u_int i;
1.67      xsa       264:        int argc, error;
1.1       jfb       265:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    266:
1.65      joris     267:        l = strlcpy(linebuf, line, sizeof(linebuf));
                    268:        if (l >= sizeof(linebuf))
                    269:                fatal("cvs_getargv: string truncation");
                    270:
1.27      pat       271:        memset(argv, 0, argvlen * sizeof(char *));
1.1       jfb       272:        argc = 0;
                    273:
                    274:        /* build the argument vector */
1.67      xsa       275:        error = 0;
1.1       jfb       276:        for (lp = linebuf; lp != NULL;) {
                    277:                if (*lp == '"') {
                    278:                        /* double-quoted string */
                    279:                        lp++;
                    280:                        i = 0;
                    281:                        memset(qbuf, 0, sizeof(qbuf));
                    282:                        while (*lp != '"') {
1.16      jfb       283:                                if (*lp == '\\')
                    284:                                        lp++;
1.1       jfb       285:                                if (*lp == '\0') {
                    286:                                        cvs_log(LP_ERR, "no terminating quote");
1.67      xsa       287:                                        error++;
1.1       jfb       288:                                        break;
1.16      jfb       289:                                }
1.1       jfb       290:
1.9       jfb       291:                                qbuf[i++] = *lp++;
1.1       jfb       292:                                if (i == sizeof(qbuf)) {
1.67      xsa       293:                                        error++;
1.1       jfb       294:                                        break;
                    295:                                }
                    296:                        }
                    297:
                    298:                        arg = qbuf;
1.15      deraadt   299:                } else {
1.1       jfb       300:                        cp = strsep(&lp, " \t");
                    301:                        if (cp == NULL)
                    302:                                break;
                    303:                        else if (*cp == '\0')
                    304:                                continue;
                    305:
                    306:                        arg = cp;
                    307:                }
                    308:
1.16      jfb       309:                if (argc == argvlen) {
1.67      xsa       310:                        error++;
1.16      jfb       311:                        break;
                    312:                }
                    313:
1.59      joris     314:                argv[argc] = xstrdup(arg);
1.1       jfb       315:                argc++;
                    316:        }
                    317:
1.67      xsa       318:        if (error != 0) {
1.1       jfb       319:                /* ditch the argument vector */
                    320:                for (i = 0; i < (u_int)argc; i++)
1.59      joris     321:                        xfree(argv[i]);
1.1       jfb       322:                argc = -1;
                    323:        }
                    324:
                    325:        return (argc);
1.17      jfb       326: }
                    327:
                    328: /*
                    329:  * cvs_makeargv()
                    330:  *
1.20      david     331:  * Allocate an argument vector large enough to accommodate for all the
1.17      jfb       332:  * arguments found in <line> and return it.
                    333:  */
1.42      xsa       334: char **
1.17      jfb       335: cvs_makeargv(const char *line, int *argc)
                    336: {
                    337:        int i, ret;
                    338:        char *argv[1024], **copy;
                    339:
                    340:        ret = cvs_getargv(line, argv, 1024);
                    341:        if (ret == -1)
                    342:                return (NULL);
                    343:
1.77      ray       344:        copy = xcalloc(ret + 1, sizeof(char *));
1.17      jfb       345:
                    346:        for (i = 0; i < ret; i++)
                    347:                copy[i] = argv[i];
                    348:        copy[ret] = NULL;
                    349:
                    350:        *argc = ret;
                    351:        return (copy);
1.1       jfb       352: }
                    353:
                    354: /*
                    355:  * cvs_freeargv()
                    356:  *
                    357:  * Free an argument vector previously generated by cvs_getargv().
                    358:  */
                    359: void
                    360: cvs_freeargv(char **argv, int argc)
                    361: {
                    362:        int i;
                    363:
                    364:        for (i = 0; i < argc; i++)
1.16      jfb       365:                if (argv[i] != NULL)
1.59      joris     366:                        xfree(argv[i]);
1.2       jfb       367: }
                    368:
1.11      krapht    369: /*
                    370:  * cvs_exec()
                    371:  */
                    372: int
                    373: cvs_exec(int argc, char **argv, int fds[3])
                    374: {
                    375:        int ret;
                    376:        pid_t pid;
                    377:
                    378:        if ((pid = fork()) == -1) {
1.79      joris     379:                cvs_log(LP_ERR, "failed to fork");
1.11      krapht    380:                return (-1);
                    381:        } else if (pid == 0) {
                    382:                execvp(argv[0], argv);
1.79      joris     383:                cvs_log(LP_ERR, "failed to exec %s", argv[0]);
1.13      jfb       384:                exit(1);
1.11      krapht    385:        }
                    386:
                    387:        if (waitpid(pid, &ret, 0) == -1)
1.79      joris     388:                cvs_log(LP_ERR, "failed to waitpid");
1.11      krapht    389:
                    390:        return (ret);
1.38      xsa       391: }
                    392:
                    393: /*
                    394:  * cvs_chdir()
                    395:  *
1.63      xsa       396:  * Change to directory <path>.
                    397:  * If <rm> is equal to `1', <path> is removed if chdir() fails so we
                    398:  * do not have temporary directories leftovers.
1.60      xsa       399:  * Returns 0 on success.
1.50      xsa       400:  */
1.38      xsa       401: int
1.63      xsa       402: cvs_chdir(const char *path, int rm)
1.38      xsa       403: {
1.63      xsa       404:        if (chdir(path) == -1) {
                    405:                if (rm == 1)
                    406:                        cvs_unlink(path);
1.60      xsa       407:                fatal("cvs_chdir: `%s': %s", path, strerror(errno));
1.63      xsa       408:        }
1.49      xsa       409:
                    410:        return (0);
                    411: }
                    412:
                    413: /*
                    414:  * cvs_rename()
                    415:  * Change the name of a file.
                    416:  * rename() wrapper with an error message.
1.60      xsa       417:  * Returns 0 on success.
1.49      xsa       418:  */
                    419: int
                    420: cvs_rename(const char *from, const char *to)
                    421: {
                    422:        cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
                    423:
                    424:        if (cvs_noexec == 1)
                    425:                return (0);
                    426:
1.60      xsa       427:        if (rename(from, to) == -1)
                    428:                fatal("cvs_rename: `%s'->`%s': %s", from, to, strerror(errno));
1.40      xsa       429:
                    430:        return (0);
                    431: }
                    432:
                    433: /*
                    434:  * cvs_unlink()
                    435:  *
                    436:  * Removes the link named by <path>.
                    437:  * unlink() wrapper with an error message.
                    438:  * Returns 0 on success, or -1 on failure.
                    439:  */
                    440: int
                    441: cvs_unlink(const char *path)
                    442: {
                    443:        cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
                    444:
                    445:        if (cvs_noexec == 1)
                    446:                return (0);
                    447:
1.78      deraadt   448:        if (unlink(path) == -1 && errno != ENOENT) {
1.79      joris     449:                cvs_log(LP_ERR, "cannot remove `%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:
                    471:        cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
                    472:
                    473:        if (cvs_noexec == 1)
                    474:                return (0);
1.24      joris     475:
                    476:        if ((dirp = opendir(path)) == NULL) {
1.79      joris     477:                cvs_log(LP_ERR, "failed to open '%s'", path);
1.30      jfb       478:                return (-1);
1.24      joris     479:        }
                    480:
                    481:        while ((ent = readdir(dirp)) != NULL) {
                    482:                if (!strcmp(ent->d_name, ".") ||
                    483:                    !strcmp(ent->d_name, ".."))
                    484:                        continue;
                    485:
1.30      jfb       486:                len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
1.33      pat       487:                if (len >= sizeof(fpath))
1.65      joris     488:                        fatal("cvs_rmdir: path truncation");
1.24      joris     489:
                    490:                if (ent->d_type == DT_DIR) {
1.45      xsa       491:                        if (cvs_rmdir(fpath) == -1)
1.33      pat       492:                                goto done;
1.78      deraadt   493:                } else if (cvs_unlink(fpath) == -1 && errno != ENOENT)
1.33      pat       494:                        goto done;
1.24      joris     495:        }
                    496:
                    497:
1.78      deraadt   498:        if (rmdir(path) == -1 && errno != ENOENT) {
1.79      joris     499:                cvs_log(LP_ERR, "failed to remove '%s'", path);
1.33      pat       500:                goto done;
1.30      jfb       501:        }
1.24      joris     502:
1.33      pat       503:        ret = 0;
                    504: done:
                    505:        closedir(dirp);
1.34      joris     506:        return (ret);
                    507: }
                    508:
                    509: /*
1.30      jfb       510:  * cvs_path_cat()
                    511:  *
                    512:  * Concatenate the two paths <base> and <end> and store the generated path
                    513:  * into the buffer <dst>, which can accept up to <dlen> bytes, including the
                    514:  * NUL byte.  The result is guaranteed to be NUL-terminated.
                    515:  * Returns the number of bytes necessary to store the full resulting path,
                    516:  * not including the NUL byte (a value equal to or larger than <dlen>
                    517:  * indicates truncation).
                    518:  */
1.29      jfb       519: size_t
                    520: cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
                    521: {
                    522:        size_t len;
                    523:
                    524:        len = strlcpy(dst, base, dlen);
                    525:        if (len >= dlen - 1) {
                    526:                errno = ENAMETOOLONG;
1.79      joris     527:                fatal("overflow in cvs_path_cat");
1.29      jfb       528:        } else {
                    529:                dst[len] = '/';
                    530:                dst[len + 1] = '\0';
                    531:                len = strlcat(dst, end, dlen);
                    532:                if (len >= dlen) {
                    533:                        errno = ENAMETOOLONG;
1.79      joris     534:                        cvs_log(LP_ERR, "%s", dst);
1.29      jfb       535:                }
                    536:        }
                    537:
                    538:        return (len);
1.35      xsa       539: }
                    540:
                    541: /*
1.79      joris     542:  * a hack to mimic and thus match gnu cvs behaviour.
1.35      xsa       543:  */
1.79      joris     544: time_t
                    545: cvs_hack_time(time_t oldtime, int togmt)
1.35      xsa       546: {
1.79      joris     547:        int l;
                    548:        struct tm *t;
                    549:        char tbuf[32];
                    550:
                    551:        if (togmt == 1) {
                    552:                t = gmtime(&oldtime);
                    553:                if (t == NULL)
                    554:                        return (0);
1.35      xsa       555:
1.79      joris     556:                return (mktime(t));
                    557:        }
1.35      xsa       558:
1.79      joris     559:        t = localtime(&oldtime);
1.35      xsa       560:
1.79      joris     561:        l = snprintf(tbuf, sizeof(tbuf), "%d/%d/%d GMT %d:%d:%d",
                    562:            t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
                    563:            t->tm_min, t->tm_sec);
                    564:        if (l == -1 || l >= (int)sizeof(tbuf))
                    565:                return (0);
                    566:
                    567:        return (cvs_date_parse(tbuf));
1.51      xsa       568: }
                    569:
                    570: void
1.81      joris     571: cvs_get_repository_path(const char *dir, char *dst, size_t len)
                    572: {
                    573:        int l;
                    574:        char buf[MAXPATHLEN];
                    575:
                    576:        cvs_get_repository_name(dir, buf, sizeof(buf));
                    577:        l = snprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
                    578:        if (l == -1 || l >= (int)len)
                    579:                fatal("cvs_get_repository_path: overflow");
                    580: }
                    581:
                    582: void
                    583: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51      xsa       584: {
1.79      joris     585:        int l;
1.51      xsa       586:        FILE *fp;
1.81      joris     587:        char *s, fpath[MAXPATHLEN];
1.79      joris     588:
                    589:        l = snprintf(fpath, sizeof(fpath), "%s/%s", dir, CVS_PATH_REPOSITORY);
                    590:        if (l == -1 || l >= (int)sizeof(fpath))
1.81      joris     591:                fatal("cvs_get_repository_name: overflow");
1.79      joris     592:
                    593:        if ((fp = fopen(fpath, "r")) != NULL) {
1.81      joris     594:                fgets(dst, len, fp);
1.51      xsa       595:
1.81      joris     596:                if ((s = strchr(dst, '\n')) != NULL)
1.79      joris     597:                        *s = '\0';
1.51      xsa       598:
                    599:                (void)fclose(fp);
1.81      joris     600:        } else {
1.84      joris     601:                dst[0] = '\0';
                    602:
                    603:                if (cvs_cmdop == CVS_OP_IMPORT) {
                    604:                        if (strlcpy(dst, import_repository, len) >= len)
1.85    ! joris     605:                                fatal("cvs_get_repository_name: truncation");
1.84      joris     606:                        if (strlcat(dst, "/", len) >= len)
1.85    ! joris     607:                                fatal("cvs_get_repository_name: truncation");
        !           608:
        !           609:                        if (strcmp(dir, ".")) {
        !           610:                                if (strlcat(dst, dir, len) >= len) {
        !           611:                                        fatal("cvs_get_repository_name: "
        !           612:                                            "truncation");
        !           613:                                }
        !           614:                        }
        !           615:                } else {
        !           616:                        if (strlcat(dst, dir, len) >= len)
        !           617:                                fatal("cvs_get_repository_name: truncation");
1.84      joris     618:                }
1.51      xsa       619:        }
                    620: }
                    621:
                    622: void
1.79      joris     623: cvs_mkadmin(const char *path, const char *root, const char *repo)
1.51      xsa       624: {
                    625:        FILE *fp;
                    626:        size_t len;
1.79      joris     627:        struct stat st;
                    628:        char buf[MAXPATHLEN];
1.51      xsa       629:
1.79      joris     630:        cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s)", path, root, repo);
1.51      xsa       631:
1.79      joris     632:        len = cvs_path_cat(path, CVS_PATH_CVSDIR, buf, sizeof(buf));
                    633:        if (len >= sizeof(buf))
                    634:                fatal("cvs_mkadmin: truncation");
1.51      xsa       635:
1.79      joris     636:        if (stat(buf, &st) != -1)
                    637:                return;
1.51      xsa       638:
1.79      joris     639:        if (mkdir(buf, 0755) == -1 && errno != EEXIST)
                    640:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    641:
                    642:        len = cvs_path_cat(path, CVS_PATH_ROOTSPEC, buf, sizeof(buf));
                    643:        if (len >= sizeof(buf))
                    644:                fatal("cvs_mkadmin: truncation");
                    645:
                    646:        if ((fp = fopen(buf, "w")) == NULL)
                    647:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    648:
                    649:        fprintf(fp, "%s\n", root);
                    650:        (void)fclose(fp);
1.51      xsa       651:
1.79      joris     652:        len = cvs_path_cat(path, CVS_PATH_REPOSITORY, buf, sizeof(buf));
                    653:        if (len >= sizeof(buf))
                    654:                fatal("cvs_mkadmin: truncation");
1.51      xsa       655:
1.79      joris     656:        if ((fp = fopen(buf, "w")) == NULL)
                    657:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51      xsa       658:
1.79      joris     659:        fprintf(fp, "%s\n", repo);
                    660:        (void)fclose(fp);
1.51      xsa       661:
1.79      joris     662:        len = cvs_path_cat(path, CVS_PATH_ENTRIES, buf, sizeof(buf));
                    663:        if (len >= sizeof(buf))
                    664:                fatal("cvs_mkadmin: truncation");
1.51      xsa       665:
1.79      joris     666:        if ((fp = fopen(buf, "w")) == NULL)
                    667:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51      xsa       668:        (void)fclose(fp);
1.54      joris     669: }
                    670:
1.79      joris     671: void
                    672: cvs_mkpath(const char *path)
1.72      niallo    673: {
1.79      joris     674:        FILE *fp;
                    675:        size_t len;
                    676:        struct stat st;
                    677:        char *sp, *dp, *dir, rpath[MAXPATHLEN], repo[MAXPATHLEN];
                    678:
                    679:        dir = xstrdup(path);
                    680:
                    681:        STRIP_SLASH(dir);
                    682:        cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72      niallo    683:
1.79      joris     684:        repo[0] = '\0';
                    685:        rpath[0] = '\0';
1.72      niallo    686:
1.79      joris     687:        if (cvs_cmdop == CVS_OP_UPDATE) {
                    688:                if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
                    689:                        fgets(repo, sizeof(repo), fp);
                    690:                        if (repo[strlen(repo) - 1] == '\n')
                    691:                                repo[strlen(repo) - 1] = '\0';
                    692:                        (void)fclose(fp);
                    693:                }
1.72      niallo    694:        }
                    695:
1.79      joris     696:        for (sp = dir; sp != NULL; sp = dp) {
                    697:                dp = strchr(sp, '/');
                    698:                if (dp != NULL)
                    699:                        *(dp++) = '\0';
                    700:
                    701:                if (repo[0] != '\0') {
                    702:                        len = strlcat(repo, "/", sizeof(repo));
                    703:                        if (len >= (int)sizeof(repo))
                    704:                                fatal("cvs_mkpath: overflow");
                    705:                }
                    706:
                    707:                len = strlcat(repo, sp, sizeof(repo));
                    708:                if (len >= (int)sizeof(repo))
                    709:                        fatal("cvs_mkpath: overflow");
                    710:
                    711:                if (rpath[0] != '\0') {
                    712:                        len = strlcat(rpath, "/", sizeof(rpath));
                    713:                        if (len >= (int)sizeof(rpath))
                    714:                                fatal("cvs_mkpath: overflow");
                    715:                }
                    716:
                    717:                len = strlcat(rpath, sp, sizeof(rpath));
                    718:                if (len >= (int)sizeof(rpath))
                    719:                        fatal("cvs_mkpath: overflow");
                    720:
                    721:                if (stat(repo, &st) != -1)
                    722:                        continue;
                    723:
                    724:                if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
                    725:                        fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.72      niallo    726:
1.79      joris     727:                cvs_mkadmin(rpath, current_cvsroot->cr_dir, repo);
                    728:        }
1.72      niallo    729:
1.79      joris     730:        xfree(dir);
1.72      niallo    731: }
1.54      joris     732:
                    733: /*
                    734:  * Split the contents of a file into a list of lines.
                    735:  */
                    736: struct cvs_lines *
                    737: cvs_splitlines(const char *fcont)
                    738: {
                    739:        char *dcp;
                    740:        struct cvs_lines *lines;
                    741:        struct cvs_line *lp;
                    742:
1.77      ray       743:        lines = xmalloc(sizeof(*lines));
1.54      joris     744:        TAILQ_INIT(&(lines->l_lines));
                    745:        lines->l_nblines = 0;
1.59      joris     746:        lines->l_data = xstrdup(fcont);
1.54      joris     747:
1.77      ray       748:        lp = xmalloc(sizeof(*lp));
1.54      joris     749:        lp->l_line = NULL;
                    750:        lp->l_lineno = 0;
                    751:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    752:
                    753:        for (dcp = lines->l_data; *dcp != '\0';) {
1.77      ray       754:                lp = xmalloc(sizeof(*lp));
1.54      joris     755:                lp->l_line = dcp;
                    756:                lp->l_lineno = ++(lines->l_nblines);
                    757:                TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    758:
                    759:                dcp = strchr(dcp, '\n');
                    760:                if (dcp == NULL)
                    761:                        break;
                    762:                *(dcp++) = '\0';
                    763:        }
                    764:
                    765:        return (lines);
                    766: }
                    767:
                    768: void
                    769: cvs_freelines(struct cvs_lines *lines)
                    770: {
                    771:        struct cvs_line *lp;
                    772:
                    773:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    774:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.59      joris     775:                xfree(lp);
1.54      joris     776:        }
                    777:
1.59      joris     778:        xfree(lines->l_data);
                    779:        xfree(lines);
1.54      joris     780: }
                    781:
                    782: BUF *
                    783: cvs_patchfile(const char *data, const char *patch,
                    784:     int (*p)(struct cvs_lines *, struct cvs_lines *))
                    785: {
                    786:        struct cvs_lines *dlines, *plines;
                    787:        struct cvs_line *lp;
                    788:        size_t len;
                    789:        int lineno;
                    790:        BUF *res;
                    791:
                    792:        len = strlen(data);
                    793:
                    794:        if ((dlines = cvs_splitlines(data)) == NULL)
                    795:                return (NULL);
                    796:
                    797:        if ((plines = cvs_splitlines(patch)) == NULL)
                    798:                return (NULL);
                    799:
                    800:        if (p(dlines, plines) < 0) {
                    801:                cvs_freelines(dlines);
                    802:                cvs_freelines(plines);
                    803:                return (NULL);
                    804:        }
                    805:
                    806:        lineno = 0;
1.62      joris     807:        res = cvs_buf_alloc(len, BUF_AUTOEXT);
1.54      joris     808:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
                    809:                if (lineno != 0)
                    810:                        cvs_buf_fappend(res, "%s\n", lp->l_line);
                    811:                lineno++;
                    812:        }
                    813:
                    814:        cvs_freelines(dlines);
                    815:        cvs_freelines(plines);
                    816:        return (res);
1.70      niallo    817: }
                    818:
1.71      xsa       819: /*
                    820:  * cvs_strsplit()
                    821:  *
                    822:  * Split a string <str> of <sep>-separated values and allocate
                    823:  * an argument vector for the values found.
                    824:  */
1.73      pat       825: struct cvs_argvector *
1.71      xsa       826: cvs_strsplit(char *str, const char *sep)
                    827: {
1.73      pat       828:        struct cvs_argvector *av;
1.76      ray       829:        size_t i = 0;
1.73      pat       830:        char **nargv;
1.71      xsa       831:        char *cp, *p;
                    832:
                    833:        cp = xstrdup(str);
1.77      ray       834:        av = xmalloc(sizeof(*av));
1.73      pat       835:        av->str = cp;
1.77      ray       836:        av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
1.71      xsa       837:
                    838:        while ((p = strsep(&cp, sep)) != NULL) {
1.73      pat       839:                av->argv[i++] = p;
1.77      ray       840:                nargv = xrealloc(av->argv,
                    841:                    i + 1, sizeof(*(av->argv)));
1.73      pat       842:                av->argv = nargv;
1.71      xsa       843:        }
1.73      pat       844:        av->argv[i] = NULL;
                    845:
                    846:        return (av);
                    847: }
1.71      xsa       848:
1.73      pat       849: /*
                    850:  * cvs_argv_destroy()
                    851:  *
                    852:  * Free an argument vector previously allocated by cvs_strsplit().
                    853:  */
                    854: void
                    855: cvs_argv_destroy(struct cvs_argvector *av)
                    856: {
                    857:        xfree(av->str);
                    858:        xfree(av->argv);
                    859:        xfree(av);
1.82      joris     860: }
                    861:
                    862: u_int
                    863: cvs_revision_select(RCSFILE *file, char *range)
                    864: {
                    865:        int i;
                    866:        u_int nrev;
                    867:        char *lstr, *rstr;
                    868:        struct rcs_delta *rdp;
                    869:        struct cvs_argvector *revargv, *revrange;
                    870:        RCSNUM *lnum, *rnum;
                    871:
                    872:        nrev = 0;
                    873:        lnum = rnum = NULL;
                    874:
                    875:        revargv = cvs_strsplit(range, ",");
                    876:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    877:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    878:                if (revrange->argv[0] == NULL)
                    879:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    880:                else if (revrange->argv[1] == NULL)
                    881:                        lstr = rstr = revrange->argv[0];
                    882:                else {
                    883:                        if (revrange->argv[2] != NULL)
                    884:                                fatal("invalid revision range: %s",
                    885:                                    revargv->argv[i]);
                    886:
                    887:                        lstr = revrange->argv[0];
                    888:                        rstr = revrange->argv[1];
                    889:
                    890:                        if (strcmp(lstr, "") == 0)
                    891:                                lstr = NULL;
                    892:                        if (strcmp(rstr, "") == 0)
                    893:                                rstr = NULL;
                    894:                }
                    895:
                    896:                if (lstr == NULL)
                    897:                        lstr = RCS_HEAD_INIT;
                    898:
1.83      joris     899:                lnum = rcs_translate_tag(lstr, file);
1.82      joris     900:
                    901:                if (rstr != NULL) {
1.83      joris     902:                        rnum = rcs_translate_tag(rstr, file);
1.82      joris     903:                } else {
                    904:                        rnum = rcsnum_alloc();
                    905:                        rcsnum_cpy(file->rf_head, rnum, 0);
                    906:                }
                    907:
                    908:                cvs_argv_destroy(revrange);
                    909:
                    910:                TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    911:                        if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
                    912:                            rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
                    913:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    914:                                rdp->rd_flags |= RCS_RD_SELECT;
                    915:                                nrev++;
                    916:                        }
                    917:                }
                    918:        }
                    919:
                    920:        cvs_argv_destroy(revargv);
                    921:
                    922:        if (lnum != NULL)
                    923:                rcsnum_free(lnum);
                    924:        if (rnum != NULL)
                    925:                rcsnum_free(rnum);
                    926:
                    927:        return (nrev);
1.71      xsa       928: }