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

Annotation of mandoc/xstd.c, Revision 1.13

1.13    ! kristaps    1: /* $Id: xstd.c,v 1.12 2009/03/19 16:40:49 kristaps Exp $ */
1.1       kristaps    2: /*
1.11      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1       kristaps    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:
1.13    ! kristaps   24: #include "libmdoc.h"
1.1       kristaps   25:
1.12      kristaps   26: #ifdef __linux__
                     27: extern size_t                  strlcpy(char *, const char *, size_t);
                     28: extern size_t                  strlcat(char *, const char *, size_t);
                     29: #endif
                     30:
1.4       kristaps   31: /*
                     32:  * Contains wrappers for common functions to simplify their general
                     33:  * usage throughout this codebase.
                     34:  */
                     35:
1.5       kristaps   36: int
1.1       kristaps   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 *
1.6       kristaps   58: xrealloc(void *ptr, size_t sz)
                     59: {
                     60:        void            *p;
                     61:
                     62:        if (NULL == (p = realloc(ptr, sz)))
                     63:                err(EXIT_FAILURE, "realloc");
                     64:        return(p);
                     65: }
                     66:
1.1       kristaps   67:
                     68: char *
                     69: xstrdup(const char *p)
                     70: {
                     71:        char            *pp;
                     72:
                     73:        if (NULL == (pp = strdup(p)))
                     74:                err(EXIT_FAILURE, "strdup");
                     75:        return(pp);
                     76: }
                     77:
1.3       kristaps   78: int
1.7       kristaps   79: xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz)
1.3       kristaps   80: {
                     81:        char             *p;
                     82:
                     83:        assert(sz > 0);
                     84:        assert(buf);
                     85:        *buf = 0;
                     86:
                     87:        for ( ; n; n = n->next) {
                     88:                assert(MDOC_TEXT == n->type);
1.8       kristaps   89:                p = n->string;
1.3       kristaps   90:                if ( ! xstrlcat(buf, p, sz))
                     91:                        return(0);
                     92:                if (n->next && ! xstrlcat(buf, " ", sz))
                     93:                        return(0);
                     94:        }
                     95:
                     96:        return(1);
                     97: }

CVSweb