2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
4 /* Leave the OpenBSD version below so we can track upstream fixes */
5 /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
8 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
35 static int eina_str_has_suffix_helper(const char *str, const char *suffix,
36 int (*cmp)(const char *, const char *));
38 * @param dst the destination
39 * @param src the source
40 * @param siz the size of the destination
41 * @return the length of the source string
42 * @brief copy a c-string
44 * Copy src to string dst of size siz. At most siz-1 characters
45 * will be copied. Always NUL terminates (unless siz == 0).
46 * Returns strlen(src); if retval >= siz, truncation occurred.
49 eina_strlcpy(char *dst, const char *src, size_t siz)
52 return strlcpy(dst, src, siz);
58 /* Copy as many bytes as will fit */
63 if ((*d++ = *s++) == '\0')
68 /* Not enough room in dst, add NUL and traverse rest of src */
72 *d = '\0'; /* NUL-terminate dst */
77 return(s - src - 1); /* count does not include NUL */
82 * @param dst the destination
83 * @param src the source
84 * @param siz the size of the destination
85 * @return the length of the source string plus MIN(siz, strlen(initial dst))
86 * @brief append a c-string
88 * Appends src to string dst of size siz (unlike strncat, siz is the
89 * full size of dst, not space left). At most siz-1 characters
90 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
91 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
92 * If retval >= siz, truncation occurred.
95 eina_strlcat(char *dst, const char *src, size_t siz)
102 /* Find the end of dst and adjust bytes left but don't go past end */
103 while (n-- != 0 && *d != '\0')
109 return(dlen + strlen(s));
119 return(dlen + (s - src)); /* count does not include NUL */
123 * @param str the string to work with
124 * @param prefix the prefix to check for
125 * @return true if str has the given prefix
126 * @brief checks if the string has the given prefix
129 eina_str_has_prefix(const char *str, const char *prefix)
134 str_len = strlen(str);
135 prefix_len = strlen(prefix);
136 if (prefix_len > str_len)
139 return (strncmp(str, prefix, prefix_len) == 0);
143 * @param str the string to work with
144 * @param suffix the suffix to check for
145 * @return true if str has the given suffix
146 * @brief checks if the string has the given suffix
149 eina_str_has_suffix(const char *str, const char *suffix)
151 return eina_str_has_suffix_helper(str, suffix, strcmp);
155 * This function does the same like eina_str_has_suffix(), but with a
156 * case insensitive compare.
158 * @param str the string to work with
159 * @param ext the extension to check for
160 * @return true if str has the given extension
161 * @brief checks if the string has the given extension
164 eina_str_has_extension(const char *str, const char *ext)
166 return eina_str_has_suffix_helper(str, ext, strcasecmp);
170 * Internal helper function used by eina_str_has_suffix() and
171 * eina_str_has_extension()
174 eina_str_has_suffix_helper(const char *str, const char *suffix,
175 int (*cmp)(const char *, const char *))
180 str_len = strlen(str);
181 suffix_len = strlen(suffix);
182 if (suffix_len > str_len)
185 return cmp(str + str_len - suffix_len, suffix) == 0;
189 * Splits a string into a maximum of max_tokens pieces, using the given
190 * delimiter. If max_tokens is reached, the final string in the returned
191 * string array contains the remainder of string.
193 * @param str A string to split.
194 * @param delim A string which specifies the places at which to split the
195 * string. The delimiter is not included in any of the
196 * resulting strings, unless max_tokens is reached.
197 * @param max_tokens The maximum number of strings to split string into.
198 * If this is less than 1, the string is split completely.
199 * @return A newly-allocated NULL-terminated array of strings.
200 * To free it: free the first element of the array
201 * and the array itself.
204 eina_str_split(const char *str, const char *delim, int max_tokens)
206 char *s, *sep, **str_array;
213 max_tokens = ((max_tokens <= 0) ? (INT_MAX) : (max_tokens - 1));
215 dlen = strlen(delim);
217 str_array = malloc(sizeof(char *) * (len + 1));
218 for (i = 0; (i < max_tokens) && (sep = strstr(s, delim)); i++)
226 str_array = realloc(str_array, sizeof(char *) * (i + 1));
233 * Join two strings of known length and store the result in @a dst buffer.
235 * @param dst where to store the result.
236 * @param size byte size of dst, will write at most (size - 1)
237 * characters and then the '\0' (null terminator).
238 * @param sep separator character to use.
239 * @param a first string to use, before @a sep.
240 * @param a_len length of @a a, not including '\0' (strlen()-like)
241 * @param b second string to use, after @a sep.
242 * @param b_len length of @a b, not including '\0' (strlen()-like)
244 * @return the number of characters printed (not including the
245 * trailing '\0' used to end output to strings). Just like
246 * snprintf(), it will not write more than @a size bytes, thus a
247 * return value of @a size or more means that the output was
250 * @see eina_str_join() and eina_str_join_static()
253 eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len)
255 size_t ret = a_len + b_len + 1;
258 if (size < 1) return ret;
262 memcpy(dst, a, size - 1);
263 dst[size - 1] = '\0';
267 memcpy(dst, a, a_len);
272 dst[size - 1] = '\0';
279 if (size <= off + b_len + 1)
281 memcpy(dst + off, b, size - off - 1);
282 dst[size - 1] = '\0';
286 memcpy(dst + off, b, b_len);
287 dst[off + b_len] = '\0';