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
24 #include <libpkgconf/bsdstubs.h>
25 #include <libpkgconf/config.h>
26
27 #ifndef HAVE_STRLCPY
28 /*
29 * Copy src to string dst of size siz. At most siz-1 characters
30 * will be copied. Always NUL terminates (unless siz == 0).
31 * Returns strlen(src); if retval >= siz, truncation occurred.
32 */
33 static inline size_t
strlcpy(char * dst,const char * src,size_t siz)34 strlcpy(char *dst, const char *src, size_t siz)
35 {
36 char *d = dst;
37 const char *s = src;
38 size_t n = siz;
39
40 /* Copy as many bytes as will fit */
41 if (n != 0) {
42 while (--n != 0) {
43 if ((*d++ = *s++) == '\0')
44 break;
45 }
46 }
47
48 /* Not enough room in dst, add NUL and traverse rest of src */
49 if (n == 0) {
50 if (siz != 0)
51 *d = '\0'; /* NUL-terminate dst */
52 while (*s++)
53 ;
54 }
55
56 return(s - src - 1); /* count does not include NUL */
57 }
58 #endif
59
60 #ifndef HAVE_STRLCAT
61 /*
62 * Appends src to string dst of size siz (unlike strncat, siz is the
63 * full size of dst, not space left). At most siz-1 characters
64 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
65 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
66 * If retval >= siz, truncation occurred.
67 */
68 static inline size_t
strlcat(char * dst,const char * src,size_t siz)69 strlcat(char *dst, const char *src, size_t siz)
70 {
71 char *d = dst;
72 const char *s = src;
73 size_t n = siz;
74 size_t dlen;
75
76 /* Find the end of dst and adjust bytes left but don't go past end */
77 while (n-- != 0 && *d != '\0')
78 d++;
79 dlen = d - dst;
80 n = siz - dlen;
81
82 if (n == 0)
83 return(dlen + strlen(s));
84 while (*s != '\0') {
85 if (n != 1) {
86 *d++ = *s;
87 n--;
88 }
89 s++;
90 }
91 *d = '\0';
92
93 return(dlen + (s - src)); /* count does not include NUL */
94 }
95 #endif
96
97 /*
98 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
99 *
100 * Permission to use, copy, modify, and/or distribute this software for any
101 * purpose with or without fee is hereby granted, provided that the above
102 * copyright notice and this permission notice appear in all copies.
103 *
104 * This software is provided 'as is' and without any warranty, express or
105 * implied. In no event shall the authors be liable for any damages arising
106 * from the use of this software.
107 */
108
109 #ifndef HAVE_STRNDUP
110 /*
111 * Creates a memory buffer and copies at most 'len' characters to it.
112 * If 'len' is less than the length of the source string, truncation occured.
113 */
114 static inline char *
strndup(const char * src,size_t len)115 strndup(const char *src, size_t len)
116 {
117 char *out = malloc(len + 1);
118 pkgconf_strlcpy(out, src, len + 1);
119 return out;
120 }
121 #endif
122
123 size_t
pkgconf_strlcpy(char * dst,const char * src,size_t siz)124 pkgconf_strlcpy(char *dst, const char *src, size_t siz)
125 {
126 return strlcpy(dst, src, siz);
127 }
128
129 size_t
pkgconf_strlcat(char * dst,const char * src,size_t siz)130 pkgconf_strlcat(char *dst, const char *src, size_t siz)
131 {
132 return strlcat(dst, src, siz);
133 }
134
135 char *
pkgconf_strndup(const char * src,size_t len)136 pkgconf_strndup(const char *src, size_t len)
137 {
138 return strndup(src, len);
139 }
140