Cleanup trim_alloc().
[oweals/opkg-lede.git] / libopkg / opkg_utils.c
index 92291c45eed69c9033cceb478ea7434c651fd229..e293197fa056d9963a81d54de3a367a61ec4f59f 100644 (file)
@@ -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);
@@ -99,40 +100,27 @@ char **read_raw_pkgs_from_stream(FILE *fp)
 }
 
 /* something to remove whitespace, a hash pooper */
-char *trim_alloc(char *line)
+char *trim_alloc(const char *src)
 {
-     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;
-     }
-     dest = new, src = line, end = line + (strlen(line) - 1);
+     const char *end;
 
      /* remove it from the front */    
      while(src && 
           isspace(*src) &&
           *src)
          src++;
+
+     end = src + (strlen(src) - 1);
+
      /* and now from the back */
      while((end > src) &&
           isspace(*end))
          end--;
+
      end++;
-     *end = '\0';
-     strcpy(new, src);
-     /* this does from the first space
-      *  blasting away any versions stuff in depends
-      while(src && 
-      !isspace(*src) &&
-      *src)
-      *dest++ = *src++;
-      *dest = '\0';
-      */
-    
-     return new;
+
+     /* xstrndup will NULL terminate for us */
+     return xstrndup(src, end-src);
 }
 
 int line_is_blank(const char *line)
@@ -156,21 +144,8 @@ void push_error_list(char * msg)
 {
        struct errlist *e;
 
-       e = calloc(1,  sizeof(struct errlist));
-       if (e == NULL) {
-               fprintf(stderr, "%s: calloc: %s\n",
-                               __FUNCTION__, strerror(errno));
-               return;
-       }
-
-       e->errmsg = strdup(msg);
-       if (e->errmsg == NULL) {
-               fprintf(stderr, "%s: strdup: %s\n",
-                               __FUNCTION__, strerror(errno));
-               free(e);
-               return;
-       }
-
+       e = xcalloc(1,  sizeof(struct errlist));
+       e->errmsg = xstrdup(msg);
        e->next = NULL;
 
        if (error_list_head) {