Fix problems in error list
[oweals/opkg-lede.git] / libopkg / opkg_utils.c
1 /* opkg_utils.c - the opkg package management system
2
3    Steven M. Ayer
4    
5    Copyright (C) 2002 Compaq Computer Corporation
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <errno.h>
20 #include <ctype.h>
21 #include <sys/vfs.h>
22
23 #include "opkg_utils.h"
24 #include "pkg.h"
25 #include "pkg_hash.h"
26
27 void print_pkg_status(pkg_t * pkg, FILE * file);
28
29 long unsigned int get_available_blocks(char * filesystem)
30 {
31     struct statfs sfs;
32
33     if(statfs(filesystem, &sfs)){
34         fprintf(stderr, "bad statfs\n");
35         return 0;
36     }
37     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
38
39     // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024) 
40     // and here we try to avoid overflow. 
41     if (sfs.f_bsize >= 1024) 
42         return (sfs.f_bavail * (sfs.f_bsize / 1024));
43     else if (sfs.f_bsize > 0)
44         return sfs.f_bavail / (1024 / sfs.f_bsize);
45     fprintf(stderr, "bad statfs f_bsize == 0\n");
46     return 0;
47 }
48
49 char **read_raw_pkgs_from_file(const char *file_name)
50 {
51      FILE *fp; 
52      char **ret;
53     
54      if(!(fp = fopen(file_name, "r"))){
55           fprintf(stderr, "can't get %s open for read\n", file_name);
56           return NULL;
57      }
58
59      ret = read_raw_pkgs_from_stream(fp);
60
61      fclose(fp);
62
63      return ret;
64 }
65
66 char **read_raw_pkgs_from_stream(FILE *fp)
67 {    
68      char **raw = NULL, *buf, *scout;
69      int count = 0;
70      size_t size = 512;
71      
72      buf = calloc (1, size);
73
74      while (fgets(buf, size, fp)) {
75           while (strlen (buf) == (size - 1)
76                  && buf[size-2] != '\n') {
77                size_t o = size - 1;
78                size *= 2;
79                buf = realloc (buf, size);
80                if (fgets (buf + o, size - o, fp) == NULL)
81                     break;
82           }
83           
84           if(!(count % 50))
85                raw = realloc(raw, (count + 50) * sizeof(char *));
86         
87           if((scout = strchr(buf, '\n')))
88                *scout = '\0';
89
90           raw[count++] = strdup(buf);
91      }
92     
93      raw = realloc(raw, (count + 1) * sizeof(char *));
94      raw[count] = NULL;
95
96      free (buf);
97     
98      return raw;
99 }
100
101 /* something to remove whitespace, a hash pooper */
102 char *trim_alloc(char *line)
103 {
104      char *new; 
105      char *dest, *src, *end;
106     
107      new = calloc(1, strlen(line) + 1);
108      if ( new == NULL ){
109         fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
110         return NULL;
111      }
112      dest = new, src = line, end = line + (strlen(line) - 1);
113
114      /* remove it from the front */    
115      while(src && 
116            isspace(*src) &&
117            *src)
118           src++;
119      /* and now from the back */
120      while((end > src) &&
121            isspace(*end))
122           end--;
123      end++;
124      *end = '\0';
125      strcpy(new, src);
126      /* this does from the first space
127       *  blasting away any versions stuff in depends
128       while(src && 
129       !isspace(*src) &&
130       *src)
131       *dest++ = *src++;
132       *dest = '\0';
133       */
134     
135      return new;
136 }
137
138 int line_is_blank(const char *line)
139 {
140      const char *s;
141
142      for (s = line; *s; s++) {
143           if (!isspace(*s))
144                return 0;
145      }
146      return 1;
147 }
148
149 /*
150  * XXX: this function should not allocate memory as it may be called to
151  *      print an error because we are out of memory.
152  */
153 void push_error_list(struct errlist ** errors, char * msg){
154   struct errlist *err_lst_tmp;
155
156   err_lst_tmp = calloc (1,  sizeof (struct errlist) );
157   if (err_lst_tmp == NULL) {
158     fprintf(stderr, "%s: calloc: %s\n", __FUNCTION__, strerror(errno));
159     return;
160   }
161
162   err_lst_tmp->errmsg = strdup(msg);
163   if (err_lst_tmp->errmsg == NULL) {
164     fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno));
165     free(err_lst_tmp);
166     return;
167   }
168
169   err_lst_tmp->next = *errors;
170   *errors = err_lst_tmp;
171 }
172
173
174 void reverse_error_list(struct errlist **errors){
175    struct errlist *result=NULL;
176    struct errlist *current= *errors;
177    struct errlist *next;
178
179    while ( current != NULL ) {
180       next = current->next;
181       current->next=result;
182       result=current;
183       current=next;
184    }
185    *errors=result;
186
187 }
188
189        
190 void free_error_list(struct errlist **errors){
191 struct errlist *err_tmp_lst;
192
193   err_tmp_lst = *errors;
194
195     while (err_tmp_lst != NULL) {
196       free(err_tmp_lst->errmsg);
197       err_tmp_lst = error_list->next;
198       free(*errors);
199       *errors = err_tmp_lst;
200     }
201
202
203 }
204
205