Annotation of src/usr.bin/cvs/rcsnum.c, Revision 1.59
1.59 ! otto 1: /* $OpenBSD: rcsnum.c,v 1.58 2016/10/13 20:51:25 fcambus Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.4 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.4 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.4 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.4 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.4 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
1.42 otto 27: #include <ctype.h>
1.57 nicm 28: #include <stdlib.h>
1.42 otto 29: #include <string.h>
1.1 jfb 30:
1.15 joris 31: #include "cvs.h"
1.1 jfb 32:
1.56 deraadt 33: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
34:
1.33 xsa 35: static void rcsnum_setsize(RCSNUM *, u_int);
1.23 xsa 36: static char *rcsnum_itoa(u_int16_t, char *, size_t);
1.11 jfb 37:
1.1 jfb 38: /*
39: * rcsnum_alloc()
40: *
1.30 ray 41: * Allocate an RCS number structure and return a pointer to it.
1.1 jfb 42: */
1.14 xsa 43: RCSNUM *
1.1 jfb 44: rcsnum_alloc(void)
45: {
46: RCSNUM *rnp;
47:
1.54 tedu 48: rnp = xcalloc(1, sizeof(*rnp));
1.1 jfb 49: rnp->rn_len = 0;
50:
51: return (rnp);
1.50 tobias 52: }
53:
54: /*
55: * rcsnum_addmagic()
56: *
57: * Adds a magic branch number to an RCS number.
58: * Returns 0 on success, or -1 on failure.
59: */
60: int
61: rcsnum_addmagic(RCSNUM *rn)
62: {
63: if (!rn->rn_len || rn->rn_len > RCSNUM_MAXLEN - 1)
64: return -1;
65: rcsnum_setsize(rn, rn->rn_len + 1);
66: rn->rn_id[rn->rn_len - 1] = rn->rn_id[rn->rn_len - 2];
67: rn->rn_id[rn->rn_len - 2] = 0;
68:
69: return 0;
1.1 jfb 70: }
71:
72: /*
1.7 jfb 73: * rcsnum_parse()
74: *
75: * Parse a string specifying an RCS number and return the corresponding RCSNUM.
76: */
1.14 xsa 77: RCSNUM *
1.7 jfb 78: rcsnum_parse(const char *str)
79: {
1.59 ! otto 80: const char *ep;
1.7 jfb 81: RCSNUM *num;
82:
1.19 joris 83: num = rcsnum_alloc();
1.34 deraadt 84: if (rcsnum_aton(str, &ep, num) < 0 || *ep != '\0') {
1.58 fcambus 85: free(num);
1.9 jfb 86: num = NULL;
1.7 jfb 87: }
88:
89: return (num);
1.1 jfb 90: }
91:
92: /*
93: * rcsnum_tostr()
1.10 jfb 94: *
95: * Format the RCS number <nump> into a human-readable dot-separated
96: * representation and store the resulting string in <buf>, which is of size
97: * <blen>.
1.25 ray 98: * Returns a pointer to the start of <buf>. On failure <buf> is set to
99: * an empty string.
1.1 jfb 100: */
1.13 xsa 101: char *
1.1 jfb 102: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
103: {
104: u_int i;
105: char tmp[8];
106:
1.34 deraadt 107: if (nump == NULL || nump->rn_len == 0) {
1.1 jfb 108: buf[0] = '\0';
109: return (buf);
110: }
111:
1.40 xsa 112: if (strlcpy(buf, rcsnum_itoa(nump->rn_id[0], buf, blen), blen) >= blen)
113: fatal("rcsnum_tostr: truncation");
1.1 jfb 114: for (i = 1; i < nump->rn_len; i++) {
1.40 xsa 115: const char *str;
116:
117: str = rcsnum_itoa(nump->rn_id[i], tmp, sizeof(tmp));
118: if (strlcat(buf, ".", blen) >= blen ||
119: strlcat(buf, str, blen) >= blen)
120: fatal("rcsnum_tostr: truncation");
1.1 jfb 121: }
122: return (buf);
1.20 niallo 123: }
124:
125: static char *
126: rcsnum_itoa(u_int16_t num, char *buf, size_t len)
127: {
1.21 reyk 128: u_int16_t i;
129: char *p;
1.20 niallo 130:
1.26 niallo 131: if (num == 0)
132: return "0";
1.35 deraadt 133:
1.21 reyk 134: p = buf + len - 1;
135: i = num;
1.20 niallo 136: bzero(buf, len);
1.21 reyk 137: while (i) {
138: *--p = '0' + (i % 10);
139: i /= 10;
140: }
141: return (p);
1.1 jfb 142: }
143:
144: /*
145: * rcsnum_cpy()
146: *
147: * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
1.30 ray 148: * numbers deep. If <depth> is 0, there is no depth limit.
1.1 jfb 149: */
1.30 ray 150: void
1.1 jfb 151: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
152: {
153: u_int len;
154:
155: len = nsrc->rn_len;
1.34 deraadt 156: if (depth != 0 && len > depth)
1.1 jfb 157: len = depth;
158:
1.41 ray 159: rcsnum_setsize(ndst, len);
1.54 tedu 160: memcpy(ndst->rn_id, nsrc->rn_id, len * sizeof(*(nsrc->rn_id)));
1.1 jfb 161: }
162:
163: /*
164: * rcsnum_cmp()
165: *
166: * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
167: * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
168: * The <depth> argument specifies how many numbers deep should be checked for
1.53 joris 169: * the result. A value of 0 means that the depth will be the maximum of the
170: * two numbers, so that a longer number is considered greater than a shorter
171: * number if they are equal up to the minimum length.
1.1 jfb 172: */
173: int
1.38 joris 174: rcsnum_cmp(RCSNUM *n1, RCSNUM *n2, u_int depth)
1.1 jfb 175: {
176: int res;
177: u_int i;
178: size_t slen;
179:
1.38 joris 180: if (!rcsnum_differ(n1, n2))
181: return (0);
182:
1.56 deraadt 183: slen = MINIMUM(n1->rn_len, n2->rn_len);
1.34 deraadt 184: if (depth != 0 && slen > depth)
1.1 jfb 185: slen = depth;
1.4 tedu 186:
1.1 jfb 187: for (i = 0; i < slen; i++) {
188: res = n1->rn_id[i] - n2->rn_id[i];
189: if (res < 0)
190: return (1);
191: else if (res > 0)
192: return (-1);
193: }
194:
1.53 joris 195: /* If an explicit depth was specified, and we've
196: * already checked up to depth, consider the
197: * revision numbers equal. */
198: if (depth != 0 && slen == depth)
199: return (0);
200: else if (n1->rn_len > n2->rn_len)
1.1 jfb 201: return (-1);
202: else if (n2->rn_len > n1->rn_len)
203: return (1);
204:
205: return (0);
206: }
207:
208: /*
209: * rcsnum_aton()
210: *
211: * Translate the string <str> containing a sequence of digits and periods into
212: * its binary representation, which is stored in <nump>. The address of the
213: * first byte not part of the number is stored in <ep> on return, if it is not
214: * NULL.
215: * Returns 0 on success, or -1 on failure.
216: */
217: int
1.59 ! otto 218: rcsnum_aton(const char *str, const char **ep, RCSNUM *nump)
1.1 jfb 219: {
1.6 jfb 220: u_int32_t val;
1.1 jfb 221: const char *sp;
1.15 joris 222: char *s;
1.1 jfb 223:
224: nump->rn_len = 0;
1.6 jfb 225: nump->rn_id[0] = 0;
1.1 jfb 226:
1.3 jfb 227: for (sp = str;; sp++) {
1.55 okan 228: if (!isdigit((unsigned char)*sp) && (*sp != '.'))
1.1 jfb 229: break;
230:
231: if (*sp == '.') {
1.52 joris 232: if (nump->rn_len >= RCSNUM_MAXLEN - 1)
1.6 jfb 233: goto rcsnum_aton_failed;
234:
1.1 jfb 235: nump->rn_len++;
1.2 vincent 236: nump->rn_id[nump->rn_len] = 0;
1.1 jfb 237: continue;
238: }
239:
1.43 ray 240: val = (nump->rn_id[nump->rn_len] * 10) + (*sp - '0');
1.24 niallo 241: if (val > RCSNUM_MAXNUM)
242: fatal("RCSNUM overflow!");
1.6 jfb 243:
244: nump->rn_id[nump->rn_len] = val;
1.1 jfb 245: }
246:
247: if (ep != NULL)
1.59 ! otto 248: *ep = sp;
1.15 joris 249:
250: /*
251: * Handle "magic" RCS branch numbers.
252: *
253: * What are they?
254: *
255: * Magic branch numbers have an extra .0. at the second farmost
256: * rightside of the branch number, so instead of having an odd
257: * number of dot-separated decimals, it will have an even number.
258: *
1.44 krw 259: * Now, according to all the documentation I've found on the net
260: * about this, cvs does this for "efficiency reasons", I'd like
1.15 joris 261: * to hear one.
262: *
263: * We just make sure we remove the .0. from in the branch number.
264: *
265: * XXX - for compatibility reasons with GNU cvs we _need_
266: * to skip this part for the 'log' command, apparently it does
267: * show the magic branches for an unknown and probably
268: * completely insane and not understandable reason in that output.
269: *
270: */
1.51 joris 271: if (nump->rn_len > 2 && nump->rn_id[nump->rn_len - 1] == 0) {
1.15 joris 272: /*
273: * Look for ".0.x" at the end of the branch number.
274: */
275: if ((s = strrchr(str, '.')) != NULL) {
1.27 deraadt 276: s--;
1.15 joris 277: while (*s != '.')
1.27 deraadt 278: s--;
1.15 joris 279:
280: /*
281: * If we have a "magic" branch, adjust it
282: * so the .0. is removed.
283: */
284: if (!strncmp(s, RCS_MAGIC_BRANCH,
1.46 tobias 285: sizeof(RCS_MAGIC_BRANCH) - 1)) {
1.15 joris 286: nump->rn_id[nump->rn_len - 1] =
287: nump->rn_id[nump->rn_len];
288: nump->rn_len--;
289: }
290: }
291: }
1.1 jfb 292:
1.26 niallo 293: /* We can't have a single-digit rcs number. */
294: if (nump->rn_len == 0) {
1.48 tobias 295: nump->rn_len++;
296: nump->rn_id[nump->rn_len] = 0;
1.26 niallo 297: }
1.31 joris 298:
1.1 jfb 299: nump->rn_len++;
300: return (nump->rn_len);
1.6 jfb 301:
302: rcsnum_aton_failed:
303: nump->rn_len = 0;
304: return (-1);
1.11 jfb 305: }
306:
307: /*
308: * rcsnum_inc()
309: *
310: * Increment the revision number specified in <num>.
311: * Returns a pointer to the <num> on success, or NULL on failure.
312: */
1.14 xsa 313: RCSNUM *
1.11 jfb 314: rcsnum_inc(RCSNUM *num)
315: {
316: if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM)
317: return (NULL);
318: num->rn_id[num->rn_len - 1]++;
1.17 joris 319: return (num);
320: }
321:
322: /*
323: * rcsnum_dec()
324: *
1.26 niallo 325: * Decreases the revision number specified in <num>, if doing so will not
326: * result in an ending value below 1. E.g. 4.2 will go to 4.1 but 4.1 will
327: * be returned as 4.1.
1.17 joris 328: */
329: RCSNUM *
330: rcsnum_dec(RCSNUM *num)
331: {
1.28 ray 332: /* XXX - Is it an error for the number to be 0? */
333: if (num->rn_id[num->rn_len - 1] <= 1)
1.26 niallo 334: return (num);
1.17 joris 335: num->rn_id[num->rn_len - 1]--;
1.11 jfb 336: return (num);
337: }
338:
339: /*
340: * rcsnum_revtobr()
341: *
342: * Retrieve the branch number associated with the revision number <num>.
343: * If <num> is a branch revision, the returned value will be the same
344: * number as the argument.
345: */
1.14 xsa 346: RCSNUM *
1.11 jfb 347: rcsnum_revtobr(const RCSNUM *num)
348: {
349: RCSNUM *brnum;
350:
351: if (num->rn_len < 2)
352: return (NULL);
353:
1.19 joris 354: brnum = rcsnum_alloc();
1.11 jfb 355: rcsnum_cpy(num, brnum, 0);
356:
357: if (!RCSNUM_ISBRANCH(brnum))
358: brnum->rn_len--;
359:
360: return (brnum);
361: }
362:
363: /*
364: * rcsnum_brtorev()
365: *
366: * Retrieve the initial revision number associated with the branch number <num>.
367: * If <num> is a revision number, an error will be returned.
368: */
1.14 xsa 369: RCSNUM *
1.11 jfb 370: rcsnum_brtorev(const RCSNUM *brnum)
371: {
372: RCSNUM *num;
373:
374: if (!RCSNUM_ISBRANCH(brnum)) {
375: return (NULL);
376: }
377:
1.19 joris 378: num = rcsnum_alloc();
1.33 xsa 379: rcsnum_setsize(num, brnum->rn_len + 1);
1.11 jfb 380: rcsnum_cpy(brnum, num, brnum->rn_len);
381: num->rn_id[num->rn_len++] = 1;
382:
383: return (num);
1.49 tobias 384: }
385:
386: RCSNUM *
387: rcsnum_new_branch(RCSNUM *rev)
388: {
389: RCSNUM *branch;
390:
391: if (rev->rn_len > RCSNUM_MAXLEN - 1)
392: return NULL;
393:
394: branch = rcsnum_alloc();
395: rcsnum_cpy(rev, branch, 0);
396: rcsnum_setsize(branch, rev->rn_len + 1);
397: branch->rn_id[branch->rn_len - 1] = 2;
398:
399: return branch;
1.47 joris 400: }
401:
402: RCSNUM *
403: rcsnum_branch_root(RCSNUM *brev)
404: {
405: RCSNUM *root;
406:
407: if (!RCSNUM_ISBRANCHREV(brev))
408: fatal("rcsnum_branch_root: no revision on branch specified");
409:
410: root = rcsnum_alloc();
411: rcsnum_cpy(brev, root, 0);
412: root->rn_len -= 2;
413: return (root);
1.11 jfb 414: }
415:
1.33 xsa 416: static void
1.11 jfb 417: rcsnum_setsize(RCSNUM *num, u_int len)
418: {
419: num->rn_len = len;
1.38 joris 420: }
421:
422: int
423: rcsnum_differ(RCSNUM *r1, RCSNUM *r2)
424: {
425: int i, len;
426:
427: if (r1->rn_len != r2->rn_len)
428: return (1);
429:
1.56 deraadt 430: len = MINIMUM(r1->rn_len, r2->rn_len);
1.38 joris 431: for (i = 0; i < len; i++) {
432: if (r1->rn_id[i] != r2->rn_id[i])
433: return (1);
434: }
435:
436: return (0);
1.1 jfb 437: }