diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-09 09:50:33 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-09 09:50:33 +0000 |
commit | f62ab2d77455ca42e1300e72b70d06e8a16db53b (patch) | |
tree | 7a5391066d647ecec698214773eee8504c7a041a | |
parent | dbef1173b00f0877a63121c40bf607155ac1e9a7 (diff) | |
download | busybox-w32-f62ab2d77455ca42e1300e72b70d06e8a16db53b.tar.gz busybox-w32-f62ab2d77455ca42e1300e72b70d06e8a16db53b.tar.bz2 busybox-w32-f62ab2d77455ca42e1300e72b70d06e8a16db53b.zip |
libbb: use improved xmalloc_read() from modprobe-small
who: fix compile breakage on some systems
modprobe-small: improve Config help text wording
-rw-r--r-- | coreutils/who.c | 6 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/read.c | 132 | ||||
-rw-r--r-- | modutils/Config.in | 2 | ||||
-rw-r--r-- | modutils/modprobe-small.c | 48 |
5 files changed, 63 insertions, 126 deletions
diff --git a/coreutils/who.c b/coreutils/who.c index a4ec740f0..baf526b01 100644 --- a/coreutils/who.c +++ b/coreutils/who.c | |||
@@ -56,16 +56,20 @@ int who_main(int argc UNUSED_PARAM, char **argv) | |||
56 | printf("USER TTY IDLE TIME HOST\n"); | 56 | printf("USER TTY IDLE TIME HOST\n"); |
57 | while ((ut = getutent()) != NULL) { | 57 | while ((ut = getutent()) != NULL) { |
58 | if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) { | 58 | if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) { |
59 | time_t tmp; | ||
59 | /* ut->ut_line is device name of tty - "/dev/" */ | 60 | /* ut->ut_line is device name of tty - "/dev/" */ |
60 | name = concat_path_file("/dev", ut->ut_line); | 61 | name = concat_path_file("/dev", ut->ut_line); |
61 | str6[0] = '?'; | 62 | str6[0] = '?'; |
62 | str6[1] = '\0'; | 63 | str6[1] = '\0'; |
63 | if (stat(name, &st) == 0) | 64 | if (stat(name, &st) == 0) |
64 | idle_string(str6, st.st_atime); | 65 | idle_string(str6, st.st_atime); |
66 | /* manpages say ut_tv.tv_sec *is* time_t, | ||
67 | * but some systems have it wrong */ | ||
68 | tmp = ut->ut_tv.tv_sec; | ||
65 | /* 15 chars for time: Nov 10 19:33:20 */ | 69 | /* 15 chars for time: Nov 10 19:33:20 */ |
66 | printf("%-10s %-8s %-9s %-15.15s %s\n", | 70 | printf("%-10s %-8s %-9s %-15.15s %s\n", |
67 | ut->ut_user, ut->ut_line, str6, | 71 | ut->ut_user, ut->ut_line, str6, |
68 | ctime(&(ut->ut_tv.tv_sec)) + 4, ut->ut_host); | 72 | ctime(&tmp) + 4, ut->ut_host); |
69 | if (ENABLE_FEATURE_CLEAN_UP) | 73 | if (ENABLE_FEATURE_CLEAN_UP) |
70 | free(name); | 74 | free(name); |
71 | } | 75 | } |
diff --git a/include/libbb.h b/include/libbb.h index fc65d52ff..2dfdded5b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -589,6 +589,7 @@ extern char *reads(int fd, char *buf, size_t count) FAST_FUNC; | |||
589 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; | 589 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; |
590 | extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; | 590 | extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; |
591 | extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; | 591 | extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; |
592 | extern void *xmalloc_read(int fd, size_t *sizep) FAST_FUNC; | ||
592 | /* Returns NULL if file can't be opened */ | 593 | /* Returns NULL if file can't be opened */ |
593 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; | 594 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; |
594 | /* Never returns NULL */ | 595 | /* Never returns NULL */ |
diff --git a/libbb/read.c b/libbb/read.c index 405e216dc..e67bbfb7e 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
@@ -106,7 +106,7 @@ ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) | |||
106 | return total; | 106 | return total; |
107 | } | 107 | } |
108 | 108 | ||
109 | // Die with an error message if we can't read the entire buffer. | 109 | /* Die with an error message if we can't read the entire buffer. */ |
110 | void FAST_FUNC xread(int fd, void *buf, size_t count) | 110 | void FAST_FUNC xread(int fd, void *buf, size_t count) |
111 | { | 111 | { |
112 | if (count) { | 112 | if (count) { |
@@ -116,7 +116,7 @@ void FAST_FUNC xread(int fd, void *buf, size_t count) | |||
116 | } | 116 | } |
117 | } | 117 | } |
118 | 118 | ||
119 | // Die with an error message if we can't read one character. | 119 | /* Die with an error message if we can't read one character. */ |
120 | unsigned char FAST_FUNC xread_char(int fd) | 120 | unsigned char FAST_FUNC xread_char(int fd) |
121 | { | 121 | { |
122 | char tmp; | 122 | char tmp; |
@@ -124,7 +124,7 @@ unsigned char FAST_FUNC xread_char(int fd) | |||
124 | return tmp; | 124 | return tmp; |
125 | } | 125 | } |
126 | 126 | ||
127 | // Read one line a-la fgets. Works only on seekable streams | 127 | /* Read one line a-la fgets. Works only on seekable streams */ |
128 | char* FAST_FUNC reads(int fd, char *buffer, size_t size) | 128 | char* FAST_FUNC reads(int fd, char *buffer, size_t size) |
129 | { | 129 | { |
130 | char *p; | 130 | char *p; |
@@ -140,9 +140,9 @@ char* FAST_FUNC reads(int fd, char *buffer, size_t size) | |||
140 | if (p) { | 140 | if (p) { |
141 | off_t offset; | 141 | off_t offset; |
142 | *p++ = '\0'; | 142 | *p++ = '\0'; |
143 | // avoid incorrect (unsigned) widening | 143 | /* avoid incorrect (unsigned) widening */ |
144 | offset = (off_t)(p - buffer) - (off_t)size; | 144 | offset = (off_t)(p - buffer) - (off_t)size; |
145 | // set fd position right after '\n' | 145 | /* set fd position right after '\n' */ |
146 | if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) | 146 | if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) |
147 | return NULL; | 147 | return NULL; |
148 | } | 148 | } |
@@ -203,39 +203,52 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) | |||
203 | return read_close(fd, buf, size); | 203 | return read_close(fd, buf, size); |
204 | } | 204 | } |
205 | 205 | ||
206 | |||
206 | // Read (potentially big) files in one go. File size is estimated | 207 | // Read (potentially big) files in one go. File size is estimated |
207 | // by stat. | 208 | // by stat. Extra '\0' byte is appended. |
208 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | 209 | void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) |
209 | { | 210 | { |
210 | char *buf; | 211 | char *buf; |
211 | size_t size; | 212 | size_t size, rd_size, total; |
212 | int fd; | 213 | off_t to_read; |
213 | off_t len; | ||
214 | struct stat st; | 214 | struct stat st; |
215 | 215 | ||
216 | fd = open(filename, O_RDONLY); | 216 | to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */ |
217 | if (fd < 0) | ||
218 | return NULL; | ||
219 | 217 | ||
220 | st.st_size = 0; /* in case fstat fail, define to 0 */ | 218 | /* Estimate file size */ |
219 | st.st_size = 0; /* in case fstat fails, assume 0 */ | ||
221 | fstat(fd, &st); | 220 | fstat(fd, &st); |
222 | /* /proc/N/stat files report len 0 here */ | 221 | /* /proc/N/stat files report st_size 0 */ |
223 | /* In order to make such files readable, we add small const */ | 222 | /* In order to make such files readable, we add small const */ |
224 | len = st.st_size | 0x3ff; /* read only 1k on unseekable files */ | 223 | size = (st.st_size | 0x3ff) + 1; |
225 | size = sizep ? *sizep : INT_MAX; | 224 | |
226 | if (len < size) | 225 | total = 0; |
227 | size = len; | 226 | buf = NULL; |
228 | buf = xmalloc(size + 1); | 227 | while (1) { |
229 | size = read_close(fd, buf, size); | 228 | if (to_read < size) |
230 | if ((ssize_t)size < 0) { | 229 | size = to_read; |
231 | free(buf); | 230 | buf = xrealloc(buf, total + size + 1); |
232 | return NULL; | 231 | rd_size = full_read(fd, buf + total, size); |
232 | if ((ssize_t)rd_size < 0) { /* error */ | ||
233 | free(buf); | ||
234 | return NULL; | ||
235 | } | ||
236 | total += rd_size; | ||
237 | if (rd_size < size) /* EOF */ | ||
238 | break; | ||
239 | to_read -= rd_size; | ||
240 | if (to_read <= 0) | ||
241 | break; | ||
242 | /* grow by 1/8, but in [1k..64k] bounds */ | ||
243 | size = ((total / 8) | 0x3ff) + 1; | ||
244 | if (size > 64*1024) | ||
245 | size = 64*1024; | ||
233 | } | 246 | } |
234 | xrealloc(buf, size + 1); | 247 | xrealloc(buf, total + 1); |
235 | buf[size] = '\0'; | 248 | buf[total] = '\0'; |
236 | 249 | ||
237 | if (sizep) | 250 | if (sizep) |
238 | *sizep = size; | 251 | *sizep = total; |
239 | return buf; | 252 | return buf; |
240 | } | 253 | } |
241 | 254 | ||
@@ -284,59 +297,26 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
284 | } | 297 | } |
285 | #endif | 298 | #endif |
286 | 299 | ||
287 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep) | 300 | // Read (potentially big) files in one go. File size is estimated |
288 | { | 301 | // by stat. |
289 | void *buf = xmalloc_open_read_close(filename, sizep); | 302 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) |
290 | if (!buf) | ||
291 | bb_perror_msg_and_die("can't read '%s'", filename); | ||
292 | return buf; | ||
293 | } | ||
294 | |||
295 | /* libbb candidate */ | ||
296 | #if 0 | ||
297 | static void *xmalloc_read(int fd, size_t *sizep) | ||
298 | { | 303 | { |
299 | char *buf; | 304 | char *buf; |
300 | size_t size, rd_size, total; | 305 | int fd; |
301 | off_t to_read; | ||
302 | struct stat st; | ||
303 | |||
304 | to_read = sizep ? *sizep : INT_MAX; /* max to read */ | ||
305 | 306 | ||
306 | /* Estimate file size */ | 307 | fd = open(filename, O_RDONLY); |
307 | st.st_size = 0; /* in case fstat fails, assume 0 */ | 308 | if (fd < 0) |
308 | fstat(fd, &st); | 309 | return NULL; |
309 | /* /proc/N/stat files report st_size 0 */ | ||
310 | /* In order to make such files readable, we add small const */ | ||
311 | size = (st.st_size | 0x3ff) + 1; | ||
312 | 310 | ||
313 | total = 0; | 311 | buf = xmalloc_read(fd, sizep); |
314 | buf = NULL; | 312 | close(fd); |
315 | while (1) { | 313 | return buf; |
316 | if (to_read < size) | 314 | } |
317 | size = to_read; | ||
318 | buf = xrealloc(buf, total + size + 1); | ||
319 | rd_size = full_read(fd, buf + total, size); | ||
320 | if ((ssize_t)rd_size < 0) { /* error */ | ||
321 | free(buf); | ||
322 | return NULL; | ||
323 | } | ||
324 | total += rd_size; | ||
325 | if (rd_size < size) /* EOF */ | ||
326 | break; | ||
327 | to_read -= rd_size; | ||
328 | if (to_read <= 0) | ||
329 | break; | ||
330 | /* grow by 1/8, but in [1k..64k] bounds */ | ||
331 | size = ((total / 8) | 0x3ff) + 1; | ||
332 | if (size > 64*1024) | ||
333 | size = 64*1024; | ||
334 | } | ||
335 | xrealloc(buf, total + 1); | ||
336 | buf[total] = '\0'; | ||
337 | 315 | ||
338 | if (sizep) | 316 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep) |
339 | *sizep = total; | 317 | { |
318 | void *buf = xmalloc_open_read_close(filename, sizep); | ||
319 | if (!buf) | ||
320 | bb_perror_msg_and_die("can't read '%s'", filename); | ||
340 | return buf; | 321 | return buf; |
341 | } | 322 | } |
342 | #endif | ||
diff --git a/modutils/Config.in b/modutils/Config.in index 25841b8ff..2e7f9b6e5 100644 --- a/modutils/Config.in +++ b/modutils/Config.in | |||
@@ -31,7 +31,7 @@ config MODPROBE_SMALL | |||
31 | than "non-small" modutils. | 31 | than "non-small" modutils. |
32 | 32 | ||
33 | config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE | 33 | config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
34 | bool "module options on cmdline" | 34 | bool "Accept module options on modprobe command line" |
35 | default n | 35 | default n |
36 | depends on MODPROBE_SMALL | 36 | depends on MODPROBE_SMALL |
37 | help | 37 | help |
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c index 4f073536a..1096ba7ae 100644 --- a/modutils/modprobe-small.c +++ b/modutils/modprobe-small.c | |||
@@ -14,54 +14,6 @@ | |||
14 | #include <sys/utsname.h> /* uname() */ | 14 | #include <sys/utsname.h> /* uname() */ |
15 | #include <fnmatch.h> | 15 | #include <fnmatch.h> |
16 | 16 | ||
17 | /* libbb candidate */ | ||
18 | static void *xmalloc_read(int fd, size_t *sizep) | ||
19 | { | ||
20 | char *buf; | ||
21 | size_t size, rd_size, total; | ||
22 | off_t to_read; | ||
23 | struct stat st; | ||
24 | |||
25 | to_read = sizep ? *sizep : INT_MAX; /* max to read */ | ||
26 | |||
27 | /* Estimate file size */ | ||
28 | st.st_size = 0; /* in case fstat fails, assume 0 */ | ||
29 | fstat(fd, &st); | ||
30 | /* /proc/N/stat files report st_size 0 */ | ||
31 | /* In order to make such files readable, we add small const */ | ||
32 | size = (st.st_size | 0x3ff) + 1; | ||
33 | |||
34 | total = 0; | ||
35 | buf = NULL; | ||
36 | while (1) { | ||
37 | if (to_read < size) | ||
38 | size = to_read; | ||
39 | buf = xrealloc(buf, total + size + 1); | ||
40 | rd_size = full_read(fd, buf + total, size); | ||
41 | if ((ssize_t)rd_size < 0) { /* error */ | ||
42 | free(buf); | ||
43 | return NULL; | ||
44 | } | ||
45 | total += rd_size; | ||
46 | if (rd_size < size) /* EOF */ | ||
47 | break; | ||
48 | to_read -= rd_size; | ||
49 | if (to_read <= 0) | ||
50 | break; | ||
51 | /* grow by 1/8, but in [1k..64k] bounds */ | ||
52 | size = ((total / 8) | 0x3ff) + 1; | ||
53 | if (size > 64*1024) | ||
54 | size = 64*1024; | ||
55 | } | ||
56 | xrealloc(buf, total + 1); | ||
57 | buf[total] = '\0'; | ||
58 | |||
59 | if (sizep) | ||
60 | *sizep = total; | ||
61 | return buf; | ||
62 | } | ||
63 | |||
64 | |||
65 | #define dbg1_error_msg(...) ((void)0) | 17 | #define dbg1_error_msg(...) ((void)0) |
66 | #define dbg2_error_msg(...) ((void)0) | 18 | #define dbg2_error_msg(...) ((void)0) |
67 | //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__) | 19 | //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__) |