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++) {
150 * XXX: this function should not allocate memory as it may be called to
151 * print an error because we are out of memory.
153 void push_error_list(struct errlist ** errors, char * msg){
154 struct errlist *err_lst_tmp;
156 err_lst_tmp = calloc (1, sizeof (struct errlist) );
157 if (err_lst_tmp == NULL) {
158 fprintf(stderr, "%s: calloc: %s\n", __FUNCTION__, strerror(errno));
162 err_lst_tmp->errmsg = strdup(msg);
163 if (err_lst_tmp->errmsg == NULL) {
164 fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno));
169 err_lst_tmp->next = *errors;
170 *errors = err_lst_tmp;
174 void reverse_error_list(struct errlist **errors){
175 struct errlist *result=NULL;
176 struct errlist *current= *errors;
177 struct errlist *next;
179 while ( current != NULL ) {
180 next = current->next;
181 current->next=result;
190 void free_error_list(struct errlist **errors){
191 struct errlist *err_tmp_lst;
193 err_tmp_lst = *errors;
195 while (err_tmp_lst != NULL) {
196 free(err_tmp_lst->errmsg);
197 err_tmp_lst = error_list->next;
199 *errors = err_tmp_lst;