lib/util: use size_t for add_string_to_array().
[samba.git] / lib / util / util_strlist.c
index 8d69eef23336fd8830684b4b73b664260222295f..987fdfbdd33f105a65e5a248f4e20f7d255d5934 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "includes.h"
 #include "system/locale.h"
+#include "lib/util/tsort.h"
 
 #undef strcasecmp
 
@@ -120,7 +121,7 @@ _PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const cha
 
 /**
  * build a null terminated list of strings from an argv-like input string 
- * Entries are seperated by spaces and can be enclosed by quotes. 
+ * Entries are separated by spaces and can be enclosed by quotes.
  * Does NOT support escaping
  */
 _PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
@@ -182,7 +183,7 @@ _PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, con
 /**
  * join a list back to one string 
  */
-_PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
+_PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char separator)
 {
        char *ret = NULL;
        int i;
@@ -193,14 +194,14 @@ _PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char sepera
        ret = talloc_strdup(mem_ctx, list[0]);
 
        for (i = 1; list[i]; i++) {
-               ret = talloc_asprintf_append_buffer(ret, "%c%s", seperator, list[i]);
+               ret = talloc_asprintf_append_buffer(ret, "%c%s", separator, list[i]);
        }
 
        return ret;
 }
 
 /** join a list back to one (shell-like) string; entries 
- * seperated by spaces, using quotes where necessary */
+ * separated by spaces, using quotes where necessary */
 _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
 {
        char *ret = NULL;
@@ -264,7 +265,8 @@ _PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
 /**
    Return true if all the elements of the list match exactly.
  */
-_PUBLIC_ bool str_list_equal(const char **list1, const char **list2)
+_PUBLIC_ bool str_list_equal(const char * const *list1,
+                            const char * const *list2)
 {
        int i;
        
@@ -328,7 +330,7 @@ _PUBLIC_ bool str_list_check(const char **list, const char *s)
 {
        int i;
 
-       for (i=0;list[i];i++) {
+       for (i=0; list != NULL && list[i] != NULL; i++) {
                if (strcmp(list[i], s) == 0) return true;
        }
        return false;
@@ -341,7 +343,7 @@ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s)
 {
        int i;
 
-       for (i=0;list[i];i++) {
+       for (i=0; list != NULL && list[i] != NULL; i++) {
                if (strcasecmp(list[i], s) == 0) return true;
        }
        return false;
@@ -392,7 +394,7 @@ _PUBLIC_ const char **str_list_unique(const char **list)
        }
        list2 = (const char **)talloc_memdup(list, list,
                                             sizeof(list[0])*(len+1));
-       qsort(list2, len, sizeof(list2[0]), QSORT_CAST list_cmp);
+       TYPESAFE_QSORT(list2, len, list_cmp);
        list[0] = list2[0];
        for (i=j=1;i<len;i++) {
                if (strcmp(list2[i], list[j-1]) != 0) {
@@ -444,6 +446,32 @@ _PUBLIC_ const char **str_list_append_const(const char **list1,
        return ret;
 }
 
+/**
+ * Add a string to an array of strings.
+ *
+ * num should be a pointer to an integer that holds the current
+ * number of elements in strings. It will be updated by this function.
+ */
+_PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
+                        const char *str, const char ***strings, size_t *num)
+{
+       char *dup_str = talloc_strdup(mem_ctx, str);
+
+       *strings = talloc_realloc(mem_ctx,
+                                   *strings,
+                                   const char *, ((*num)+1));
+
+       if ((*strings == NULL) || (dup_str == NULL)) {
+               *num = 0;
+               return false;
+       }
+
+       (*strings)[*num] = dup_str;
+       *num += 1;
+
+       return true;
+}
+
 /**
   add an entry to a string list
   this assumes s will not change
@@ -485,3 +513,85 @@ _PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
        ret[i] = NULL;
        return ret;
 }
+
+/**
+ * Needed for making an "unconst" list "const"
+ */
+_PUBLIC_ const char **const_str_list(char **list)
+{
+       return discard_const_p(const char *, list);
+}
+
+/**
+ * str_list_make, v3 version. The v4 version does not
+ * look at quoted strings with embedded blanks, so
+ * do NOT merge this function please!
+ */
+#define S_LIST_ABS 16 /* List Allocation Block Size */
+
+char **str_list_make_v3(TALLOC_CTX *mem_ctx, const char *string,
+       const char *sep)
+{
+       char **list;
+       const char *str;
+       char *s, *tok;
+       int num, lsize;
+
+       if (!string || !*string)
+               return NULL;
+
+       list = talloc_array(mem_ctx, char *, S_LIST_ABS+1);
+       if (list == NULL) {
+               return NULL;
+       }
+       lsize = S_LIST_ABS;
+
+       s = talloc_strdup(list, string);
+       if (s == NULL) {
+               DEBUG(0,("str_list_make: Unable to allocate memory"));
+               TALLOC_FREE(list);
+               return NULL;
+       }
+       if (!sep) sep = LIST_SEP;
+
+       num = 0;
+       str = s;
+
+       while (next_token_talloc(list, &str, &tok, sep)) {
+
+               if (num == lsize) {
+                       char **tmp;
+
+                       lsize += S_LIST_ABS;
+
+                       tmp = talloc_realloc(mem_ctx, list, char *,
+                                                  lsize + 1);
+                       if (tmp == NULL) {
+                               DEBUG(0,("str_list_make: "
+                                       "Unable to allocate memory"));
+                               TALLOC_FREE(list);
+                               return NULL;
+                       }
+
+                       list = tmp;
+
+                       memset (&list[num], 0,
+                               ((sizeof(char*)) * (S_LIST_ABS +1)));
+               }
+
+               list[num] = tok;
+               num += 1;
+       }
+
+       list[num] = NULL;
+
+       TALLOC_FREE(s);
+       return list;
+}
+
+const char **str_list_make_v3_const(TALLOC_CTX *mem_ctx,
+                                   const char *string,
+                                   const char *sep)
+{
+       return const_str_list(str_list_make_v3(mem_ctx, string, sep));
+}