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