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