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

1.10    ! jfb         1: /*     $OpenBSD: rcsnum.c,v 1.9 2005/03/05 05:58:39 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);
1.9       jfb        77:                num = NULL;
                     78:                if (*ep != '\0')
                     79:                        rcs_errno = RCS_ERR_BADNUM;
1.7       jfb        80:        }
                     81:
                     82:        return (num);
                     83: }
                     84:
                     85:
                     86: /*
1.1       jfb        87:  * rcsnum_free()
                     88:  *
                     89:  * Free an RCSNUM structure previously allocated with rcsnum_alloc().
                     90:  */
                     91: void
                     92: rcsnum_free(RCSNUM *rn)
                     93: {
                     94:        if (rn->rn_id != NULL)
                     95:                free(rn->rn_id);
                     96:        free(rn);
                     97: }
                     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: /*
                    130:  * rcsnum_cpy()
                    131:  *
                    132:  * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
                    133:  * numbers deep.
                    134:  * Returns 0 on success, or -1 on failure.
                    135:  */
                    136: int
                    137: rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
                    138: {
                    139:        u_int len;
                    140:        size_t sz;
                    141:        void *tmp;
                    142:
                    143:        len = nsrc->rn_len;
                    144:        if ((depth != 0) && (len > depth))
                    145:                len = depth;
                    146:        sz = len * sizeof(u_int16_t);
                    147:
                    148:        tmp = realloc(ndst->rn_id, sz);
                    149:        if (tmp == NULL) {
                    150:                cvs_log(LP_ERR, "failed to reallocate RCSNUM");
                    151:                return (-1);
                    152:        }
                    153:
                    154:        ndst->rn_id = (u_int16_t *)tmp;
                    155:        ndst->rn_len = len;
                    156:        memcpy(ndst->rn_id, nsrc->rn_id, sz);
                    157:        return (0);
                    158: }
                    159:
                    160:
                    161: /*
                    162:  * rcsnum_cmp()
                    163:  *
                    164:  * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
                    165:  * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
                    166:  * The <depth> argument specifies how many numbers deep should be checked for
                    167:  * the result.  A value of 0 means that the depth will be the minimum of the
                    168:  * two numbers.
                    169:  */
                    170: int
                    171: rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
                    172: {
                    173:        int res;
                    174:        u_int i;
                    175:        size_t slen;
                    176:
                    177:        slen = MIN(n1->rn_len, n2->rn_len);
                    178:        if ((depth != 0) && (slen > depth))
                    179:                slen = depth;
1.4       tedu      180:
1.1       jfb       181:        for (i = 0; i < slen; i++) {
                    182:                res = n1->rn_id[i] - n2->rn_id[i];
                    183:                if (res < 0)
                    184:                        return (1);
                    185:                else if (res > 0)
                    186:                        return (-1);
                    187:        }
                    188:
                    189:        if (n1->rn_len > n2->rn_len)
                    190:                return (-1);
                    191:        else if (n2->rn_len > n1->rn_len)
                    192:                return (1);
                    193:
                    194:        return (0);
                    195: }
                    196:
                    197:
                    198: /*
                    199:  * rcsnum_aton()
                    200:  *
                    201:  * Translate the string <str> containing a sequence of digits and periods into
                    202:  * its binary representation, which is stored in <nump>.  The address of the
                    203:  * first byte not part of the number is stored in <ep> on return, if it is not
                    204:  * NULL.
                    205:  * Returns 0 on success, or -1 on failure.
                    206:  */
                    207: int
                    208: rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
                    209: {
1.6       jfb       210:        u_int32_t val;
1.1       jfb       211:        const char *sp;
                    212:        void *tmp;
                    213:
1.2       vincent   214:        if (nump->rn_id == NULL) {
                    215:                nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
1.6       jfb       216:                if (nump->rn_id == NULL) {
                    217:                        cvs_log(LP_ERRNO, "failed to allocate RCSNUM");
1.2       vincent   218:                        return (-1);
1.6       jfb       219:                }
1.2       vincent   220:        }
1.1       jfb       221:
                    222:        nump->rn_len = 0;
1.6       jfb       223:        nump->rn_id[0] = 0;
1.1       jfb       224:
1.3       jfb       225:        for (sp = str;; sp++) {
                    226:                if (!isdigit(*sp) && (*sp != '.'))
1.1       jfb       227:                        break;
                    228:
                    229:                if (*sp == '.') {
1.6       jfb       230:                        if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
                    231:                                cvs_log(LP_ERR,
                    232:                                    "RCSNUM exceeds maximum length");
                    233:                                goto rcsnum_aton_failed;
                    234:                        }
                    235:
1.1       jfb       236:                        nump->rn_len++;
                    237:                        tmp = realloc(nump->rn_id,
                    238:                            (nump->rn_len + 1) * sizeof(u_int16_t));
1.9       jfb       239:                        if (tmp == NULL)
1.6       jfb       240:                                goto rcsnum_aton_failed;
1.1       jfb       241:                        nump->rn_id = (u_int16_t *)tmp;
1.2       vincent   242:                        nump->rn_id[nump->rn_len] = 0;
1.1       jfb       243:                        continue;
                    244:                }
                    245:
1.6       jfb       246:                val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30);
                    247:                if (val > RCSNUM_MAXNUM) {
                    248:                        cvs_log(LP_ERR, "RCSNUM overflow");
                    249:                        goto rcsnum_aton_failed;
                    250:                }
                    251:
                    252:                nump->rn_id[nump->rn_len] = val;
1.1       jfb       253:        }
                    254:
                    255:        if (ep != NULL)
1.5       jfb       256:                *(const char **)ep = sp;
1.1       jfb       257:
                    258:        nump->rn_len++;
                    259:        return (nump->rn_len);
1.6       jfb       260:
                    261: rcsnum_aton_failed:
                    262:        nump->rn_len = 0;
                    263:        free(nump->rn_id);
                    264:        nump->rn_id = NULL;
                    265:        return (-1);
1.1       jfb       266: }