]>
git.cameronkatri.com Git - mandoc.git/blob - xstd.c
1 /* $Id: xstd.c,v 1.9 2009/03/08 19:47:41 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
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.
27 * Contains wrappers for common functions to simplify their general
28 * usage throughout this codebase.
32 extern size_t strlcat(char *, const char *, size_t);
33 extern size_t strlcpy(char *, const char *, size_t);
38 xstrcmp(const char *p1
, const char *p2
)
41 return(0 == strcmp(p1
, p2
));
45 xstrlcat(char *dst
, const char *src
, size_t sz
)
48 return(strlcat(dst
, src
, sz
) < sz
);
52 xstrlcpy(char *dst
, const char *src
, size_t sz
)
55 return(strlcpy(dst
, src
, sz
) < sz
);
59 xrealloc(void *ptr
, size_t sz
)
63 if (NULL
== (p
= realloc(ptr
, sz
)))
64 err(EXIT_FAILURE
, "realloc");
69 xcalloc(size_t num
, size_t sz
)
73 if (NULL
== (p
= calloc(num
, sz
)))
74 err(EXIT_FAILURE
, "calloc");
79 xstrdup(const char *p
)
83 if (NULL
== (pp
= strdup(p
)))
84 err(EXIT_FAILURE
, "strdup");
89 xstrlcpys(char *buf
, const struct mdoc_node
*n
, size_t sz
)
97 for ( ; n
; n
= n
->next
) {
98 assert(MDOC_TEXT
== n
->type
);
100 if ( ! xstrlcat(buf
, p
, sz
))
102 if (n
->next
&& ! xstrlcat(buf
, " ", sz
))
110 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
113 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
115 * Permission to use, copy, modify, and distribute this software for any
116 * purpose with or without fee is hereby granted, provided that the above
117 * copyright notice and this permission notice appear in all copies.
119 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
120 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
122 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
123 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
124 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
125 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
128 #include <sys/types.h>
133 strlcat(char *dst
, const char *src
, size_t siz
)
140 /* Find the end of dst and adjust bytes left but don't go past end */
141 while (n
-- != 0 && *d
!= '\0')
147 return(dlen
+ strlen(s
));
157 return(dlen
+ (s
- src
)); /* count does not include NUL */
161 strlcpy(char *dst
, const char *src
, size_t siz
)
167 /* Copy as many bytes as will fit */
170 if ((*d
++ = *s
++) == '\0')
175 /* Not enough room in dst, add NUL and traverse rest of src */
178 *d
= '\0'; /* NUL-terminate dst */
183 return(s
- src
- 1); /* count does not include NUL */