ccc3496666333caa414d96527c851e0dbfef8fb3
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
= calloc (1, size
);
75 while (fgets(buf
, size
, fp
)) {
76 while (strlen (buf
) == (size
- 1)
77 && buf
[size
-2] != '\n') {
80 buf
= realloc (buf
, size
);
81 if (fgets (buf
+ o
, size
- o
, fp
) == NULL
)
86 raw
= realloc(raw
, (count
+ 50) * sizeof(char *));
88 if((scout
= strchr(buf
, '\n')))
91 raw
[count
++] = xstrdup(buf
);
94 raw
= realloc(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 = calloc(1, strlen(line
) + 1);
110 fprintf(stderr
,"%s: Unable to allocate memory\n",__FUNCTION__
);
113 dest
= new, src
= line
, end
= line
+ (strlen(line
) - 1);
115 /* remove it from the front */
120 /* and now from the back */
127 /* this does from the first space
128 * blasting away any versions stuff in depends
139 int line_is_blank(const char *line
)
143 for (s
= line
; *s
; s
++) {
150 static struct errlist
*error_list_head
, *error_list_tail
;
153 * XXX: this function should not allocate memory as it may be called to
154 * print an error because we are out of memory.
156 void push_error_list(char * msg
)
160 e
= calloc(1, sizeof(struct errlist
));
162 fprintf(stderr
, "%s: calloc: %s\n",
163 __FUNCTION__
, strerror(errno
));
167 e
->errmsg
= xstrdup(msg
);
170 if (error_list_head
) {
171 error_list_tail
->next
= e
;
174 error_list_head
= error_list_tail
= e
;
178 void free_error_list(void)
180 struct errlist
*err
, *err_tmp
;
182 err
= error_list_head
;
183 while (err
!= NULL
) {
191 void print_error_list (void)
193 struct errlist
*err
= error_list_head
;
196 printf ("Collected errors:\n");
197 /* Here we print the errors collected and free the list */
198 while (err
!= NULL
) {
199 printf (" * %s", err
->errmsg
);