]> jfr.im git - solanum.git/blobdiff - librb/src/tools.c
librb: rb_snprinf_append: handle error return value from vsnprintf()
[solanum.git] / librb / src / tools.c
index 0f4b68c142c6f20c217525bacdae2d7d7b92d88d..05c7e3eb451f31257e7a4c3e27fd288386e5d4ae 100644 (file)
@@ -20,7 +20,6 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  *  USA
  *
- *  $Id: tools.c 26170 2008-10-26 20:59:07Z androsyn $
  *
  *  Here is the original header:
  *
@@ -32,6 +31,7 @@
  *  defined, tools.h will build inlined versions of the functions
  *  on supported compilers
  */
+
 #define _GNU_SOURCE 1
 #include <librb_config.h>
 #include <rb_lib.h>
@@ -83,7 +83,7 @@ rb_free_rb_dlink_node(rb_dlink_node *ptr)
  *   Changes a given buffer into an array of parameters.
  *   Taken from ircd-ratbox.
  *
- * inputs      - string to parse, array to put in
+ * inputs      - string to parse, array to put in (size >= maxpara)
  * outputs     - number of parameters
  */
 int
@@ -92,8 +92,6 @@ rb_string_to_array(char *string, char **parv, int maxpara)
        char *p, *xbuf = string;
        int x = 0;
 
-       parv[x] = NULL;
-
        if(string == NULL || string[0] == '\0')
                return x;
 
@@ -108,13 +106,11 @@ rb_string_to_array(char *string, char **parv, int maxpara)
                {
                        xbuf++;
                        parv[x++] = xbuf;
-                       parv[x] = NULL;
                        return x;
                }
                else
                {
                        parv[x++] = xbuf;
-                       parv[x] = NULL;
                        if((p = strchr(xbuf, ' ')) != NULL)
                        {
                                *p++ = '\0';
@@ -134,10 +130,106 @@ rb_string_to_array(char *string, char **parv, int maxpara)
                p++;
 
        parv[x++] = p;
-       parv[x] = NULL;
        return x;
 }
 
+#ifndef HAVE_STRCASECMP
+#ifndef _WIN32
+/* Fallback taken from FreeBSD. --Elizafox */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       const unsigned char *us1 = (const unsigned char *)s1;
+       const unsigned char *us2 = (const unsigned char *)s2;
+
+       while (tolower(*us1) == tolower(*us2++))
+       {
+               if (*us1++ == '\0')
+                       return 0;
+       }
+
+       return (tolower(*us1) - tolower(*--us2));
+}
+#else /* _WIN32 */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       return stricmp(s1, s2);
+}
+#endif /* _WIN32 */
+#else /* HAVE_STRCASECMP */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       return strcasecmp(s1, s2);
+}
+#endif
+
+#ifndef HAVE_STRNCASECMP
+#ifndef _WIN32
+/* Fallback taken from FreeBSD. --Elizafox */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       if (n != 0)
+       {
+               const unsigned char *us1 = (const unsigned char *)s1;
+               const unsigned char *us2 = (const unsigned char *)s2;
+
+               do
+               {
+                       if (tolower(*us1) != tolower(*us2++))
+                               return (tolower(*us1) - tolower(*--us2));
+                       if (*us1++ == '\0')
+                               break;
+               } while (--n != 0);
+       }
+       return 0;
+}
+#else /* _WIN32 */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       return strnicmp(s1, s2, n);
+}
+#endif /* _WIN32 */
+#else /* HAVE_STRNCASECMP */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       return strncasecmp(s1, s2, n);
+}
+#endif
+
+#ifndef HAVE_STRCASESTR
+/* Fallback taken from FreeBSD. --Elizafox */
+char *
+rb_strcasestr(const char *s, const char *find)
+{
+       char c, sc;
+       size_t len;
+
+       if ((c = *find++) != 0) {
+               c = tolower((unsigned char)c);
+               len = strlen(find);
+               do {
+                       do {
+                               if ((sc = *s++) == 0)
+                                       return (NULL);
+                       } while ((char)tolower((unsigned char)sc) != c);
+               } while (rb_strncasecmp(s, find, len) != 0);
+               s--;
+       }
+       return ((char *)s);
+}
+#else
+char *
+rb_strcasestr(const char *s, const char *find)
+{
+       return strcasestr(s, find);
+}
+#endif
+
 #ifndef HAVE_STRLCAT
 size_t
 rb_strlcat(char *dest, const char *src, size_t count)
@@ -210,25 +302,26 @@ rb_strnlen(const char *s, size_t count)
 int
 rb_snprintf_append(char *str, size_t len, const char *format, ...)
 {
-       int x;
-
        if(len == 0)
-               return 0;
+               return -1;
 
-       x = strlen(str);
+       int orig_len = strlen(str);
 
-       if(len < x)
+       if((int)len < orig_len)
        {
                str[len - 1] = '\0';
-               return (int)len - 1;
+               return len - 1;
        }
 
        va_list ap;
        va_start(ap, format);
-       x = (vsnprintf(str + x, len - x, format, ap) + (int)x);
+       int append_len = vsnprintf(str + orig_len, len - orig_len, format, ap);
        va_end(ap);
 
-       return (x);
+       if (append_len < 0)
+               return append_len;
+
+       return (orig_len + append_len);
 }
 
 /* rb_basename