xref: /freebsd/contrib/pkgconf/libpkgconf/bsdstubs.c (revision a3cefe7f2b4df0f70ff92d4570ce18e517af43ec)
1 /*	$OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $	*/
2 /*	$OpenBSD: strlcat.c,v 1.12 2005/03/30 20:13:52 otto Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <string.h>
23 #include <errno.h>
24 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 
28 #include <libpkgconf/bsdstubs.h>
29 #include <libpkgconf/config.h>
30 
31 #if !HAVE_DECL_STRLCPY
32 /*
33  * Copy src to string dst of size siz.  At most siz-1 characters
34  * will be copied.  Always NUL terminates (unless siz == 0).
35  * Returns strlen(src); if retval >= siz, truncation occurred.
36  */
37 static inline size_t
strlcpy(char * dst,const char * src,size_t siz)38 strlcpy(char *dst, const char *src, size_t siz)
39 {
40 	char *d = dst;
41 	const char *s = src;
42 	size_t n = siz;
43 
44 	/* Copy as many bytes as will fit */
45 	if (n != 0) {
46 		while (--n != 0) {
47 			if ((*d++ = *s++) == '\0')
48 				break;
49 		}
50 	}
51 
52 	/* Not enough room in dst, add NUL and traverse rest of src */
53 	if (n == 0) {
54 		if (siz != 0)
55 			*d = '\0';		/* NUL-terminate dst */
56 		while (*s++)
57 			;
58 	}
59 
60 	return(s - src - 1);	/* count does not include NUL */
61 }
62 #endif
63 
64 #if !HAVE_DECL_STRLCAT
65 /*
66  * Appends src to string dst of size siz (unlike strncat, siz is the
67  * full size of dst, not space left).  At most siz-1 characters
68  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
69  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
70  * If retval >= siz, truncation occurred.
71  */
72 static inline size_t
strlcat(char * dst,const char * src,size_t siz)73 strlcat(char *dst, const char *src, size_t siz)
74 {
75 	char *d = dst;
76 	const char *s = src;
77 	size_t n = siz;
78 	size_t dlen;
79 
80 	/* Find the end of dst and adjust bytes left but don't go past end */
81 	while (n-- != 0 && *d != '\0')
82 		d++;
83 	dlen = d - dst;
84 	n = siz - dlen;
85 
86 	if (n == 0)
87 		return(dlen + strlen(s));
88 	while (*s != '\0') {
89 		if (n != 1) {
90 			*d++ = *s;
91 			n--;
92 		}
93 		s++;
94 	}
95 	*d = '\0';
96 
97 	return(dlen + (s - src));	/* count does not include NUL */
98 }
99 #endif
100 
101 /*
102  * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
103  *
104  * Permission to use, copy, modify, and/or distribute this software for any
105  * purpose with or without fee is hereby granted, provided that the above
106  * copyright notice and this permission notice appear in all copies.
107  *
108  * This software is provided 'as is' and without any warranty, express or
109  * implied.  In no event shall the authors be liable for any damages arising
110  * from the use of this software.
111  */
112 
113 #if !HAVE_DECL_STRNDUP
114 /*
115  * Creates a memory buffer and copies at most 'len' characters to it.
116  * If 'len' is less than the length of the source string, truncation occured.
117  */
118 static inline char *
strndup(const char * src,size_t len)119 strndup(const char *src, size_t len)
120 {
121 	char *out = malloc(len + 1);
122 	pkgconf_strlcpy(out, src, len + 1);
123 	return out;
124 }
125 #endif
126 
127 #if !HAVE_DECL_PLEDGE
128 static inline int
pledge(const char * promises,const char * execpromises)129 pledge(const char *promises, const char *execpromises)
130 {
131 	(void) promises;
132 	(void) execpromises;
133 
134 	return 0;
135 }
136 #endif
137 
138 #if !HAVE_DECL_UNVEIL
139 static inline int
unveil(const char * path,const char * permissions)140 unveil(const char *path, const char *permissions)
141 {
142 	(void) path;
143 	(void) permissions;
144 
145 	return 0;
146 }
147 #endif
148 
149 size_t
pkgconf_strlcpy(char * dst,const char * src,size_t siz)150 pkgconf_strlcpy(char *dst, const char *src, size_t siz)
151 {
152 	return strlcpy(dst, src, siz);
153 }
154 
155 size_t
pkgconf_strlcat(char * dst,const char * src,size_t siz)156 pkgconf_strlcat(char *dst, const char *src, size_t siz)
157 {
158 	return strlcat(dst, src, siz);
159 }
160 
161 char *
pkgconf_strndup(const char * src,size_t len)162 pkgconf_strndup(const char *src, size_t len)
163 {
164 	return strndup(src, len);
165 }
166 
167 #if !HAVE_DECL_REALLOCARRAY
168 void *
reallocarray(void * ptr,size_t m,size_t n)169 reallocarray(void *ptr, size_t m, size_t n)
170 {
171 	if (n && m > -1 / n)
172 	{
173 		errno = ENOMEM;
174 		return 0;
175 	}
176 
177 	return realloc(ptr, m * n);
178 }
179 #endif
180 
181 void *
pkgconf_reallocarray(void * ptr,size_t m,size_t n)182 pkgconf_reallocarray(void *ptr, size_t m, size_t n)
183 {
184 	return reallocarray(ptr, m, n);
185 }
186 
187 int
pkgconf_pledge(const char * promises,const char * execpromises)188 pkgconf_pledge(const char *promises, const char *execpromises)
189 {
190 	return pledge(promises, execpromises);
191 }
192 
193 int
pkgconf_unveil(const char * path,const char * permissions)194 pkgconf_unveil(const char *path, const char *permissions)
195 {
196 	return unveil(path, permissions);
197 }
198