make rrd_mkdir_p work on bsd* unixes too ... their dirname call works differently...
[rrdtool.git] / src / rrd_utils.c
index 3936cff..add19c3 100644 (file)
 
 #include <assert.h>
 #include <errno.h>
-#include <libgen.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifndef _MSC_VER
+#include <libgen.h>
 #include <unistd.h>
-
+#endif
 #ifdef WIN32
 #      define random() rand()
 #      define srandom(x) srand(x)
 #      define getpid() 0
 #endif /* WIN32 */
 
+#ifndef S_ISDIR
+#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
+#endif
+
 /* make sure that the random number generator seeded EXACTLY ONCE */
 long rrd_random(void)
 {
@@ -50,29 +55,53 @@ long rrd_random(void)
     return random();
 }
 
-/* rrd_add_ptr: add a pointer to a dynamically sized array of pointers,
- * realloc as necessary.  returns 1 on success, 0 on failure.
+/* rrd_add_ptr_chunk: add a pointer to a dynamically sized array of
+ * pointers, realloc as necessary in multiples of "chunk".
+ *
+ * "alloc" is the number of pointers allocated
+ * "dest_size" is the number of valid pointers
+ *
+ * returns 1 on success, 0 on failure.
  */
 
-int rrd_add_ptr(void ***dest, size_t *dest_size, void *src)
+int rrd_add_ptr_chunk(void ***dest, size_t *dest_size, void *src,
+                      size_t *alloc, size_t chunk)
 {
     void **temp;
 
     assert(dest != NULL);
+    assert(alloc != NULL);
+    assert(*alloc >= *dest_size);
 
-    temp = (void **) rrd_realloc(*dest, (*dest_size+1) * sizeof(*dest));
-    if (!temp)
-        return 0;
+    if (*alloc == *dest_size)
+    {
+        temp = (void **) rrd_realloc(*dest, (*alloc+chunk) * sizeof(*dest));
+        if (!temp)
+            return 0;
+
+        *dest = temp;
+        *alloc += chunk;
+    }
 
-    *dest = temp;
-    temp[*dest_size] = src;
+    (*dest)[*dest_size] = src;
     (*dest_size)++;
 
     return 1;
 }
 
-/* like rrd_add_ptr, but calls strdup() on a string first. */
-int rrd_add_strdup(char ***dest, size_t *dest_size, char *src)
+/* rrd_add_ptr: add a pointer to a dynamically sized array of pointers,
+ * realloc as necessary.  returns 1 on success, 0 on failure.
+ */
+int rrd_add_ptr(void ***dest, size_t *dest_size, void *src)
+{
+    size_t alloc = *dest_size;
+
+    return rrd_add_ptr_chunk(dest, dest_size, src, &alloc, 1);
+}
+
+/* like rrd_add_ptr_chunk, but calls strdup() on a string first. */
+int rrd_add_strdup_chunk(char ***dest, size_t *dest_size, char *src,
+                         size_t *alloc, size_t chunk)
 {
     char *dup_src;
     int add_ok;
@@ -84,13 +113,20 @@ int rrd_add_strdup(char ***dest, size_t *dest_size, char *src)
     if (!dup_src)
         return 0;
 
-    add_ok = rrd_add_ptr((void ***)dest, dest_size, (void *)dup_src);
+    add_ok = rrd_add_ptr_chunk((void ***)dest, dest_size, (void *)dup_src, alloc, chunk);
     if (!add_ok)
         free(dup_src);
 
     return add_ok;
 }
 
+int rrd_add_strdup(char ***dest, size_t *dest_size, char *src)
+{
+    size_t alloc = *dest_size;
+
+    return rrd_add_strdup_chunk(dest, dest_size, src, &alloc, 1);
+}
+
 void rrd_free_ptrs(void ***src, size_t *cnt)
 {
     void **sp;
@@ -140,20 +176,39 @@ int rrd_mkdir_p(const char *pathname, mode_t mode)
     if (NULL == (pathname_copy = strdup(pathname)))
         return -1;
 
-    base_dir = dirname(pathname_copy);
+#ifndef _MSC_VER
+    /* the data pointedd too by dirname might change too (bsd) */
+    if (NULL == (base_dir = strdup(dirname(pathname_copy)))) {
+        free(pathname_copy);
+        return -1;
+    }
+#else
+    _splitpath(pathname_copy, NULL, base_dir, NULL, NULL);
+#endif
 
     if (0 != rrd_mkdir_p(base_dir, mode)) {
         int orig_errno = errno;
         free(pathname_copy);
+#ifndef _MSC_VER
+        free(base_dir);
+#endif
         errno = orig_errno;
         return -1;
     }
 
     free(pathname_copy);
+#ifndef _MSC_VER
+    free(base_dir);
+#endif
 
     /* keep errno as set by mkdir() */
+#ifdef _MSC_VER
+    if (0 != mkdir(pathname))
+        return -1;
+#else
     if (0 != mkdir(pathname, mode))
         return -1;
+#endif
     return 0;
 } /* rrd_mkdir_p */