Fix output of whatdepends, whatsuggests and whatrecommends commands.
[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 /* something to remove whitespace, a hash pooper */
51 char *trim_xstrdup(const char *src)
52 {
53 const char *end;
54
55 /* remove it from the front */
56 while(src &&
57 isspace(*src) &&
58 *src)
59 src++;
60
61 end = src + (strlen(src) - 1);
62
63 /* and now from the back */
64 while((end > src) &&
65 isspace(*end))
66 end--;
67
68 end++;
69
70 /* xstrndup will NULL terminate for us */
71 return xstrndup(src, end-src);
72 }
73
74 int line_is_blank(const char *line)
75 {
76 const char *s;
77
78 for (s = line; *s; s++) {
79 if (!isspace(*s))
80 return 0;
81 }
82 return 1;
83 }
84
85 static struct errlist *error_list_head, *error_list_tail;
86
87 /*
88 * XXX: this function should not allocate memory as it may be called to
89 * print an error because we are out of memory.
90 */
91 void push_error_list(char * msg)
92 {
93 struct errlist *e;
94
95 e = xcalloc(1, sizeof(struct errlist));
96 e->errmsg = xstrdup(msg);
97 e->next = NULL;
98
99 if (error_list_head) {
100 error_list_tail->next = e;
101 error_list_tail = e;
102 } else {
103 error_list_head = error_list_tail = e;
104 }
105 }
106
107 void free_error_list(void)
108 {
109 struct errlist *err, *err_tmp;
110
111 err = error_list_head;
112 while (err != NULL) {
113 free(err->errmsg);
114 err_tmp = err;
115 err = err->next;
116 free(err_tmp);
117 }
118 }
119
120 void print_error_list (void)
121 {
122 struct errlist *err = error_list_head;
123
124 if (err) {
125 printf ("Collected errors:\n");
126 /* Here we print the errors collected and free the list */
127 while (err != NULL) {
128 printf (" * %s", err->errmsg);
129 err = err->next;
130 }
131 }
132 }