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

Annotation of mandoc/xstd.c, Revision 1.4

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

CVSweb