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

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