A working wcscasecmp replacement.
[doldaconnect.git] / common / utils.c
index e3178d5..6dac3dc 100644 (file)
@@ -79,14 +79,19 @@ char *vsprintf2(char *format, va_list al)
 {
     int ret;
     char *buf;
+    va_list al2;
     
-    ret = vsnprintf(NULL, 0, format, al);
+    va_copy(al2, al);
+    ret = vsnprintf(NULL, 0, format, al2);
+    va_end(al2);
     if((buf = malloc(ret + 1)) == NULL)
     {
        LOGOOM(ret + 1);
        return(NULL);
     }
-    vsnprintf(buf, ret + 1, format, al);
+    va_copy(al2, al);
+    vsnprintf(buf, ret + 1, format, al2);
+    va_end(al2);
     return(buf);
 }
 
@@ -106,10 +111,18 @@ wchar_t *vswprintf2(wchar_t *format, va_list al)
     int ret;
     wchar_t *buf;
     size_t bufsize;
+    va_list al2;
     
     buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
-    while((ret = vswprintf(buf, bufsize, format, al)) < 0)
+    while(1)
+    {
+       va_copy(al2, al);
+       ret = vswprintf(buf, bufsize, format, al2);
+       va_end(al2);
+       if(ret >= 0)
+           break;
        buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
+    }
     if(bufsize > ret + 1)
        buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
     return(buf);
@@ -428,11 +441,7 @@ int wcsexists(wchar_t *h, wchar_t *n)
 #ifndef HAVE_WCSCASECMP
 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
 {
-    while(towlower(*s1) == towlower(*s2))
-    {
-       if(*s1 == L'\0')
-           return(0);
-    }
+    for(; (towlower(*s1) == towlower(*s2)) && (*s1 != L'\0'); s1++, s2++);
     return(towlower(*s1) - towlower(*s2));
 }
 #endif