1 /* opkg_utils.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
23 #include "opkg_utils.h"
26 #include "libbb/libbb.h"
28 void print_pkg_status(pkg_t * pkg, FILE * file);
30 long unsigned int get_available_blocks(char * filesystem)
34 if(statfs(filesystem, &sfs)){
35 fprintf(stderr, "bad statfs\n");
38 /* fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
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");
50 char **read_raw_pkgs_from_file(const char *file_name)
55 if(!(fp = fopen(file_name, "r"))){
56 fprintf(stderr, "can't get %s open for read\n", file_name);
60 ret = read_raw_pkgs_from_stream(fp);
67 char **read_raw_pkgs_from_stream(FILE *fp)
69 char **raw = NULL, *buf, *scout;
73 buf = xcalloc(1, size);
75 while (fgets(buf, size, fp)) {
76 while (strlen (buf) == (size - 1)
77 && buf[size-2] != '\n') {
80 buf = xrealloc (buf, size);
81 if (fgets (buf + o, size - o, fp) == NULL)
86 raw = xrealloc(raw, (count + 50) * sizeof(char *));
88 if((scout = strchr(buf, '\n')))
91 raw[count++] = xstrdup(buf);
94 raw = xrealloc(raw, (count + 1) * sizeof(char *));
102 /* something to remove whitespace, a hash pooper */
103 char *trim_alloc(char *line)
106 char *dest, *src, *end;
108 new = xcalloc(1, strlen(line) + 1);
109 dest = new, src = line, end = line + (strlen(line) - 1);
111 /* remove it from the front */
116 /* and now from the back */
123 /* this does from the first space
124 * blasting away any versions stuff in depends
135 int line_is_blank(const char *line)
139 for (s = line; *s; s++) {
146 static struct errlist *error_list_head, *error_list_tail;
149 * XXX: this function should not allocate memory as it may be called to
150 * print an error because we are out of memory.
152 void push_error_list(char * msg)
156 e = xcalloc(1, sizeof(struct errlist));
157 e->errmsg = xstrdup(msg);
160 if (error_list_head) {
161 error_list_tail->next = e;
164 error_list_head = error_list_tail = e;
168 void free_error_list(void)
170 struct errlist *err, *err_tmp;
172 err = error_list_head;
173 while (err != NULL) {
181 void print_error_list (void)
183 struct errlist *err = error_list_head;
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);