diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-10 17:43:01 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-10 17:43:01 +0000 |
| commit | b6052724ffc9d45894147b70e7aff226938bd2d3 (patch) | |
| tree | e39d062e16d85da315cdb8cc0a96a03f15c10a23 /libbb | |
| parent | 0e2c93fc0b5f335d731ff712ce8f42d8616f05b9 (diff) | |
| download | busybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.tar.gz busybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.tar.bz2 busybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.zip | |
open_transformer: do not return fd, it does not change
libbb: adopt zipped read from modprobe-small
function old new delta
getoptscmd 708 713 +5
qgravechar 106 109 +3
huft_build 1165 1168 +3
tr_main 474 472 -2
open_transformer 91 89 -2
evalvar 1376 1374 -2
rpm_main 1691 1688 -3
qrealloc 36 33 -3
get_header_tar_lzma 55 52 -3
get_header_tar_gz 100 97 -3
get_header_tar_bz2 55 52 -3
get_header_tar_Z 89 86 -3
find_main 418 406 -12
prepare 302 283 -19
xmalloc_open_zipped_read_close 161 135 -26
xmalloc_read 248 199 -49
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/13 up/down: 11/-130) Total: -119 bytes
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/read.c | 62 |
1 files changed, 46 insertions, 16 deletions
diff --git a/libbb/read.c b/libbb/read.c index e67bbfb7e..7af895207 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
| @@ -8,6 +8,9 @@ | |||
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | #include "libbb.h" | 10 | #include "libbb.h" |
| 11 | #if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED | ||
| 12 | #include "unarchive.h" | ||
| 13 | #endif | ||
| 11 | 14 | ||
| 12 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) | 15 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
| 13 | { | 16 | { |
| @@ -206,14 +209,14 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) | |||
| 206 | 209 | ||
| 207 | // Read (potentially big) files in one go. File size is estimated | 210 | // Read (potentially big) files in one go. File size is estimated |
| 208 | // by stat. Extra '\0' byte is appended. | 211 | // by stat. Extra '\0' byte is appended. |
| 209 | void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) | 212 | void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) |
| 210 | { | 213 | { |
| 211 | char *buf; | 214 | char *buf; |
| 212 | size_t size, rd_size, total; | 215 | size_t size, rd_size, total; |
| 213 | off_t to_read; | 216 | size_t to_read; |
| 214 | struct stat st; | 217 | struct stat st; |
| 215 | 218 | ||
| 216 | to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */ | 219 | to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */ |
| 217 | 220 | ||
| 218 | /* Estimate file size */ | 221 | /* Estimate file size */ |
| 219 | st.st_size = 0; /* in case fstat fails, assume 0 */ | 222 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
| @@ -229,16 +232,16 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) | |||
| 229 | size = to_read; | 232 | size = to_read; |
| 230 | buf = xrealloc(buf, total + size + 1); | 233 | buf = xrealloc(buf, total + size + 1); |
| 231 | rd_size = full_read(fd, buf + total, size); | 234 | rd_size = full_read(fd, buf + total, size); |
| 232 | if ((ssize_t)rd_size < 0) { /* error */ | 235 | if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */ |
| 233 | free(buf); | 236 | free(buf); |
| 234 | return NULL; | 237 | return NULL; |
| 235 | } | 238 | } |
| 236 | total += rd_size; | 239 | total += rd_size; |
| 237 | if (rd_size < size) /* EOF */ | 240 | if (rd_size < size) /* EOF */ |
| 238 | break; | 241 | break; |
| 239 | to_read -= rd_size; | 242 | if (to_read <= rd_size) |
| 240 | if (to_read <= 0) | ||
| 241 | break; | 243 | break; |
| 244 | to_read -= rd_size; | ||
| 242 | /* grow by 1/8, but in [1k..64k] bounds */ | 245 | /* grow by 1/8, but in [1k..64k] bounds */ |
| 243 | size = ((total / 8) | 0x3ff) + 1; | 246 | size = ((total / 8) | 0x3ff) + 1; |
| 244 | if (size > 64*1024) | 247 | if (size > 64*1024) |
| @@ -247,8 +250,8 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) | |||
| 247 | xrealloc(buf, total + 1); | 250 | xrealloc(buf, total + 1); |
| 248 | buf[total] = '\0'; | 251 | buf[total] = '\0'; |
| 249 | 252 | ||
| 250 | if (sizep) | 253 | if (maxsz_p) |
| 251 | *sizep = total; | 254 | *maxsz_p = total; |
| 252 | return buf; | 255 | return buf; |
| 253 | } | 256 | } |
| 254 | 257 | ||
| @@ -260,7 +263,7 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) | |||
| 260 | 263 | ||
| 261 | // Read (potentially big) files in one go. File size is estimated by | 264 | // Read (potentially big) files in one go. File size is estimated by |
| 262 | // lseek to end. | 265 | // lseek to end. |
| 263 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | 266 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
| 264 | { | 267 | { |
| 265 | char *buf; | 268 | char *buf; |
| 266 | size_t size; | 269 | size_t size; |
| @@ -277,7 +280,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
| 277 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ | 280 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 278 | if (len != (off_t)-1) { | 281 | if (len != (off_t)-1) { |
| 279 | xlseek(fd, 0, SEEK_SET); | 282 | xlseek(fd, 0, SEEK_SET); |
| 280 | size = sizep ? *sizep : INT_MAX; | 283 | size = maxsz_p ? *maxsz_p : INT_MAX; |
| 281 | if (len < size) | 284 | if (len < size) |
| 282 | size = len; | 285 | size = len; |
| 283 | } | 286 | } |
| @@ -291,15 +294,15 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
| 291 | xrealloc(buf, size + 1); | 294 | xrealloc(buf, size + 1); |
| 292 | buf[size] = '\0'; | 295 | buf[size] = '\0'; |
| 293 | 296 | ||
| 294 | if (sizep) | 297 | if (maxsz_p) |
| 295 | *sizep = size; | 298 | *maxsz_p = size; |
| 296 | return buf; | 299 | return buf; |
| 297 | } | 300 | } |
| 298 | #endif | 301 | #endif |
| 299 | 302 | ||
| 300 | // Read (potentially big) files in one go. File size is estimated | 303 | // Read (potentially big) files in one go. File size is estimated |
| 301 | // by stat. | 304 | // by stat. |
| 302 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | 305 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
| 303 | { | 306 | { |
| 304 | char *buf; | 307 | char *buf; |
| 305 | int fd; | 308 | int fd; |
| @@ -308,15 +311,42 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
| 308 | if (fd < 0) | 311 | if (fd < 0) |
| 309 | return NULL; | 312 | return NULL; |
| 310 | 313 | ||
| 311 | buf = xmalloc_read(fd, sizep); | 314 | buf = xmalloc_read(fd, maxsz_p); |
| 312 | close(fd); | 315 | close(fd); |
| 313 | return buf; | 316 | return buf; |
| 314 | } | 317 | } |
| 315 | 318 | ||
| 316 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep) | 319 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) |
| 317 | { | 320 | { |
| 318 | void *buf = xmalloc_open_read_close(filename, sizep); | 321 | void *buf = xmalloc_open_read_close(filename, maxsz_p); |
| 319 | if (!buf) | 322 | if (!buf) |
| 320 | bb_perror_msg_and_die("can't read '%s'", filename); | 323 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 321 | return buf; | 324 | return buf; |
| 322 | } | 325 | } |
| 326 | |||
| 327 | #if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED | ||
| 328 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) | ||
| 329 | { | ||
| 330 | char *image; | ||
| 331 | char *suffix; | ||
| 332 | |||
| 333 | int fd = open(fname, O_RDONLY); | ||
| 334 | if (fd < 0) | ||
| 335 | return NULL; | ||
| 336 | |||
| 337 | suffix = strrchr(fname, '.'); | ||
| 338 | if (suffix) { | ||
| 339 | if (strcmp(suffix, ".gz") == 0) | ||
| 340 | open_transformer(fd, unpack_gz_stream, "gunzip"); | ||
| 341 | else if (strcmp(suffix, ".bz2") == 0) | ||
| 342 | open_transformer(fd, unpack_bz2_stream, "bunzip2"); | ||
| 343 | } | ||
| 344 | |||
| 345 | image = xmalloc_read(fd, maxsz_p); | ||
| 346 | if (!image) | ||
| 347 | bb_perror_msg("read error from '%s'", fname); | ||
| 348 | close(fd); | ||
| 349 | |||
| 350 | return image; | ||
| 351 | } | ||
| 352 | #endif | ||
