[BACK]Return to xstd.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/xstd.c, Revision 1.9

1.9     ! kristaps    1: /* $Id: xstd.c,v 1.8 2009/03/08 11:41:22 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.3       kristaps   19: #include <assert.h>
1.1       kristaps   20: #include <err.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "private.h"
                     25:
1.4       kristaps   26: /*
                     27:  * Contains wrappers for common functions to simplify their general
                     28:  * usage throughout this codebase.
                     29:  */
                     30:
1.2       kristaps   31: #ifdef __linux__
                     32: extern size_t           strlcat(char *, const char *, size_t);
                     33: extern size_t           strlcpy(char *, const char *, size_t);
                     34: #endif
                     35:
1.5       kristaps   36:
                     37: int
1.1       kristaps   38: xstrcmp(const char *p1, const char *p2)
                     39: {
                     40:
                     41:        return(0 == strcmp(p1, p2));
                     42: }
                     43:
                     44: int
                     45: xstrlcat(char *dst, const char *src, size_t sz)
                     46: {
                     47:
                     48:        return(strlcat(dst, src, sz) < sz);
                     49: }
                     50:
                     51: int
                     52: xstrlcpy(char *dst, const char *src, size_t sz)
                     53: {
                     54:
                     55:        return(strlcpy(dst, src, sz) < sz);
                     56: }
                     57:
                     58: void *
1.6       kristaps   59: xrealloc(void *ptr, size_t sz)
                     60: {
                     61:        void            *p;
                     62:
                     63:        if (NULL == (p = realloc(ptr, sz)))
                     64:                err(EXIT_FAILURE, "realloc");
                     65:        return(p);
                     66: }
                     67:
                     68: void *
1.1       kristaps   69: xcalloc(size_t num, size_t sz)
                     70: {
                     71:        void            *p;
                     72:
                     73:        if (NULL == (p = calloc(num, sz)))
                     74:                err(EXIT_FAILURE, "calloc");
                     75:        return(p);
                     76: }
                     77:
                     78: char *
                     79: xstrdup(const char *p)
                     80: {
                     81:        char            *pp;
                     82:
                     83:        if (NULL == (pp = strdup(p)))
                     84:                err(EXIT_FAILURE, "strdup");
                     85:        return(pp);
                     86: }
                     87:
1.3       kristaps   88: int
1.7       kristaps   89: xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz)
1.3       kristaps   90: {
                     91:        char             *p;
                     92:
                     93:        assert(sz > 0);
                     94:        assert(buf);
                     95:        *buf = 0;
                     96:
                     97:        for ( ; n; n = n->next) {
                     98:                assert(MDOC_TEXT == n->type);
1.8       kristaps   99:                p = n->string;
1.3       kristaps  100:                if ( ! xstrlcat(buf, p, sz))
                    101:                        return(0);
                    102:                if (n->next && ! xstrlcat(buf, " ", sz))
                    103:                        return(0);
                    104:        }
                    105:
                    106:        return(1);
                    107: }
                    108:
1.2       kristaps  109: #ifdef __linux__
                    110: /*     $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $      */
                    111:
                    112: /*
                    113:  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
                    114:  *
                    115:  * Permission to use, copy, modify, and distribute this software for any
                    116:  * purpose with or without fee is hereby granted, provided that the above
                    117:  * copyright notice and this permission notice appear in all copies.
                    118:  *
                    119:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                    120:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                    121:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                    122:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                    123:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                    124:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                    125:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                    126:  */
                    127:
                    128: #include <sys/types.h>
                    129: #include <string.h>
                    130:
                    131:
                    132: size_t
                    133: strlcat(char *dst, const char *src, size_t siz)
                    134: {
                    135:        char *d = dst;
                    136:        const char *s = src;
                    137:        size_t n = siz;
                    138:        size_t dlen;
                    139:
                    140:        /* Find the end of dst and adjust bytes left but don't go past end */
                    141:        while (n-- != 0 && *d != '\0')
                    142:                d++;
                    143:        dlen = d - dst;
                    144:        n = siz - dlen;
                    145:
                    146:        if (n == 0)
                    147:                return(dlen + strlen(s));
                    148:        while (*s != '\0') {
                    149:                if (n != 1) {
                    150:                        *d++ = *s;
                    151:                        n--;
                    152:                }
                    153:                s++;
                    154:        }
                    155:        *d = '\0';
                    156:
                    157:        return(dlen + (s - src));       /* count does not include NUL */
                    158: }
                    159:
                    160: size_t
                    161: strlcpy(char *dst, const char *src, size_t siz)
                    162: {
                    163:        char *d = dst;
                    164:        const char *s = src;
                    165:        size_t n = siz;
                    166:
                    167:        /* Copy as many bytes as will fit */
                    168:        if (n != 0) {
                    169:                while (--n != 0) {
                    170:                        if ((*d++ = *s++) == '\0')
                    171:                                break;
                    172:                }
                    173:        }
                    174:
                    175:        /* Not enough room in dst, add NUL and traverse rest of src */
                    176:        if (n == 0) {
                    177:                if (siz != 0)
                    178:                        *d = '\0';              /* NUL-terminate dst */
                    179:                while (*s++)
                    180:                        ;
                    181:        }
                    182:
                    183:        return(s - src - 1);    /* count does not include NUL */
                    184: }
                    185: #endif

CVSweb