[BACK]Return to rcsnum.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/rcsnum.c, Revision 1.12

1.12    ! xsa         1: /*     $OpenBSD: rcsnum.c,v 1.11 2005/05/26 01:45:54 jfb 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:
                     27: #include <sys/param.h>
                     28:
                     29: #include <ctype.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33:
1.12    ! xsa        34: #include "log.h"
1.1       jfb        35: #include "rcs.h"
                     36:
                     37:
1.11      jfb        38: static int rcsnum_setsize  (RCSNUM *, u_int);
                     39:
1.1       jfb        40:
                     41: /*
                     42:  * rcsnum_alloc()
                     43:  *
1.11      jfb        44:  * Allocate an RCS number structure and return a pointer to it on success,
                     45:  * or NULL on failure.
1.1       jfb        46:  */
                     47: RCSNUM*
                     48: rcsnum_alloc(void)
                     49: {
                     50:        RCSNUM *rnp;
                     51:
                     52:        rnp = (RCSNUM *)malloc(sizeof(*rnp));
                     53:        if (rnp == NULL) {
1.11      jfb        54:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb        55:                return (NULL);
                     56:        }
                     57:        rnp->rn_len = 0;
                     58:        rnp->rn_id = NULL;
                     59:
                     60:        return (rnp);
                     61: }
                     62:
                     63: /*
1.7       jfb        64:  * rcsnum_parse()
                     65:  *
                     66:  * Parse a string specifying an RCS number and return the corresponding RCSNUM.
                     67:  */
                     68: RCSNUM*
                     69: rcsnum_parse(const char *str)
                     70: {
                     71:        char *ep;
                     72:        RCSNUM *num;
                     73:
                     74:        if ((num = rcsnum_alloc()) == NULL)
                     75:                return (NULL);
                     76:
1.8       jfb        77:        if ((rcsnum_aton(str, &ep, num) < 0) || (*ep != '\0')) {
1.7       jfb        78:                rcsnum_free(num);
1.9       jfb        79:                num = NULL;
                     80:                if (*ep != '\0')
                     81:                        rcs_errno = RCS_ERR_BADNUM;
1.7       jfb        82:        }
                     83:
                     84:        return (num);
                     85: }
                     86:
                     87: /*
1.1       jfb        88:  * rcsnum_free()
                     89:  *
                     90:  * Free an RCSNUM structure previously allocated with rcsnum_alloc().
                     91:  */
                     92: void
                     93: rcsnum_free(RCSNUM *rn)
                     94: {
                     95:        if (rn->rn_id != NULL)
                     96:                free(rn->rn_id);
                     97:        free(rn);
                     98: }
                     99:
                    100: /*
                    101:  * rcsnum_tostr()
1.10      jfb       102:  *
                    103:  * Format the RCS number <nump> into a human-readable dot-separated
                    104:  * representation and store the resulting string in <buf>, which is of size
                    105:  * <blen>.
1.1       jfb       106:  * Returns a pointer to the start of <buf> on success, or NULL on failure.
                    107:  */
                    108: char*
                    109: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
                    110: {
                    111:        u_int i;
                    112:        char tmp[8];
                    113:
1.10      jfb       114:        if ((nump == NULL) || (nump->rn_len == 0)) {
1.1       jfb       115:                buf[0] = '\0';
                    116:                return (buf);
                    117:        }
                    118:
1.4       tedu      119:        snprintf(buf, blen, "%u", nump->rn_id[0]);
1.1       jfb       120:        for (i = 1; i < nump->rn_len; i++) {
                    121:                snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]);
                    122:                strlcat(buf, tmp, blen);
                    123:        }
                    124:
                    125:        return (buf);
                    126: }
                    127:
                    128: /*
                    129:  * rcsnum_cpy()
                    130:  *
                    131:  * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
                    132:  * numbers deep.
                    133:  * Returns 0 on success, or -1 on failure.
                    134:  */
                    135: int
                    136: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
                    137: {
                    138:        u_int len;
                    139:        size_t sz;
                    140:        void *tmp;
                    141:
                    142:        len = nsrc->rn_len;
                    143:        if ((depth != 0) && (len > depth))
                    144:                len = depth;
                    145:        sz = len * sizeof(u_int16_t);
                    146:
                    147:        tmp = realloc(ndst->rn_id, sz);
                    148:        if (tmp == NULL) {
1.11      jfb       149:                rcs_errno = RCS_ERR_ERRNO;
1.1       jfb       150:                return (-1);
                    151:        }
                    152:
                    153:        ndst->rn_id = (u_int16_t *)tmp;
                    154:        ndst->rn_len = len;
                    155:        memcpy(ndst->rn_id, nsrc->rn_id, sz);
                    156:        return (0);
                    157: }
                    158:
                    159: /*
                    160:  * rcsnum_cmp()
                    161:  *
                    162:  * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
                    163:  * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
                    164:  * The <depth> argument specifies how many numbers deep should be checked for
                    165:  * the result.  A value of 0 means that the depth will be the minimum of the
                    166:  * two numbers.
                    167:  */
                    168: int
                    169: rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
                    170: {
                    171:        int res;
                    172:        u_int i;
                    173:        size_t slen;
                    174:
                    175:        slen = MIN(n1->rn_len, n2->rn_len);
                    176:        if ((depth != 0) && (slen > depth))
                    177:                slen = depth;
1.4       tedu      178:
1.1       jfb       179:        for (i = 0; i < slen; i++) {
                    180:                res = n1->rn_id[i] - n2->rn_id[i];
                    181:                if (res < 0)
                    182:                        return (1);
                    183:                else if (res > 0)
                    184:                        return (-1);
                    185:        }
                    186:
                    187:        if (n1->rn_len > n2->rn_len)
                    188:                return (-1);
                    189:        else if (n2->rn_len > n1->rn_len)
                    190:                return (1);
                    191:
                    192:        return (0);
                    193: }
                    194:
                    195: /*
                    196:  * rcsnum_aton()
                    197:  *
                    198:  * Translate the string <str> containing a sequence of digits and periods into
                    199:  * its binary representation, which is stored in <nump>.  The address of the
                    200:  * first byte not part of the number is stored in <ep> on return, if it is not
                    201:  * NULL.
                    202:  * Returns 0 on success, or -1 on failure.
                    203:  */
                    204: int
                    205: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
                    206: {
1.6       jfb       207:        u_int32_t val;
1.1       jfb       208:        const char *sp;
                    209:        void *tmp;
                    210:
1.2       vincent   211:        if (nump->rn_id == NULL) {
                    212:                nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
1.6       jfb       213:                if (nump->rn_id == NULL) {
1.11      jfb       214:                        rcs_errno = RCS_ERR_ERRNO;
1.2       vincent   215:                        return (-1);
1.6       jfb       216:                }
1.2       vincent   217:        }
1.1       jfb       218:
                    219:        nump->rn_len = 0;
1.6       jfb       220:        nump->rn_id[0] = 0;
1.1       jfb       221:
1.3       jfb       222:        for (sp = str;; sp++) {
                    223:                if (!isdigit(*sp) && (*sp != '.'))
1.1       jfb       224:                        break;
                    225:
                    226:                if (*sp == '.') {
1.6       jfb       227:                        if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
1.11      jfb       228:                                rcs_errno = RCS_ERR_BADNUM;
1.6       jfb       229:                                goto rcsnum_aton_failed;
                    230:                        }
                    231:
1.1       jfb       232:                        nump->rn_len++;
                    233:                        tmp = realloc(nump->rn_id,
                    234:                            (nump->rn_len + 1) * sizeof(u_int16_t));
1.9       jfb       235:                        if (tmp == NULL)
1.6       jfb       236:                                goto rcsnum_aton_failed;
1.1       jfb       237:                        nump->rn_id = (u_int16_t *)tmp;
1.2       vincent   238:                        nump->rn_id[nump->rn_len] = 0;
1.1       jfb       239:                        continue;
                    240:                }
                    241:
1.6       jfb       242:                val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30);
                    243:                if (val > RCSNUM_MAXNUM) {
                    244:                        cvs_log(LP_ERR, "RCSNUM overflow");
                    245:                        goto rcsnum_aton_failed;
                    246:                }
                    247:
                    248:                nump->rn_id[nump->rn_len] = val;
1.1       jfb       249:        }
                    250:
                    251:        if (ep != NULL)
1.5       jfb       252:                *(const char **)ep = sp;
1.1       jfb       253:
                    254:        nump->rn_len++;
                    255:        return (nump->rn_len);
1.6       jfb       256:
                    257: rcsnum_aton_failed:
                    258:        nump->rn_len = 0;
                    259:        free(nump->rn_id);
                    260:        nump->rn_id = NULL;
                    261:        return (-1);
1.11      jfb       262: }
                    263:
                    264: /*
                    265:  * rcsnum_inc()
                    266:  *
                    267:  * Increment the revision number specified in <num>.
                    268:  * Returns a pointer to the <num> on success, or NULL on failure.
                    269:  */
                    270: RCSNUM*
                    271: rcsnum_inc(RCSNUM *num)
                    272: {
                    273:        if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM)
                    274:                return (NULL);
                    275:        num->rn_id[num->rn_len - 1]++;
                    276:        return (num);
                    277: }
                    278:
                    279: /*
                    280:  * rcsnum_revtobr()
                    281:  *
                    282:  * Retrieve the branch number associated with the revision number <num>.
                    283:  * If <num> is a branch revision, the returned value will be the same
                    284:  * number as the argument.
                    285:  */
                    286: RCSNUM*
                    287: rcsnum_revtobr(const RCSNUM *num)
                    288: {
                    289:        RCSNUM *brnum;
                    290:
                    291:        if (num->rn_len < 2)
                    292:                return (NULL);
                    293:
                    294:        if ((brnum = rcsnum_alloc()) == NULL)
                    295:                return (NULL);
                    296:
                    297:        rcsnum_cpy(num, brnum, 0);
                    298:
                    299:        if (!RCSNUM_ISBRANCH(brnum))
                    300:                brnum->rn_len--;
                    301:
                    302:        return (brnum);
                    303: }
                    304:
                    305: /*
                    306:  * rcsnum_brtorev()
                    307:  *
                    308:  * Retrieve the initial revision number associated with the branch number <num>.
                    309:  * If <num> is a revision number, an error will be returned.
                    310:  */
                    311: RCSNUM*
                    312: rcsnum_brtorev(const RCSNUM *brnum)
                    313: {
                    314:        RCSNUM *num;
                    315:
                    316:        if (!RCSNUM_ISBRANCH(brnum)) {
                    317:                return (NULL);
                    318:        }
                    319:
                    320:        if ((num = rcsnum_alloc()) == NULL)
                    321:                return (NULL);
                    322:
                    323:        if (rcsnum_setsize(num, brnum->rn_len + 1) < 0) {
                    324:                rcsnum_free(num);
                    325:                return (NULL);
                    326:        }
                    327:
                    328:        rcsnum_cpy(brnum, num, brnum->rn_len);
                    329:        num->rn_id[num->rn_len++] = 1;
                    330:
                    331:        return (num);
                    332: }
                    333:
                    334: static int
                    335: rcsnum_setsize(RCSNUM *num, u_int len)
                    336: {
                    337:        void *tmp;
                    338:
                    339:        tmp = realloc(num->rn_id, len * sizeof(u_int16_t));
                    340:        if (tmp == NULL) {
                    341:                rcs_errno = RCS_ERR_ERRNO;
                    342:                return (-1);
                    343:        }
                    344:
                    345:        num->rn_id = (u_int16_t *)tmp;
                    346:        num->rn_len = len;
                    347:        return (0);
1.1       jfb       348: }