2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
14 my_strlcpy(register char *dst, register const char *src, size_t n)
16 for (; *src != '\0' && n > 1; n--)
22 my_strncasecmp(const char *str1, const char *str2, size_t len)
24 register const unsigned char *s1 = (unsigned char *) str1,
25 *s2 = (unsigned char *) str2, *e;
28 for (e = s1 + len - 1; s1 < e && *s1 != '\0' && *s2 != '\0' &&
29 tolower(*s1) == tolower(*s2); s1++, s2++) ;
30 ret = tolower(*s1) - tolower(*s2);
36 my_strndup(const char *ptr, size_t len)
40 if ((p = malloc(len + 1)) != NULL)
41 my_strlcpy(p, ptr, len + 1);
48 my_strdup(const char *str)
50 return (my_strndup(str, strlen(str)));
54 * Sane snprintf(). Acts like snprintf(), but never return -1 or the
55 * value bigger than supplied buffer.
56 * Thanks Adam Zeldis to pointing snprintf()-caused vulnerability
57 * in his audit report.
60 my_snprintf(char *buf, size_t buflen, const char *fmt, ...)
69 n = vsnprintf(buf, buflen, fmt, ap);
72 if (n < 0 || (size_t) n >= buflen)
80 * Verify that given file has certain extension
83 match_extension(const char *path, const char *ext_list)
87 path_len = strlen(path);
89 FOR_EACH_WORD_IN_LIST(ext_list, len)
90 if (len < path_len && path[path_len - len - 1] == '.' &&
91 !my_strncasecmp(path + path_len - len, ext_list, len))