Use this new data structure in the utility function.

Signed-off-by: Simon Glass <s...@chromium.org>
---

(no changes since v3)

Changes in v3:
- Update for alist_add() API change

 lib/strto.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/lib/strto.c b/lib/strto.c
index f83ac67c666..277e83b20fd 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -9,6 +9,7 @@
  * Wirzenius wrote this portably, Torvalds fucked it up :-)
  */
 
+#include <alist.h>
 #include <errno.h>
 #include <malloc.h>
 #include <vsprintf.h>
@@ -226,37 +227,39 @@ void str_to_upper(const char *in, char *out, size_t len)
 
 const char **str_to_list(const char *instr)
 {
-       const char **ptr;
-       char *str, *p;
-       int count, i;
+       struct alist alist;
+       char *str, *p, *start;
 
        /* don't allocate if the string is empty */
        str = *instr ? strdup(instr) : (char *)instr;
        if (!str)
                return NULL;
 
-       /* count the number of space-separated strings */
-       for (count = 0, p = str; *p; p++) {
+       alist_init_struct(&alist, char *);
+
+       if (*str)
+               alist_add(&alist, str, char *);
+       for (start = str, p = str; *p; p++) {
                if (*p == ' ') {
-                       count++;
                        *p = '\0';
+                       start = p + 1;
+                       if (*start)
+                               alist_add(&alist, start, char *);
                }
        }
-       if (p != str && p[-1])
-               count++;
 
-       /* allocate the pointer array, allowing for a NULL terminator */
-       ptr = calloc(count + 1, sizeof(char *));
-       if (!ptr) {
-               if (*str)
+       /* terminate list */
+       p = NULL;
+       alist_add(&alist, p, char *);
+       if (alist_err(&alist)) {
+               alist_uninit(&alist);
+
+               if (*instr)
                        free(str);
                return NULL;
        }
 
-       for (i = 0, p = str; i < count; p += strlen(p) + 1, i++)
-               ptr[i] = p;
-
-       return ptr;
+       return alist_uninit_move(&alist, NULL, const char *);
 }
 
 void str_free_list(const char **ptr)
-- 
2.34.1

Reply via email to