move str_list_make_v3 out of s3 code
[sfrench/samba-autobuild/.git] / lib / util / util_strlist.c
index 8397589d747282e3f0b3bd4ea37190aec883c2f0..d542e6f74f5eba2d3cdb07e94bf227fbd1363c3b 100644 (file)
@@ -522,3 +522,69 @@ _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;
+}