From 5d8f9c016f67c98899376510ff37d07253d1ae4c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 11 Jan 2025 12:43:00 +0000 Subject: [PATCH] parser: Use string functions to manage filelists Signed-off-by: Michael Tremer --- src/pakfire/parser.c | 45 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/src/pakfire/parser.c b/src/pakfire/parser.c index 70f934de5..b051c3e65 100644 --- a/src/pakfire/parser.c +++ b/src/pakfire/parser.c @@ -765,34 +765,9 @@ char* pakfire_parser_get(struct pakfire_parser* parser, const char* namespace, c return pakfire_parser_expand(parser, namespace, value); } -static int append_to_array(char*** array, const char* s) { - unsigned int length = 0; - - // Determine the length of the existing array - if (*array) { - for (char** element = *array; *element; element++) - length++; - } - - // Allocate space - *array = reallocarray(*array, length + 2, sizeof(**array)); - if (!*array) - return 1; - - // Copy the string to the heap - char* p = strdup(s); - if (!p) - return 1; - - // Append p and terminate the array - (*array)[length] = p; - (*array)[length + 1] = NULL; - - return 0; -} - int pakfire_parser_get_filelist(struct pakfire_parser* parser, const char* namespace, const char* name, char*** includes, char*** excludes) { + const char* file = NULL; char* p = NULL; int r = 0; @@ -801,19 +776,23 @@ int pakfire_parser_get_filelist(struct pakfire_parser* parser, const char* names // Nothing to do for empty lists if (!list) - return 0; + goto ERROR; - const char* file = strtok_r(list, " \n", &p); + // Split the string + file = strtok_r(list, " \n", &p); // Split into includes and excludes while (file) { + // Excludes if (excludes && *file == '!') { - r = append_to_array(excludes, file + 1); - if (r) + r = pakfire_strings_append(excludes, file + 1); + if (r < 0) goto ERROR; - } else { - r = append_to_array(includes, file); - if (r) + + // Includes + } else if (includes) { + r = pakfire_strings_append(includes, file); + if (r < 0) goto ERROR; } -- 2.47.3