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 <ctype.h>
19 #include <sys/statvfs.h>
20 
21 #include "libbb/libbb.h"
22 #include "opkg_utils.h"
23 
get_available_kbytes(char * filesystem)24 unsigned long get_available_kbytes(char *filesystem)
25 {
26 	struct statvfs f;
27 
28 	if (statvfs(filesystem, &f) == -1) {
29 		opkg_perror(ERROR, "Failed to statvfs for %s", filesystem);
30 		return 0;
31 	}
32 	// Actually ((sfs.f_bavail * sfs.f_frsize) / 1024)
33 	// and here we try to avoid overflow.
34 	if (f.f_frsize >= 1024)
35 		return (f.f_bavail * (f.f_frsize / 1024));
36 	else if (f.f_frsize > 0)
37 		return f.f_bavail / (1024 / f.f_frsize);
38 
39 	opkg_msg(ERROR, "Unknown block size for target filesystem.\n");
40 
41 	return 0;
42 }
43 
44 /* something to remove whitespace, a hash pooper */
trim_xstrdup(const char * src)45 char *trim_xstrdup(const char *src)
46 {
47 	const char *end;
48 
49 	/* remove it from the front */
50 	while (src && isspace(*src) && *src)
51 		src++;
52 
53 	end = src + (strlen(src) - 1);
54 
55 	/* and now from the back */
56 	while ((end > src) && isspace(*end))
57 		end--;
58 
59 	end++;
60 
61 	/* xstrndup will NULL terminate for us */
62 	return xstrndup(src, end - src);
63 }
64 
line_is_blank(const char * line)65 int line_is_blank(const char *line)
66 {
67 	const char *s;
68 
69 	for (s = line; *s; s++) {
70 		if (!isspace(*s))
71 			return 0;
72 	}
73 	return 1;
74 }
75