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

1.8     ! jfb         1: /*     $OpenBSD: rcsnum.c,v 1.7 2005/02/25 20:05:42 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:
                     34: #include "rcs.h"
                     35: #include "log.h"
                     36:
                     37:
                     38:
                     39: /*
                     40:  * rcsnum_alloc()
                     41:  *
                     42:  * Allocate an RCS number structure.
                     43:  */
                     44: RCSNUM*
                     45: rcsnum_alloc(void)
                     46: {
                     47:        RCSNUM *rnp;
                     48:
                     49:        rnp = (RCSNUM *)malloc(sizeof(*rnp));
                     50:        if (rnp == NULL) {
                     51:                cvs_log(LP_ERR, "failed to allocate RCS number");
                     52:                return (NULL);
                     53:        }
                     54:        rnp->rn_len = 0;
                     55:        rnp->rn_id = NULL;
                     56:
                     57:        return (rnp);
                     58: }
                     59:
                     60:
                     61: /*
1.7       jfb        62:  * rcsnum_parse()
                     63:  *
                     64:  * Parse a string specifying an RCS number and return the corresponding RCSNUM.
                     65:  */
                     66: RCSNUM*
                     67: rcsnum_parse(const char *str)
                     68: {
                     69:        char *ep;
                     70:        RCSNUM *num;
                     71:
                     72:        if ((num = rcsnum_alloc()) == NULL)
                     73:                return (NULL);
                     74:
1.8     ! jfb        75:        if ((rcsnum_aton(str, &ep, num) < 0) || (*ep != '\0')) {
1.7       jfb        76:                rcsnum_free(num);
                     77:                return (NULL);
                     78:        }
                     79:
                     80:        return (num);
                     81: }
                     82:
                     83:
                     84: /*
1.1       jfb        85:  * rcsnum_free()
                     86:  *
                     87:  * Free an RCSNUM structure previously allocated with rcsnum_alloc().
                     88:  */
                     89: void
                     90: rcsnum_free(RCSNUM *rn)
                     91: {
                     92:        if (rn->rn_id != NULL)
                     93:                free(rn->rn_id);
                     94:        free(rn);
                     95: }
                     96:
                     97:
                     98: /*
                     99:  * rcsnum_tostr()
                    100:  * Returns a pointer to the start of <buf> on success, or NULL on failure.
                    101:  */
                    102: char*
                    103: rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
                    104: {
                    105:        u_int i;
                    106:        char tmp[8];
                    107:
                    108:        if (nump->rn_len == 0) {
                    109:                buf[0] = '\0';
                    110:                return (buf);
                    111:        }
                    112:
1.4       tedu      113:        snprintf(buf, blen, "%u", nump->rn_id[0]);
1.1       jfb       114:        for (i = 1; i < nump->rn_len; i++) {
                    115:                snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]);
                    116:                strlcat(buf, tmp, blen);
                    117:        }
                    118:
                    119:        return (buf);
                    120: }
                    121:
                    122:
                    123: /*
                    124:  * rcsnum_cpy()
                    125:  *
                    126:  * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
                    127:  * numbers deep.
                    128:  * Returns 0 on success, or -1 on failure.
                    129:  */
                    130: int
                    131: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
                    132: {
                    133:        u_int len;
                    134:        size_t sz;
                    135:        void *tmp;
                    136:
                    137:        len = nsrc->rn_len;
                    138:        if ((depth != 0) && (len > depth))
                    139:                len = depth;
                    140:        sz = len * sizeof(u_int16_t);
                    141:
                    142:        tmp = realloc(ndst->rn_id, sz);
                    143:        if (tmp == NULL) {
                    144:                cvs_log(LP_ERR, "failed to reallocate RCSNUM");
                    145:                return (-1);
                    146:        }
                    147:
                    148:        ndst->rn_id = (u_int16_t *)tmp;
                    149:        ndst->rn_len = len;
                    150:        memcpy(ndst->rn_id, nsrc->rn_id, sz);
                    151:        return (0);
                    152: }
                    153:
                    154:
                    155: /*
                    156:  * rcsnum_cmp()
                    157:  *
                    158:  * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
                    159:  * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
                    160:  * The <depth> argument specifies how many numbers deep should be checked for
                    161:  * the result.  A value of 0 means that the depth will be the minimum of the
                    162:  * two numbers.
                    163:  */
                    164: int
                    165: rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
                    166: {
                    167:        int res;
                    168:        u_int i;
                    169:        size_t slen;
                    170:
                    171:        slen = MIN(n1->rn_len, n2->rn_len);
                    172:        if ((depth != 0) && (slen > depth))
                    173:                slen = depth;
1.4       tedu      174:
1.1       jfb       175:        for (i = 0; i < slen; i++) {
                    176:                res = n1->rn_id[i] - n2->rn_id[i];
                    177:                if (res < 0)
                    178:                        return (1);
                    179:                else if (res > 0)
                    180:                        return (-1);
                    181:        }
                    182:
                    183:        if (n1->rn_len > n2->rn_len)
                    184:                return (-1);
                    185:        else if (n2->rn_len > n1->rn_len)
                    186:                return (1);
                    187:
                    188:        return (0);
                    189: }
                    190:
                    191:
                    192: /*
                    193:  * rcsnum_aton()
                    194:  *
                    195:  * Translate the string <str> containing a sequence of digits and periods into
                    196:  * its binary representation, which is stored in <nump>.  The address of the
                    197:  * first byte not part of the number is stored in <ep> on return, if it is not
                    198:  * NULL.
                    199:  * Returns 0 on success, or -1 on failure.
                    200:  */
                    201: int
                    202: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
                    203: {
1.6       jfb       204:        u_int32_t val;
1.1       jfb       205:        const char *sp;
                    206:        void *tmp;
                    207:
1.2       vincent   208:        if (nump->rn_id == NULL) {
                    209:                nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
1.6       jfb       210:                if (nump->rn_id == NULL) {
                    211:                        cvs_log(LP_ERRNO, "failed to allocate RCSNUM");
1.2       vincent   212:                        return (-1);
1.6       jfb       213:                }
1.2       vincent   214:        }
1.1       jfb       215:
                    216:        nump->rn_len = 0;
1.6       jfb       217:        nump->rn_id[0] = 0;
1.1       jfb       218:
1.3       jfb       219:        for (sp = str;; sp++) {
                    220:                if (!isdigit(*sp) && (*sp != '.'))
1.1       jfb       221:                        break;
                    222:
                    223:                if (*sp == '.') {
1.6       jfb       224:                        if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
                    225:                                cvs_log(LP_ERR,
                    226:                                    "RCSNUM exceeds maximum length");
                    227:                                goto rcsnum_aton_failed;
                    228:                        }
                    229:
1.1       jfb       230:                        nump->rn_len++;
                    231:                        tmp = realloc(nump->rn_id,
                    232:                            (nump->rn_len + 1) * sizeof(u_int16_t));
                    233:                        if (tmp == NULL) {
1.6       jfb       234:                                goto rcsnum_aton_failed;
1.1       jfb       235:                        }
                    236:                        nump->rn_id = (u_int16_t *)tmp;
1.2       vincent   237:                        nump->rn_id[nump->rn_len] = 0;
1.1       jfb       238:                        continue;
                    239:                }
                    240:
1.6       jfb       241:                val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30);
                    242:                if (val > RCSNUM_MAXNUM) {
                    243:                        cvs_log(LP_ERR, "RCSNUM overflow");
                    244:                        goto rcsnum_aton_failed;
                    245:                }
                    246:
                    247:                nump->rn_id[nump->rn_len] = val;
1.1       jfb       248:        }
                    249:
                    250:        if (ep != NULL)
1.5       jfb       251:                *(const char **)ep = sp;
1.1       jfb       252:
                    253:        nump->rn_len++;
                    254:        return (nump->rn_len);
1.6       jfb       255:
                    256: rcsnum_aton_failed:
                    257:        nump->rn_len = 0;
                    258:        free(nump->rn_id);
                    259:        nump->rn_id = NULL;
                    260:        return (-1);
1.1       jfb       261: }