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"
27 void print_pkg_status(pkg_t
* pkg
, FILE * file
);
29 long unsigned int get_available_blocks(char * filesystem
)
33 if(statfs(filesystem
, &sfs
)){
34 fprintf(stderr
, "bad statfs\n");
37 /* fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
39 // Actually ((sfs.f_bavail * sfs.f_bsize) / 1024)
40 // and here we try to avoid overflow.
41 if (sfs
.f_bsize
>= 1024)
42 return (sfs
.f_bavail
* (sfs
.f_bsize
/ 1024));
43 else if (sfs
.f_bsize
> 0)
44 return sfs
.f_bavail
/ (1024 / sfs
.f_bsize
);
45 fprintf(stderr
, "bad statfs f_bsize == 0\n");
49 char **read_raw_pkgs_from_file(const char *file_name
)
54 if(!(fp
= fopen(file_name
, "r"))){
55 fprintf(stderr
, "can't get %s open for read\n", file_name
);
59 ret
= read_raw_pkgs_from_stream(fp
);
66 char **read_raw_pkgs_from_stream(FILE *fp
)
68 char **raw
= NULL
, *buf
, *scout
;
72 buf
= calloc (1, size
);
74 while (fgets(buf
, size
, fp
)) {
75 while (strlen (buf
) == (size
- 1)
76 && buf
[size
-2] != '\n') {
79 buf
= realloc (buf
, size
);
80 if (fgets (buf
+ o
, size
- o
, fp
) == NULL
)
85 raw
= realloc(raw
, (count
+ 50) * sizeof(char *));
87 if((scout
= strchr(buf
, '\n')))
90 raw
[count
++] = strdup(buf
);
93 raw
= realloc(raw
, (count
+ 1) * sizeof(char *));
101 /* something to remove whitespace, a hash pooper */
102 char *trim_alloc(char *line
)
105 char *dest
, *src
, *end
;
107 new = calloc(1, strlen(line
) + 1);
109 fprintf(stderr
,"%s: Unable to allocate memory\n",__FUNCTION__
);
112 dest
= new, src
= line
, end
= line
+ (strlen(line
) - 1);
114 /* remove it from the front */
119 /* and now from the back */
126 /* this does from the first space
127 * blasting away any versions stuff in depends
138 int line_is_blank(const char *line
)
142 for (s
= line
; *s
; s
++) {
149 void push_error_list(struct errlist
** errors
, char * msg
){
150 struct errlist
*err_lst_tmp
;
153 err_lst_tmp
= calloc (1, sizeof (err_lst_tmp
) );
154 err_lst_tmp
->errmsg
=strdup(msg
) ;
155 err_lst_tmp
->next
= *errors
;
156 *errors
= err_lst_tmp
;
160 void reverse_error_list(struct errlist
**errors
){
161 struct errlist
*result
=NULL
;
162 struct errlist
*current
= *errors
;
163 struct errlist
*next
;
165 while ( current
!= NULL
) {
166 next
= current
->next
;
167 current
->next
=result
;
176 void free_error_list(){
177 struct errlist
*err_tmp_lst
;
179 err_tmp_lst
= error_list
;
181 while (err_tmp_lst
!= NULL
) {
182 free(err_tmp_lst
->errmsg
);
183 err_tmp_lst
= error_list
->next
;
185 error_list
= err_tmp_lst
;