s/malloc/xmalloc/ s/calloc/xcalloc/ s/realloc/realloc/
[project/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 #include "libbb/libbb.h"
27
28 void print_pkg_status(pkg_t * pkg, FILE * file);
29
30 long unsigned int get_available_blocks(char * filesystem)
31 {
32 struct statfs sfs;
33
34 if(statfs(filesystem, &sfs)){
35 fprintf(stderr, "bad statfs\n");
36 return 0;
37 }
38 /* fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
39
40 // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024)
41 // and here we try to avoid overflow.
42 if (sfs.f_bsize >= 1024)
43 return (sfs.f_bavail * (sfs.f_bsize / 1024));
44 else if (sfs.f_bsize > 0)
45 return sfs.f_bavail / (1024 / sfs.f_bsize);
46 fprintf(stderr, "bad statfs f_bsize == 0\n");
47 return 0;
48 }
49
50 char **read_raw_pkgs_from_file(const char *file_name)
51 {
52 FILE *fp;
53 char **ret;
54
55 if(!(fp = fopen(file_name, "r"))){
56 fprintf(stderr, "can't get %s open for read\n", file_name);
57 return NULL;
58 }
59
60 ret = read_raw_pkgs_from_stream(fp);
61
62 fclose(fp);
63
64 return ret;
65 }
66
67 char **read_raw_pkgs_from_stream(FILE *fp)
68 {
69 char **raw = NULL, *buf, *scout;
70 int count = 0;
71 size_t size = 512;
72
73 buf = xcalloc(1, size);
74
75 while (fgets(buf, size, fp)) {
76 while (strlen (buf) == (size - 1)
77 && buf[size-2] != '\n') {
78 size_t o = size - 1;
79 size *= 2;
80 buf = xrealloc (buf, size);
81 if (fgets (buf + o, size - o, fp) == NULL)
82 break;
83 }
84
85 if(!(count % 50))
86 raw = xrealloc(raw, (count + 50) * sizeof(char *));
87
88 if((scout = strchr(buf, '\n')))
89 *scout = '\0';
90
91 raw[count++] = xstrdup(buf);
92 }
93
94 raw = xrealloc(raw, (count + 1) * sizeof(char *));
95 raw[count] = NULL;
96
97 free (buf);
98
99 return raw;
100 }
101
102 /* something to remove whitespace, a hash pooper */
103 char *trim_alloc(char *line)
104 {
105 char *new;
106 char *dest, *src, *end;
107
108 new = xcalloc(1, strlen(line) + 1);
109 dest = new, src = line, end = line + (strlen(line) - 1);
110
111 /* remove it from the front */
112 while(src &&
113 isspace(*src) &&
114 *src)
115 src++;
116 /* and now from the back */
117 while((end > src) &&
118 isspace(*end))
119 end--;
120 end++;
121 *end = '\0';
122 strcpy(new, src);
123 /* this does from the first space
124 * blasting away any versions stuff in depends
125 while(src &&
126 !isspace(*src) &&
127 *src)
128 *dest++ = *src++;
129 *dest = '\0';
130 */
131
132 return new;
133 }
134
135 int line_is_blank(const char *line)
136 {
137 const char *s;
138
139 for (s = line; *s; s++) {
140 if (!isspace(*s))
141 return 0;
142 }
143 return 1;
144 }
145
146 static struct errlist *error_list_head, *error_list_tail;
147
148 /*
149 * XXX: this function should not allocate memory as it may be called to
150 * print an error because we are out of memory.
151 */
152 void push_error_list(char * msg)
153 {
154 struct errlist *e;
155
156 e = xcalloc(1, sizeof(struct errlist));
157 e->errmsg = xstrdup(msg);
158 e->next = NULL;
159
160 if (error_list_head) {
161 error_list_tail->next = e;
162 error_list_tail = e;
163 } else {
164 error_list_head = error_list_tail = e;
165 }
166 }
167
168 void free_error_list(void)
169 {
170 struct errlist *err, *err_tmp;
171
172 err = error_list_head;
173 while (err != NULL) {
174 free(err->errmsg);
175 err_tmp = err;
176 err = err->next;
177 free(err_tmp);
178 }
179 }
180
181 void print_error_list (void)
182 {
183 struct errlist *err = error_list_head;
184
185 if (err) {
186 printf ("Collected errors:\n");
187 /* Here we print the errors collected and free the list */
188 while (err != NULL) {
189 printf (" * %s", err->errmsg);
190 err = err->next;
191 }
192 }
193 }