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

Annotation of mandoc/xstd.c, Revision 1.11

1.11    ! kristaps    1: /* $Id: xstd.c,v 1.10 2009/03/16 22:19:19 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:
                     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.5       kristaps   31: int
1.1       kristaps   32: xstrcmp(const char *p1, const char *p2)
                     33: {
                     34:
                     35:        return(0 == strcmp(p1, p2));
                     36: }
                     37:
                     38: int
                     39: xstrlcat(char *dst, const char *src, size_t sz)
                     40: {
                     41:
                     42:        return(strlcat(dst, src, sz) < sz);
                     43: }
                     44:
                     45: int
                     46: xstrlcpy(char *dst, const char *src, size_t sz)
                     47: {
                     48:
                     49:        return(strlcpy(dst, src, sz) < sz);
                     50: }
                     51:
                     52: void *
1.6       kristaps   53: xrealloc(void *ptr, size_t sz)
                     54: {
                     55:        void            *p;
                     56:
                     57:        if (NULL == (p = realloc(ptr, sz)))
                     58:                err(EXIT_FAILURE, "realloc");
                     59:        return(p);
                     60: }
                     61:
                     62: void *
1.1       kristaps   63: xcalloc(size_t num, size_t sz)
                     64: {
                     65:        void            *p;
                     66:
                     67:        if (NULL == (p = calloc(num, sz)))
                     68:                err(EXIT_FAILURE, "calloc");
                     69:        return(p);
                     70: }
                     71:
                     72: char *
                     73: xstrdup(const char *p)
                     74: {
                     75:        char            *pp;
                     76:
                     77:        if (NULL == (pp = strdup(p)))
                     78:                err(EXIT_FAILURE, "strdup");
                     79:        return(pp);
                     80: }
                     81:
1.3       kristaps   82: int
1.7       kristaps   83: xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz)
1.3       kristaps   84: {
                     85:        char             *p;
                     86:
                     87:        assert(sz > 0);
                     88:        assert(buf);
                     89:        *buf = 0;
                     90:
                     91:        for ( ; n; n = n->next) {
                     92:                assert(MDOC_TEXT == n->type);
1.8       kristaps   93:                p = n->string;
1.3       kristaps   94:                if ( ! xstrlcat(buf, p, sz))
                     95:                        return(0);
                     96:                if (n->next && ! xstrlcat(buf, " ", sz))
                     97:                        return(0);
                     98:        }
                     99:
                    100:        return(1);
                    101: }

CVSweb