Annotation of src/usr.bin/cvs/util.c, Revision 1.79
1.79 ! joris 1: /* $OpenBSD: util.c,v 1.78 2006/04/14 02:45:35 deraadt 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.79 ! joris 571: cvs_get_repo(const char *dir, char *dst, size_t len)
1.51 xsa 572: {
1.79 ! joris 573: int l;
1.51 xsa 574: FILE *fp;
1.79 ! joris 575: char *s, buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.51 xsa 576:
1.79 ! joris 577: if (strlcpy(buf, dir, sizeof(buf)) >= sizeof(buf))
! 578: fatal("cvs_get_repo: truncation");
! 579:
! 580: l = snprintf(fpath, sizeof(fpath), "%s/%s", dir, CVS_PATH_REPOSITORY);
! 581: if (l == -1 || l >= (int)sizeof(fpath))
! 582: fatal("cvs_get_repo: overflow");
! 583:
! 584: if ((fp = fopen(fpath, "r")) != NULL) {
! 585: fgets(buf, sizeof(buf), fp);
1.51 xsa 586:
1.79 ! joris 587: if ((s = strrchr(buf, '\n')) != NULL)
! 588: *s = '\0';
1.51 xsa 589:
590: (void)fclose(fp);
591: }
1.79 ! joris 592:
! 593: l = snprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
! 594: if (l == -1 || l >= (int)len)
! 595: fatal("cvs_get_repo: overflow");
1.51 xsa 596: }
597:
598: void
1.79 ! joris 599: cvs_mkadmin(const char *path, const char *root, const char *repo)
1.51 xsa 600: {
601: FILE *fp;
602: size_t len;
1.79 ! joris 603: struct stat st;
! 604: char buf[MAXPATHLEN];
1.51 xsa 605:
1.79 ! joris 606: cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s)", path, root, repo);
1.51 xsa 607:
1.79 ! joris 608: len = cvs_path_cat(path, CVS_PATH_CVSDIR, buf, sizeof(buf));
! 609: if (len >= sizeof(buf))
! 610: fatal("cvs_mkadmin: truncation");
1.51 xsa 611:
1.79 ! joris 612: if (stat(buf, &st) != -1)
! 613: return;
1.51 xsa 614:
1.79 ! joris 615: if (mkdir(buf, 0755) == -1 && errno != EEXIST)
! 616: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
! 617:
! 618: len = cvs_path_cat(path, CVS_PATH_ROOTSPEC, buf, sizeof(buf));
! 619: if (len >= sizeof(buf))
! 620: fatal("cvs_mkadmin: truncation");
! 621:
! 622: if ((fp = fopen(buf, "w")) == NULL)
! 623: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
! 624:
! 625: fprintf(fp, "%s\n", root);
! 626: (void)fclose(fp);
1.51 xsa 627:
1.79 ! joris 628: len = cvs_path_cat(path, CVS_PATH_REPOSITORY, buf, sizeof(buf));
! 629: if (len >= sizeof(buf))
! 630: fatal("cvs_mkadmin: truncation");
1.51 xsa 631:
1.79 ! joris 632: if ((fp = fopen(buf, "w")) == NULL)
! 633: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 634:
1.79 ! joris 635: fprintf(fp, "%s\n", repo);
! 636: (void)fclose(fp);
1.51 xsa 637:
1.79 ! joris 638: len = cvs_path_cat(path, CVS_PATH_ENTRIES, buf, sizeof(buf));
! 639: if (len >= sizeof(buf))
! 640: fatal("cvs_mkadmin: truncation");
1.51 xsa 641:
1.79 ! joris 642: if ((fp = fopen(buf, "w")) == NULL)
! 643: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 644: (void)fclose(fp);
1.54 joris 645: }
646:
1.79 ! joris 647: void
! 648: cvs_mkpath(const char *path)
1.72 niallo 649: {
1.79 ! joris 650: FILE *fp;
! 651: size_t len;
! 652: struct stat st;
! 653: char *sp, *dp, *dir, rpath[MAXPATHLEN], repo[MAXPATHLEN];
! 654:
! 655: dir = xstrdup(path);
! 656:
! 657: STRIP_SLASH(dir);
! 658: cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72 niallo 659:
1.79 ! joris 660: repo[0] = '\0';
! 661: rpath[0] = '\0';
1.72 niallo 662:
1.79 ! joris 663: if (cvs_cmdop == CVS_OP_UPDATE) {
! 664: if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
! 665: fgets(repo, sizeof(repo), fp);
! 666: if (repo[strlen(repo) - 1] == '\n')
! 667: repo[strlen(repo) - 1] = '\0';
! 668: (void)fclose(fp);
! 669: }
1.72 niallo 670: }
671:
1.79 ! joris 672: for (sp = dir; sp != NULL; sp = dp) {
! 673: dp = strchr(sp, '/');
! 674: if (dp != NULL)
! 675: *(dp++) = '\0';
! 676:
! 677: if (repo[0] != '\0') {
! 678: len = strlcat(repo, "/", sizeof(repo));
! 679: if (len >= (int)sizeof(repo))
! 680: fatal("cvs_mkpath: overflow");
! 681: }
! 682:
! 683: len = strlcat(repo, sp, sizeof(repo));
! 684: if (len >= (int)sizeof(repo))
! 685: fatal("cvs_mkpath: overflow");
! 686:
! 687: if (rpath[0] != '\0') {
! 688: len = strlcat(rpath, "/", sizeof(rpath));
! 689: if (len >= (int)sizeof(rpath))
! 690: fatal("cvs_mkpath: overflow");
! 691: }
! 692:
! 693: len = strlcat(rpath, sp, sizeof(rpath));
! 694: if (len >= (int)sizeof(rpath))
! 695: fatal("cvs_mkpath: overflow");
! 696:
! 697: if (stat(repo, &st) != -1)
! 698: continue;
! 699:
! 700: if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
! 701: fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.72 niallo 702:
1.79 ! joris 703: cvs_mkadmin(rpath, current_cvsroot->cr_dir, repo);
! 704: }
1.72 niallo 705:
1.79 ! joris 706: xfree(dir);
1.72 niallo 707: }
1.54 joris 708:
709: /*
710: * Split the contents of a file into a list of lines.
711: */
712: struct cvs_lines *
713: cvs_splitlines(const char *fcont)
714: {
715: char *dcp;
716: struct cvs_lines *lines;
717: struct cvs_line *lp;
718:
1.77 ray 719: lines = xmalloc(sizeof(*lines));
1.54 joris 720: TAILQ_INIT(&(lines->l_lines));
721: lines->l_nblines = 0;
1.59 joris 722: lines->l_data = xstrdup(fcont);
1.54 joris 723:
1.77 ray 724: lp = xmalloc(sizeof(*lp));
1.54 joris 725: lp->l_line = NULL;
726: lp->l_lineno = 0;
727: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
728:
729: for (dcp = lines->l_data; *dcp != '\0';) {
1.77 ray 730: lp = xmalloc(sizeof(*lp));
1.54 joris 731: lp->l_line = dcp;
732: lp->l_lineno = ++(lines->l_nblines);
733: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
734:
735: dcp = strchr(dcp, '\n');
736: if (dcp == NULL)
737: break;
738: *(dcp++) = '\0';
739: }
740:
741: return (lines);
742: }
743:
744: void
745: cvs_freelines(struct cvs_lines *lines)
746: {
747: struct cvs_line *lp;
748:
749: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
750: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.59 joris 751: xfree(lp);
1.54 joris 752: }
753:
1.59 joris 754: xfree(lines->l_data);
755: xfree(lines);
1.54 joris 756: }
757:
758: BUF *
759: cvs_patchfile(const char *data, const char *patch,
760: int (*p)(struct cvs_lines *, struct cvs_lines *))
761: {
762: struct cvs_lines *dlines, *plines;
763: struct cvs_line *lp;
764: size_t len;
765: int lineno;
766: BUF *res;
767:
768: len = strlen(data);
769:
770: if ((dlines = cvs_splitlines(data)) == NULL)
771: return (NULL);
772:
773: if ((plines = cvs_splitlines(patch)) == NULL)
774: return (NULL);
775:
776: if (p(dlines, plines) < 0) {
777: cvs_freelines(dlines);
778: cvs_freelines(plines);
779: return (NULL);
780: }
781:
782: lineno = 0;
1.62 joris 783: res = cvs_buf_alloc(len, BUF_AUTOEXT);
1.54 joris 784: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
785: if (lineno != 0)
786: cvs_buf_fappend(res, "%s\n", lp->l_line);
787: lineno++;
788: }
789:
790: cvs_freelines(dlines);
791: cvs_freelines(plines);
792: return (res);
1.29 jfb 793: }
1.70 niallo 794:
795: /*
796: * cvs_yesno()
797: *
798: * Read from standart input for `y' or `Y' character.
799: * Returns 0 on success, or -1 on failure.
800: */
801: int
802: cvs_yesno(void)
803: {
804: int c, ret;
805:
806: ret = 0;
807:
808: fflush(stderr);
809: fflush(stdout);
810:
811: if ((c = getchar()) != 'y' && c != 'Y')
812: ret = -1;
813: else
814: while (c != EOF && c != '\n')
815: c = getchar();
816:
817: return (ret);
818: }
819:
1.71 xsa 820: /*
821: * cvs_strsplit()
822: *
823: * Split a string <str> of <sep>-separated values and allocate
824: * an argument vector for the values found.
825: */
1.73 pat 826: struct cvs_argvector *
1.71 xsa 827: cvs_strsplit(char *str, const char *sep)
828: {
1.73 pat 829: struct cvs_argvector *av;
1.76 ray 830: size_t i = 0;
1.73 pat 831: char **nargv;
1.71 xsa 832: char *cp, *p;
833:
834: cp = xstrdup(str);
1.77 ray 835: av = xmalloc(sizeof(*av));
1.73 pat 836: av->str = cp;
1.77 ray 837: av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
1.71 xsa 838:
839: while ((p = strsep(&cp, sep)) != NULL) {
1.73 pat 840: av->argv[i++] = p;
1.77 ray 841: nargv = xrealloc(av->argv,
842: i + 1, sizeof(*(av->argv)));
1.73 pat 843: av->argv = nargv;
1.71 xsa 844: }
1.73 pat 845: av->argv[i] = NULL;
846:
847: return (av);
848: }
1.71 xsa 849:
1.73 pat 850: /*
851: * cvs_argv_destroy()
852: *
853: * Free an argument vector previously allocated by cvs_strsplit().
854: */
855: void
856: cvs_argv_destroy(struct cvs_argvector *av)
857: {
858: xfree(av->str);
859: xfree(av->argv);
860: xfree(av);
1.71 xsa 861: }