1 /* Leave the OpenBSD version below so we can track upstream fixes */
2 /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
5 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
36 #include "eina_private.h"
39 /*============================================================================*
41 *============================================================================*/
48 * Internal helper function used by eina_str_has_suffix() and
49 * eina_str_has_extension()
51 static inline Eina_Bool
52 eina_str_has_suffix_helper(const char *str,
54 int (*cmp)(const char *, const char *))
59 str_len = strlen(str);
60 suffix_len = eina_strlen_bounded(suffix, str_len);
61 if (suffix_len == (size_t)-1)
64 return cmp(str + str_len - suffix_len, suffix) == 0;
68 eina_str_split_full_helper(const char *str,
71 unsigned int *elements)
89 /* count tokens and check strlen(str) */
92 const char *d = delim, *d_end = d + dlen;
93 const char *tmp = src;
94 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
96 if (EINA_LIKELY(*d != *tmp))
99 if (EINA_UNLIKELY(d == d_end))
109 if ((max_tokens > 0) && (tokens > (unsigned int)max_tokens))
112 str_array = malloc(sizeof(char *) * (tokens + 2));
131 /* copy tokens and string */
137 const char *d = delim, *d_end = d + dlen;
138 const char *tmp = src;
139 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
141 if (EINA_LIKELY(*d != *tmp))
144 if (EINA_UNLIKELY(d == d_end))
150 str_array[tokens] = s;
160 str_array[tokens + 1] = NULL;
162 *elements = (tokens + 1);
171 /*============================================================================*
173 *============================================================================*/
175 /*============================================================================*
177 *============================================================================*/
180 * @addtogroup Eina_String_Group String
182 * @brief These functions provide useful C string management.
189 * @brief Copy a c-string to another.
191 * @param dst The destination string.
192 * @param src The source string.
193 * @param siz The size of the destination string.
194 * @return The length of the source string.
196 * This function copies up to @p siz - 1 characters from the
197 * NUL-terminated string @p src to @p dst, NUL-terminating the result
198 * (unless @p siz is equal to 0). The returned value is the length of
199 * @p src. If the returned value is greater than @p siz, truncation
203 eina_strlcpy(char *dst, const char *src, size_t siz)
206 return strlcpy(dst, src, siz);
212 /* Copy as many bytes as will fit */
216 if ((*d++ = *s++) == '\0')
220 /* Not enough room in dst, add NUL and traverse rest of src */
224 *d = '\0'; /* NUL-terminate dst */
230 return(s - src - 1); /* count does not include NUL */
235 * @brief Append a c-string.
237 * @param dst The destination string.
238 * @param src The source string.
239 * @param siz The size of the destination string.
240 * @return The length of the source string plus MIN(siz, strlen(initial dst))
242 * This function appends @p src to @p dst of size @p siz (unlike
243 * strncat, @p siz is the full size of @p dst, not space left). At
244 * most @p siz - 1 characters will be copied. Always NUL terminates
245 * (unless @p siz <= strlen(dst)). This function returns strlen(src) +
246 * MIN(siz, strlen(initial dst)). If the returned value is greater or
247 * equal than @p siz, truncation occurred.
250 eina_strlcat(char *dst, const char *src, size_t siz)
257 /* Find the end of dst and adjust bytes left but don't go past end */
258 while (n-- != 0 && *d != '\0')
264 return(dlen + strlen(s));
277 return(dlen + (s - src)); /* count does not include NUL */
281 * @brief Check if the given string has the given prefix.
283 * @param str The string to work with.
284 * @param prefix The prefix to check for.
285 * @return #EINA_TRUE if the string has the given prefix, #EINA_FALSE otherwise.
287 * This function returns #EINA_TRUE if @p str has the prefix
288 * @p prefix, #EINA_FALSE otherwise. If the length of @p prefix is
289 * greater than @p str, #EINA_FALSE is returned.
292 eina_str_has_prefix(const char *str, const char *prefix)
297 str_len = strlen(str);
298 prefix_len = eina_strlen_bounded(prefix, str_len);
299 if (prefix_len == (size_t)-1)
302 return (strncmp(str, prefix, prefix_len) == 0);
306 * @brief Check if the given string has the given suffix.
308 * @param str The string to work with.
309 * @param suffix The suffix to check for.
310 * @return #EINA_TRUE if the string has the given suffix, #EINA_FALSE otherwise.
312 * This function returns #EINA_TRUE if @p str has the suffix
313 * @p suffix, #EINA_FALSE otherwise. If the length of @p suffix is
314 * greater than @p str, #EINA_FALSE is returned.
317 * @param str the string to work with
318 * @param suffix the suffix to check for
319 * @return true if str has the given suffix
320 * @brief checks if the string has the given suffix
323 eina_str_has_suffix(const char *str, const char *suffix)
325 return eina_str_has_suffix_helper(str, suffix, strcmp);
329 * @brief Check if the given string has the given suffix.
331 * @param str The string to work with.
332 * @param ext The extension to check for.
333 * @return #EINA_TRUE if the string has the given extension, #EINA_FALSE otherwise.
335 * This function does the same like eina_str_has_suffix(), but with a
336 * case insensitive compare.
339 eina_str_has_extension(const char *str, const char *ext)
341 return eina_str_has_suffix_helper(str, ext, strcasecmp);
345 * @brief Split a string using a delimiter and returns number of elements.
347 * @param str The string to split.
348 * @param delim The string which specifies the places at which to split the string.
349 * @param max_tokens The maximum number of strings to split string into.
350 * @param elements Where to return the number of elements in returned
351 * array (not counting the terminating @c NULL). May be @c NULL.
352 * @return A newly-allocated NULL-terminated array of strings or NULL if it
353 * fails to allocate the array.
355 * This functin splits @p str into a maximum of @p max_tokens pieces,
356 * using the given delimiter @p delim. @p delim is not included in any
357 * of the resulting strings, unless @p max_tokens is reached. If
358 * @p max_tokens is less than @c 1, the string is splitted completely. If
359 * @p max_tokens is reached, the last string in the returned string
360 * array contains the remainder of string. The returned value is a
361 * newly allocated NULL-terminated array of strings or NULL if it fails to
362 * allocate the array. To free it, free the first element of the array and the
365 * @see eina_str_split()
368 eina_str_split_full(const char *str,
371 unsigned int *elements)
373 return eina_str_split_full_helper(str, delim, max_tokens, elements);
378 * @brief Split a string using a delimiter.
380 * @param str The string to split.
381 * @param delim The string which specifies the places at which to split the string.
382 * @param max_tokens The maximum number of strings to split string into.
383 * @return A newly-allocated NULL-terminated array of strings or NULL if it
384 * fails to allocate the array.
386 * This functin splits @p str into a maximum of @p max_tokens pieces,
387 * using the given delimiter @p delim. @p delim is not included in any
388 * of the resulting strings, unless @p max_tokens is reached. If
389 * @p max_tokens is less than @c 1, the string is splitted completely. If
390 * @p max_tokens is reached, the last string in the returned string
391 * array contains the remainder of string. The returned value is a
392 * newly allocated NULL-terminated array of strings or NULL if it fails to
393 * allocate the array. To free it, free the first element of the array and the
397 eina_str_split(const char *str, const char *delim, int max_tokens)
399 return eina_str_split_full_helper(str, delim, max_tokens, NULL);
403 * @brief Join two strings of known length.
405 * @param dst The buffer to store the result.
406 * @param size Size (in byte) of the buffer.
407 * @param sep The separator character to use.
408 * @param a First string to use, before @p sep.
409 * @param a_len length of @p a.
410 * @param b Second string to use, after @p sep.
411 * @param b_len length of @p b.
412 * @return The number of characters printed.
414 * This function joins the strings @p a and @p b (in that order) and
415 * separate them with @p sep. The result is stored in the buffer
416 * @p dst and at most @p size - 1 characters will be written and the
417 * string is NULL-terminated. @p a_len is the length of @p a (not
418 * including '\\0') and @p b_len is the length of @p b (not including
419 * '\\0'). This function returns the number of characters printed (not
420 * including the trailing '\\0' used to end output to strings). Just
421 * like snprintf(), it will not write more than @p size bytes, thus a
422 * returned value of @p size or more means that the output was
425 * @see eina_str_join()
426 * @see eina_str_join_static()
429 eina_str_join_len(char *dst,
437 size_t ret = a_len + b_len + 1;
445 memcpy(dst, a, size - 1);
446 dst[size - 1] = '\0';
450 memcpy(dst, a, a_len);
455 dst[size - 1] = '\0';
462 if (size <= off + b_len + 1)
464 memcpy(dst + off, b, size - off - 1);
465 dst[size - 1] = '\0';
469 memcpy(dst + off, b, b_len);
470 dst[off + b_len] = '\0';
475 * @brief Use iconv to convert a text string from one encoding to another
477 * @param enc_from encoding to convert from
478 * @param enc_to encoding to convert to
479 * @param text text to convert
484 eina_str_convert(const char *enc_from, const char *enc_to, const char *text)
487 char *new_txt, *inp, *outp;
488 size_t inb, outb, outlen, tob, outalloc;
493 ic = iconv_open(enc_to, enc_from);
494 if (ic == (iconv_t)(-1))
497 new_txt = malloc(64);
510 count = iconv(ic, &inp, &inb, &outp, &outb);
511 outlen += tob - outb;
512 if (count == (size_t)(-1))
516 new_txt = realloc(new_txt, outalloc + 64);
517 outp = new_txt + outlen;
521 else if (errno == EILSEQ)
529 else if (errno == EINVAL)
549 if (outalloc == outlen)
550 new_txt = realloc(new_txt, outalloc + 1);
561 eina_str_convert(const char *enc_from __UNUSED__,
562 const char *enc_to __UNUSED__,
563 const char *text __UNUSED__)
570 * @brief Put a \ before and Space( ), \ or ' in a string.
572 * @param str the string to escape
574 * A newly allocated string is returned.
577 eina_str_escape(const char *str)
582 s2 = malloc((strlen(str) * 2) + 1);
586 for (s = str, d = s2; *s != 0; s++, d++)
588 if ((*s == ' ') || (*s == '\\') || (*s == '\''))
601 * @brief Lowercase all the characters in range [A-Z] in the given string.
603 * @param str the string to lowercase
605 * This modifies the original string, changing all characters in [A-Z] to lowercase.
608 eina_str_tolower(char **str)
611 if ((!str) || (!(*str)))
614 for (p = *str; (*p); p++)
619 * @brief Uppercase all the characters in range [a-z] in the given string.
621 * @param str the string to uppercase
623 * This modifies the original string, changing all characters in [a-z] to uppercase.
626 eina_str_toupper(char **str)
629 if ((!str) || (!(*str)))
632 for (p = *str; (*p); p++)