1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
21/*
* Guillaume Cottenceau (gc@mandrakesoft.com)
*
* Copyright 2000 Mandrakesoft
*
* This software may be freely redistributed under the terms of the GNU
* public license.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/utsname.h>
#include "utils.h"
#include "log.h"
// warning, many things rely on the fact that:
// - when failing it returns 0
// - it stops on first non-digit char
int charstar_to_int(const char * s)
{
int number = 0;
while (*s && isdigit(*s)) {
number = (number * 10) + (*s - '0');
s++;
}
return number;
}
off_t file_size(const char * path)
{
struct stat statr;
if (stat(path, &statr))
return -1;
else
return statr.st_size;
}
int total_memory(void)
{
int value;
/* drakx powered: use /proc/kcore and rounds every 4 Mbytes */
value = 4 * ((int)((float)file_size("/proc/kcore") / 1024 / 1024 / 4 + 0.5));
log_message("Total Memory: %d Mbytes", value);
return value;
}
/* pixel's */
void * memdup(void *src, size_t size)
{
void * r;
r = malloc(size);
memcpy(r, src, size);
return r;
}
void add_to_env(char * name, char * value)
{
FILE* fakeenv = fopen("/tmp/env", "a");
if (fakeenv) {
char* e = asprintf_("%s=%s\n", name, value);
fwrite(e, 1, strlen(e), fakeenv);
free(e);
fclose(fakeenv);
} else
log_message("couldn't fopen to fake env");
}
char ** list_directory(char * direct)
{
char * tmp[50000]; /* in /dev there can be many many files.. */
int i = 0;
struct dirent *ep;
DIR *dp = opendir(direct);
while (dp && (ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
tmp[i] = strdup(ep->d_name);
i++;
}
}
if (dp)
closedir(dp);
tmp[i] = NULL;
return memdup(tmp, sizeof(char*) * (i+1));
}
int string_array_length(char ** a)
{
int i = 0;
if (!a)
return -1;
while (a && *a) {
a++;
i++;
}
return i;
}
int kernel_version(void)
{
struct utsname val;
if (uname(&val)) {
log_perror("uname failed");
return -1;
}
return charstar_to_int(val.release + 2);
}
char * asprintf_(const char *msg, ...)
{
int n;
char * s;
char dummy;
va_list arg_ptr;
va_start(arg_ptr, msg);
n = vsnprintf(&dummy, sizeof(dummy), msg, arg_ptr);
va_start(arg_ptr, msg);
if ((s = malloc(n + 1))) {
vsnprintf(s, n + 1, msg, arg_ptr);
va_end(arg_ptr);
return s;
}
va_end(arg_ptr);
return strdup("");
}
int scall_(int retval, char * msg, char * file, int line)
{
char tmp[5000];
sprintf(tmp, "%s(%s:%d) failed", msg, file, line);
if (retval)
log_perror(tmp);
return retval;
}
void lowercase(char *s)
{
int i = 0;
while (s[i]) {
s[i] = tolower(s[i]);
i++;
}
}
|