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