X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libopkg%2Fopkg_utils.c;h=20af4d5a73bf4776dfadadf8051b669d487727a7;hb=94b0a6bec6a0d3301e28385ee3fdf4c19f67b866;hp=fb27b4060751d8238faf01ba3ed07a05172ca8a7;hpb=33c7e93565efbb0c07d9aa0f8b72ca737111f338;p=oweals%2Fopkg-lede.git diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index fb27b40..20af4d5 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -23,6 +23,7 @@ #include "opkg_utils.h" #include "pkg.h" #include "pkg_hash.h" +#include "libbb/libbb.h" void print_pkg_status(pkg_t * pkg, FILE * file); @@ -69,28 +70,28 @@ char **read_raw_pkgs_from_stream(FILE *fp) int count = 0; size_t size = 512; - buf = calloc (1, size); + buf = xcalloc(1, size); while (fgets(buf, size, fp)) { while (strlen (buf) == (size - 1) && buf[size-2] != '\n') { size_t o = size - 1; size *= 2; - buf = realloc (buf, size); + buf = xrealloc (buf, size); if (fgets (buf + o, size - o, fp) == NULL) break; } if(!(count % 50)) - raw = realloc(raw, (count + 50) * sizeof(char *)); + raw = xrealloc(raw, (count + 50) * sizeof(char *)); if((scout = strchr(buf, '\n'))) *scout = '\0'; - raw[count++] = strdup(buf); + raw[count++] = xstrdup(buf); } - raw = realloc(raw, (count + 1) * sizeof(char *)); + raw = xrealloc(raw, (count + 1) * sizeof(char *)); raw[count] = NULL; free (buf); @@ -104,11 +105,7 @@ char *trim_alloc(char *line) char *new; char *dest, *src, *end; - new = calloc(1, strlen(line) + 1); - if ( new == NULL ){ - fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); - return NULL; - } + new = xcalloc(1, strlen(line) + 1); dest = new, src = line, end = line + (strlen(line) - 1); /* remove it from the front */ @@ -146,46 +143,51 @@ int line_is_blank(const char *line) return 1; } -void push_error_list(struct errlist ** errors, char * msg){ - struct errlist *err_lst_tmp; - +static struct errlist *error_list_head, *error_list_tail; - err_lst_tmp = calloc (1, sizeof (err_lst_tmp) ); - err_lst_tmp->errmsg=strdup(msg) ; - err_lst_tmp->next = *errors; - *errors = err_lst_tmp; +/* + * XXX: this function should not allocate memory as it may be called to + * print an error because we are out of memory. + */ +void push_error_list(char * msg) +{ + struct errlist *e; + + e = xcalloc(1, sizeof(struct errlist)); + e->errmsg = xstrdup(msg); + e->next = NULL; + + if (error_list_head) { + error_list_tail->next = e; + error_list_tail = e; + } else { + error_list_head = error_list_tail = e; + } } - -void reverse_error_list(struct errlist **errors){ - struct errlist *result=NULL; - struct errlist *current= *errors; - struct errlist *next; - - while ( current != NULL ) { - next = current->next; - current->next=result; - result=current; - current=next; - } - *errors=result; - +void free_error_list(void) +{ + struct errlist *err, *err_tmp; + + err = error_list_head; + while (err != NULL) { + free(err->errmsg); + err_tmp = err; + err = err->next; + free(err_tmp); + } } - -void free_error_list(){ -struct errlist *err_tmp_lst; - - err_tmp_lst = error_list; - - while (err_tmp_lst != NULL) { - free(err_tmp_lst->errmsg); - err_tmp_lst = error_list->next; - free(error_list); - error_list = err_tmp_lst; - } - - +void print_error_list (void) +{ + struct errlist *err = error_list_head; + + if (err) { + printf ("Collected errors:\n"); + /* Here we print the errors collected and free the list */ + while (err != NULL) { + printf (" * %s", err->errmsg); + err = err->next; + } + } } - -