Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 11 | #if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED |
| 12 | #include "unarchive.h" |
| 13 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 15 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 16 | { |
| 17 | ssize_t n; |
| 18 | |
| 19 | do { |
| 20 | n = read(fd, buf, count); |
| 21 | } while (n < 0 && errno == EINTR); |
| 22 | |
| 23 | return n; |
| 24 | } |
| 25 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 26 | /* Suppose that you are a shell. You start child processes. |
| 27 | * They work and eventually exit. You want to get user input. |
| 28 | * You read stdin. But what happens if last child switched |
| 29 | * its stdin into O_NONBLOCK mode? |
| 30 | * |
| 31 | * *** SURPRISE! It will affect the parent too! *** |
| 32 | * *** BIG SURPRISE! It stays even after child exits! *** |
| 33 | * |
| 34 | * This is a design bug in UNIX API. |
| 35 | * fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK); |
| 36 | * will set nonblocking mode not only on _your_ stdin, but |
| 37 | * also on stdin of your parent, etc. |
| 38 | * |
| 39 | * In general, |
| 40 | * fd2 = dup(fd1); |
| 41 | * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL, 0) | O_NONBLOCK); |
| 42 | * sets both fd1 and fd2 to O_NONBLOCK. This includes cases |
| 43 | * where duping is done implicitly by fork() etc. |
| 44 | * |
| 45 | * We need |
| 46 | * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK); |
| 47 | * (note SETFD, not SETFL!) but such thing doesn't exist. |
| 48 | * |
| 49 | * Alternatively, we need nonblocking_read(fd, ...) which doesn't |
| 50 | * require O_NONBLOCK dance at all. Actually, it exists: |
| 51 | * n = recv(fd, buf, len, MSG_DONTWAIT); |
| 52 | * "MSG_DONTWAIT: |
| 53 | * Enables non-blocking operation; if the operation |
| 54 | * would block, EAGAIN is returned." |
| 55 | * but recv() works only for sockets! |
| 56 | * |
| 57 | * So far I don't see any good solution, I can only propose |
| 58 | * that affected readers should be careful and use this routine, |
| 59 | * which detects EAGAIN and uses poll() to wait on the fd. |
Denis Vlasenko | 081efd1 | 2008-02-20 22:57:24 +0000 | [diff] [blame] | 60 | * Thankfully, poll() doesn't care about O_NONBLOCK flag. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 61 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 62 | ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 63 | { |
| 64 | struct pollfd pfd[1]; |
| 65 | ssize_t n; |
| 66 | |
| 67 | while (1) { |
| 68 | n = safe_read(fd, buf, count); |
| 69 | if (n >= 0 || errno != EAGAIN) |
| 70 | return n; |
| 71 | /* fd is in O_NONBLOCK mode. Wait using poll and repeat */ |
| 72 | pfd[0].fd = fd; |
| 73 | pfd[0].events = POLLIN; |
| 74 | safe_poll(pfd, 1, -1); |
| 75 | } |
| 76 | } |
| 77 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 78 | /* |
| 79 | * Read all of the supplied buffer from a file. |
| 80 | * This does multiple reads as necessary. |
| 81 | * Returns the amount read, or -1 on an error. |
| 82 | * A short read is returned on an end of file. |
| 83 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 84 | ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 85 | { |
| 86 | ssize_t cc; |
| 87 | ssize_t total; |
| 88 | |
| 89 | total = 0; |
| 90 | |
| 91 | while (len) { |
| 92 | cc = safe_read(fd, buf, len); |
| 93 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 94 | if (cc < 0) { |
| 95 | if (total) { |
| 96 | /* we already have some! */ |
| 97 | /* user can do another read to know the error code */ |
| 98 | return total; |
| 99 | } |
| 100 | return cc; /* read() returns -1 on failure. */ |
| 101 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 102 | if (cc == 0) |
| 103 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 104 | buf = ((char *)buf) + cc; |
| 105 | total += cc; |
| 106 | len -= cc; |
| 107 | } |
| 108 | |
| 109 | return total; |
| 110 | } |
| 111 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 112 | /* Die with an error message if we can't read the entire buffer. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 113 | void FAST_FUNC xread(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 114 | { |
| 115 | if (count) { |
| 116 | ssize_t size = full_read(fd, buf, count); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 117 | if ((size_t)size != count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 118 | bb_error_msg_and_die("short read"); |
| 119 | } |
| 120 | } |
| 121 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 122 | /* Die with an error message if we can't read one character. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 123 | unsigned char FAST_FUNC xread_char(int fd) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 124 | { |
| 125 | char tmp; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 126 | xread(fd, &tmp, 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 127 | return tmp; |
| 128 | } |
| 129 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 130 | /* Read one line a-la fgets. Works only on seekable streams */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 131 | char* FAST_FUNC reads(int fd, char *buffer, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 132 | { |
| 133 | char *p; |
| 134 | |
| 135 | if (size < 2) |
| 136 | return NULL; |
| 137 | size = full_read(fd, buffer, size-1); |
| 138 | if ((ssize_t)size <= 0) |
| 139 | return NULL; |
| 140 | |
| 141 | buffer[size] = '\0'; |
| 142 | p = strchr(buffer, '\n'); |
| 143 | if (p) { |
| 144 | off_t offset; |
| 145 | *p++ = '\0'; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 146 | /* avoid incorrect (unsigned) widening */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 147 | offset = (off_t)(p - buffer) - (off_t)size; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 148 | /* set fd position right after '\n' */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 149 | if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) |
| 150 | return NULL; |
| 151 | } |
| 152 | return buffer; |
| 153 | } |
| 154 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 155 | // Reads one line a-la fgets (but doesn't save terminating '\n'). |
| 156 | // Reads byte-by-byte. Useful when it is important to not read ahead. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 157 | // Bytes are appended to pfx (which must be malloced, or NULL). |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 158 | char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p) |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 159 | { |
| 160 | char *p; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 161 | size_t sz = buf ? strlen(buf) : 0; |
| 162 | size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t); |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 163 | |
| 164 | goto jump_in; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 165 | while (sz < maxsz) { |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 166 | if ((size_t)(p - buf) == sz) { |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 167 | jump_in: |
| 168 | buf = xrealloc(buf, sz + 128); |
| 169 | p = buf + sz; |
| 170 | sz += 128; |
| 171 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 172 | /* nonblock_safe_read() because we are used by e.g. shells */ |
| 173 | if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */ |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 174 | if (p == buf) { /* we read nothing */ |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 175 | free(buf); |
| 176 | return NULL; |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | if (*p == '\n') |
| 181 | break; |
| 182 | p++; |
| 183 | } |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 184 | *p = '\0'; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 185 | if (maxsz_p) |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 186 | *maxsz_p = p - buf; |
| 187 | p++; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 188 | return xrealloc(buf, p - buf); |
| 189 | } |
| 190 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 191 | ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 192 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 193 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 194 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 195 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 196 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 197 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 198 | return size; |
| 199 | } |
| 200 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 201 | ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 202 | { |
| 203 | int fd = open(filename, O_RDONLY); |
| 204 | if (fd < 0) |
| 205 | return fd; |
| 206 | return read_close(fd, buf, size); |
| 207 | } |
| 208 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | fe733a9 | 2008-06-24 16:08:22 +0000 | [diff] [blame] | 210 | // Read (potentially big) files in one go. File size is estimated |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 211 | // by stat. Extra '\0' byte is appended. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 212 | void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 213 | { |
| 214 | char *buf; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 215 | size_t size, rd_size, total; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 216 | size_t to_read; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 217 | struct stat st; |
| 218 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 219 | to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 220 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 221 | /* Estimate file size */ |
| 222 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 223 | fstat(fd, &st); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 224 | /* /proc/N/stat files report st_size 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 225 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 226 | size = (st.st_size | 0x3ff) + 1; |
| 227 | |
| 228 | total = 0; |
| 229 | buf = NULL; |
| 230 | while (1) { |
| 231 | if (to_read < size) |
| 232 | size = to_read; |
| 233 | buf = xrealloc(buf, total + size + 1); |
| 234 | rd_size = full_read(fd, buf + total, size); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 235 | if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 236 | free(buf); |
| 237 | return NULL; |
| 238 | } |
| 239 | total += rd_size; |
| 240 | if (rd_size < size) /* EOF */ |
| 241 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 242 | if (to_read <= rd_size) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 243 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 244 | to_read -= rd_size; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 245 | /* grow by 1/8, but in [1k..64k] bounds */ |
| 246 | size = ((total / 8) | 0x3ff) + 1; |
| 247 | if (size > 64*1024) |
| 248 | size = 64*1024; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 249 | } |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 250 | xrealloc(buf, total + 1); |
| 251 | buf[total] = '\0'; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 252 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 253 | if (maxsz_p) |
| 254 | *maxsz_p = total; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 255 | return buf; |
| 256 | } |
| 257 | |
| 258 | #ifdef USING_LSEEK_TO_GET_SIZE |
| 259 | /* Alternatively, file size can be obtained by lseek to the end. |
| 260 | * The code is slightly bigger. Retained in case fstat approach |
| 261 | * will not work for some weird cases (/proc, block devices, etc). |
| 262 | * (NB: lseek also can fail to work for some weird files) */ |
| 263 | |
| 264 | // Read (potentially big) files in one go. File size is estimated by |
| 265 | // lseek to end. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 266 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 267 | { |
| 268 | char *buf; |
| 269 | size_t size; |
| 270 | int fd; |
| 271 | off_t len; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 273 | fd = open(filename, O_RDONLY); |
| 274 | if (fd < 0) |
| 275 | return NULL; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 277 | /* /proc/N/stat files report len 0 here */ |
| 278 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 279 | size = 0x3ff; /* read only 1k on unseekable files */ |
| 280 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 281 | if (len != (off_t)-1) { |
| 282 | xlseek(fd, 0, SEEK_SET); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 283 | size = maxsz_p ? *maxsz_p : INT_MAX; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 284 | if (len < size) |
| 285 | size = len; |
| 286 | } |
| 287 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 288 | buf = xmalloc(size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 289 | size = read_close(fd, buf, size); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 290 | if ((ssize_t)size < 0) { |
| 291 | free(buf); |
| 292 | return NULL; |
| 293 | } |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 294 | xrealloc(buf, size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 295 | buf[size] = '\0'; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 297 | if (maxsz_p) |
| 298 | *maxsz_p = size; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 299 | return buf; |
| 300 | } |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 301 | #endif |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 302 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 303 | // Read (potentially big) files in one go. File size is estimated |
| 304 | // by stat. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 305 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 306 | { |
| 307 | char *buf; |
| 308 | int fd; |
| 309 | |
| 310 | fd = open(filename, O_RDONLY); |
| 311 | if (fd < 0) |
| 312 | return NULL; |
| 313 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 314 | buf = xmalloc_read(fd, maxsz_p); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 315 | close(fd); |
| 316 | return buf; |
| 317 | } |
| 318 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 319 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 320 | { |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 321 | void *buf = xmalloc_open_read_close(filename, maxsz_p); |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 322 | if (!buf) |
| 323 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 324 | return buf; |
| 325 | } |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame^] | 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 |