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(const char *src)
107 /* remove it from the front */
113 end = src + (strlen(src) - 1);
115 /* and now from the back */
122 /* xstrndup will NULL terminate for us */
123 return xstrndup(src, end-src);
126 int line_is_blank(const char *line)
130 for (s = line; *s; s++) {
137 static struct errlist *error_list_head, *error_list_tail;
140 * XXX: this function should not allocate memory as it may be called to
141 * print an error because we are out of memory.
143 void push_error_list(char * msg)
147 e = xcalloc(1, sizeof(struct errlist));
148 e->errmsg = xstrdup(msg);
151 if (error_list_head) {
152 error_list_tail->next = e;
155 error_list_head = error_list_tail = e;
159 void free_error_list(void)
161 struct errlist *err, *err_tmp;
163 err = error_list_head;
164 while (err != NULL) {
172 void print_error_list (void)
174 struct errlist *err = error_list_head;
177 printf ("Collected errors:\n");
178 /* Here we print the errors collected and free the list */
179 while (err != NULL) {
180 printf (" * %s", err->errmsg);