Cleanup trim_alloc().
[oweals/opkg-lede.git] / libopkg / opkg_utils.c
index 0b596453d5859abffa5dc1ff1166e653e74f8073..e293197fa056d9963a81d54de3a367a61ec4f59f 100644 (file)
@@ -1,4 +1,4 @@
-/* opkg_utils.c - the itsy package management system
+/* opkg_utils.c - the opkg package management system
 
    Steven M. Ayer
    
@@ -15,7 +15,7 @@
    General Public License for more details.
 */
 
-#include "opkg.h"
+#include "includes.h"
 #include <errno.h>
 #include <ctype.h>
 #include <sys/vfs.h>
 #include "opkg_utils.h"
 #include "pkg.h"
 #include "pkg_hash.h"
+#include "libbb/libbb.h"
 
 void print_pkg_status(pkg_t * pkg, FILE * file);
 
-int get_available_blocks(char * filesystem)
+long unsigned int get_available_blocks(char * filesystem)
 {
-     struct statfs sfs;
+    struct statfs sfs;
 
-     if(statfs(filesystem, &sfs)){
-         fprintf(stderr, "bad statfs\n");
-         return 0;
-     }
-     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
-     return ((sfs.f_bavail * sfs.f_bsize) / 1024);
+    if(statfs(filesystem, &sfs)){
+        fprintf(stderr, "bad statfs\n");
+        return 0;
+    }
+    /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
+
+    // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024) 
+    // and here we try to avoid overflow. 
+    if (sfs.f_bsize >= 1024) 
+        return (sfs.f_bavail * (sfs.f_bsize / 1024));
+    else if (sfs.f_bsize > 0)
+        return sfs.f_bavail / (1024 / sfs.f_bsize);
+    fprintf(stderr, "bad statfs f_bsize == 0\n");
+    return 0;
 }
 
 char **read_raw_pkgs_from_file(const char *file_name)
@@ -61,28 +70,28 @@ char **read_raw_pkgs_from_stream(FILE *fp)
      int count = 0;
      size_t size = 512;
      
-     buf = malloc (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);
@@ -91,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 = malloc(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)
@@ -138,46 +134,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 = malloc ( 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;
+               }
+       }
 }
-
-