* Add ipkg for future development
[oweals/opkg-lede.git] / ipkg_utils.c
1 /* ipkg_utils.c - the itsy 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 "ipkg.h"
19 #include <errno.h>
20 #include <ctype.h>
21 #include <sys/vfs.h>
22
23 #include "ipkg_utils.h"
24 #include "pkg.h"
25 #include "pkg_hash.h"
26
27 void print_pkg_status(pkg_t * pkg, FILE * file);
28
29 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      return ((sfs.f_bavail * sfs.f_bsize) / 1024);
39 }
40
41 char **read_raw_pkgs_from_file(const char *file_name)
42 {
43      FILE *fp; 
44      char **ret;
45     
46      if(!(fp = fopen(file_name, "r"))){
47           fprintf(stderr, "can't get %s open for read\n", file_name);
48           return NULL;
49      }
50
51      ret = read_raw_pkgs_from_stream(fp);
52
53      fclose(fp);
54
55      return ret;
56 }
57
58 char **read_raw_pkgs_from_stream(FILE *fp)
59 {    
60      char **raw = NULL, *buf, *scout;
61      int count = 0;
62      size_t size = 512;
63      
64      buf = malloc (size);
65
66      while (fgets(buf, size, fp)) {
67           while (strlen (buf) == (size - 1)
68                  && buf[size-2] != '\n') {
69                size_t o = size - 1;
70                size *= 2;
71                buf = realloc (buf, size);
72                if (fgets (buf + o, size - o, fp) == NULL)
73                     break;
74           }
75           
76           if(!(count % 50))
77                raw = realloc(raw, (count + 50) * sizeof(char *));
78         
79           if((scout = strchr(buf, '\n')))
80                *scout = '\0';
81
82           raw[count++] = strdup(buf);
83      }
84     
85      raw = realloc(raw, (count + 1) * sizeof(char *));
86      raw[count] = NULL;
87
88      free (buf);
89     
90      return raw;
91 }
92
93 /* something to remove whitespace, a hash pooper */
94 char *trim_alloc(char *line)
95 {
96      char *new; 
97      char *dest, *src, *end;
98     
99      new = malloc(strlen(line) + 1);
100      if ( new == NULL ){
101         fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
102         return NULL;
103      }
104      dest = new, src = line, end = line + (strlen(line) - 1);
105
106      /* remove it from the front */    
107      while(src && 
108            isspace(*src) &&
109            *src)
110           src++;
111      /* and now from the back */
112      while((end > src) &&
113            isspace(*end))
114           end--;
115      end++;
116      *end = '\0';
117      strcpy(new, src);
118      /* this does from the first space
119       *  blasting away any versions stuff in depends
120       while(src && 
121       !isspace(*src) &&
122       *src)
123       *dest++ = *src++;
124       *dest = '\0';
125       */
126     
127      return new;
128 }
129
130 int line_is_blank(const char *line)
131 {
132      const char *s;
133
134      for (s = line; *s; s++) {
135           if (!isspace(*s))
136                return 0;
137      }
138      return 1;
139 }
140
141 void push_error_list(struct errlist ** errors, char * msg){
142   struct errlist *err_lst_tmp;
143
144
145   err_lst_tmp = malloc ( sizeof (err_lst_tmp) );
146   err_lst_tmp->errmsg=strdup(msg) ;
147   err_lst_tmp->next = *errors;
148   *errors = err_lst_tmp;
149 }
150
151
152 void reverse_error_list(struct errlist **errors){
153    struct errlist *result=NULL;
154    struct errlist *current= *errors;
155    struct errlist *next;
156
157    while ( current != NULL ) {
158       next = current->next;
159       current->next=result;
160       result=current;
161       current=next;
162    }
163    *errors=result;
164
165 }
166
167        
168 void free_error_list(){
169 struct errlist *err_tmp_lst;
170
171   err_tmp_lst = error_list;
172
173     while (err_tmp_lst != NULL) {
174       free(err_tmp_lst->errmsg);
175       err_tmp_lst = error_list->next;
176       free(error_list);
177       error_list = err_tmp_lst;
178     }
179
180
181 }
182
183