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