Merge commit 'grg' into HEAD
[oweals/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/statvfs.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 unsigned long
31 get_available_kbytes(char * filesystem)
32 {
33     struct statvfs f;
34
35     if (statvfs(filesystem, &f) == -1) {
36         opkg_perror(ERROR, "Failed to statvfs for %s", filesystem);
37         return 0;
38     }
39
40     // Actually ((sfs.f_bavail * sfs.f_frsize) / 1024) 
41     // and here we try to avoid overflow. 
42     if (f.f_frsize >= 1024) 
43         return (f.f_bavail * (f.f_frsize / 1024));
44     else if (f.f_frsize > 0)
45         return f.f_bavail / (1024 / f.f_frsize);
46
47     opkg_msg(ERROR, "Unknown block size for target filesystem.\n");
48
49     return 0;
50 }
51
52 /* something to remove whitespace, a hash pooper */
53 char *trim_xstrdup(const char *src)
54 {
55      const char *end;
56
57      /* remove it from the front */    
58      while(src && 
59            isspace(*src) &&
60            *src)
61           src++;
62
63      end = src + (strlen(src) - 1);
64
65      /* and now from the back */
66      while((end > src) &&
67            isspace(*end))
68           end--;
69
70      end++;
71
72      /* xstrndup will NULL terminate for us */
73      return xstrndup(src, end-src);
74 }
75
76 int line_is_blank(const char *line)
77 {
78      const char *s;
79
80      for (s = line; *s; s++) {
81           if (!isspace(*s))
82                return 0;
83      }
84      return 1;
85 }