Annotation of mandoc/xstd.c, Revision 1.12
1.12 ! kristaps 1: /* $Id: xstd.c,v 1.11 2009/03/16 23:37:28 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.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:
67: void *
1.1 kristaps 68: xcalloc(size_t num, size_t sz)
69: {
70: void *p;
71:
72: if (NULL == (p = calloc(num, sz)))
73: err(EXIT_FAILURE, "calloc");
74: return(p);
75: }
76:
77: char *
78: xstrdup(const char *p)
79: {
80: char *pp;
81:
82: if (NULL == (pp = strdup(p)))
83: err(EXIT_FAILURE, "strdup");
84: return(pp);
85: }
86:
1.3 kristaps 87: int
1.7 kristaps 88: xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz)
1.3 kristaps 89: {
90: char *p;
91:
92: assert(sz > 0);
93: assert(buf);
94: *buf = 0;
95:
96: for ( ; n; n = n->next) {
97: assert(MDOC_TEXT == n->type);
1.8 kristaps 98: p = n->string;
1.3 kristaps 99: if ( ! xstrlcat(buf, p, sz))
100: return(0);
101: if (n->next && ! xstrlcat(buf, " ", sz))
102: return(0);
103: }
104:
105: return(1);
106: }
CVSweb