modprobe: use buffering line reads (fgets) instead of reads().
[oweals/busybox.git] / libbb / read.c
index 7b804125ab197f893cf064e0f89d3c189a1dc282..18f62838ec69a91888fd9baba0f216e576c78d0e 100644 (file)
@@ -8,6 +8,9 @@
  */
 
 #include "libbb.h"
+#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
+#include "unarchive.h"
+#endif
 
 ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
 {
@@ -106,7 +109,7 @@ ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
        return total;
 }
 
-// Die with an error message if we can't read the entire buffer.
+/* Die with an error message if we can't read the entire buffer. */
 void FAST_FUNC xread(int fd, void *buf, size_t count)
 {
        if (count) {
@@ -116,7 +119,7 @@ void FAST_FUNC xread(int fd, void *buf, size_t count)
        }
 }
 
-// Die with an error message if we can't read one character.
+/* Die with an error message if we can't read one character. */
 unsigned char FAST_FUNC xread_char(int fd)
 {
        char tmp;
@@ -124,31 +127,6 @@ unsigned char FAST_FUNC xread_char(int fd)
        return tmp;
 }
 
-// Read one line a-la fgets. Works only on seekable streams
-char* FAST_FUNC reads(int fd, char *buffer, size_t size)
-{
-       char *p;
-
-       if (size < 2)
-               return NULL;
-       size = full_read(fd, buffer, size-1);
-       if ((ssize_t)size <= 0)
-               return NULL;
-
-       buffer[size] = '\0';
-       p = strchr(buffer, '\n');
-       if (p) {
-               off_t offset;
-               *p++ = '\0';
-               // avoid incorrect (unsigned) widening
-               offset = (off_t)(p - buffer) - (off_t)size;
-               // set fd position right after '\n'
-               if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
-                       return NULL;
-       }
-       return buffer;
-}
-
 // Reads one line a-la fgets (but doesn't save terminating '\n').
 // Reads byte-by-byte. Useful when it is important to not read ahead.
 // Bytes are appended to pfx (which must be malloced, or NULL).
@@ -203,39 +181,52 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
        return read_close(fd, buf, size);
 }
 
+
 // Read (potentially big) files in one go. File size is estimated
-// by stat.
-void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
+// by stat. Extra '\0' byte is appended.
+void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
 {
        char *buf;
-       size_t size;
-       int fd;
-       off_t len;
+       size_t size, rd_size, total;
+       size_t to_read;
        struct stat st;
 
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               return NULL;
+       to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */
 
-       st.st_size = 0; /* in case fstat fail, define to 0 */
+       /* Estimate file size */
+       st.st_size = 0; /* in case fstat fails, assume 0 */
        fstat(fd, &st);
-       /* /proc/N/stat files report len 0 here */
+       /* /proc/N/stat files report st_size 0 */
        /* In order to make such files readable, we add small const */
-       len = st.st_size | 0x3ff; /* read only 1k on unseekable files */
-       size = sizep ? *sizep : INT_MAX;
-       if (len < size)
-               size = len;
-       buf = xmalloc(size + 1);
-       size = read_close(fd, buf, size);
-       if ((ssize_t)size < 0) {
-               free(buf);
-               return NULL;
+       size = (st.st_size | 0x3ff) + 1;
+
+       total = 0;
+       buf = NULL;
+       while (1) {
+               if (to_read < size)
+                       size = to_read;
+               buf = xrealloc(buf, total + size + 1);
+               rd_size = full_read(fd, buf + total, size);
+               if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
+                       free(buf);
+                       return NULL;
+               }
+               total += rd_size;
+               if (rd_size < size) /* EOF */
+                       break;
+               if (to_read <= rd_size)
+                       break;
+               to_read -= rd_size;
+               /* grow by 1/8, but in [1k..64k] bounds */
+               size = ((total / 8) | 0x3ff) + 1;
+               if (size > 64*1024)
+                       size = 64*1024;
        }
-       xrealloc(buf, size + 1);
-       buf[size] = '\0';
+       xrealloc(buf, total + 1);
+       buf[total] = '\0';
 
-       if (sizep)
-               *sizep = size;
+       if (maxsz_p)
+               *maxsz_p = total;
        return buf;
 }
 
@@ -247,7 +238,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
 
 // Read (potentially big) files in one go. File size is estimated by
 // lseek to end.
-void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
+void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
 {
        char *buf;
        size_t size;
@@ -264,7 +255,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
        len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
        if (len != (off_t)-1) {
                xlseek(fd, 0, SEEK_SET);
-               size = sizep ? *sizep : INT_MAX;
+               size = maxsz_p ? *maxsz_p : INT_MAX;
                if (len < size)
                        size = len;
        }
@@ -278,16 +269,59 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
        xrealloc(buf, size + 1);
        buf[size] = '\0';
 
-       if (sizep)
-               *sizep = size;
+       if (maxsz_p)
+               *maxsz_p = size;
        return buf;
 }
 #endif
 
-void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep)
+// Read (potentially big) files in one go. File size is estimated
+// by stat.
+void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
 {
-       void *buf = xmalloc_open_read_close(filename, sizep);
+       char *buf;
+       int fd;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return NULL;
+
+       buf = xmalloc_read(fd, maxsz_p);
+       close(fd);
+       return buf;
+}
+
+void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
+{
+       void *buf = xmalloc_open_read_close(filename, maxsz_p);
        if (!buf)
                bb_perror_msg_and_die("can't read '%s'", filename);
        return buf;
 }
+
+#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
+void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
+{
+       char *image;
+       char *suffix;
+
+       int fd = open(fname, O_RDONLY);
+       if (fd < 0)
+               return NULL;
+
+       suffix = strrchr(fname, '.');
+       if (suffix) {
+               if (strcmp(suffix, ".gz") == 0)
+                       open_transformer(fd, unpack_gz_stream, "gunzip");
+               else if (strcmp(suffix, ".bz2") == 0)
+                       open_transformer(fd, unpack_bz2_stream, "bunzip2");
+       }
+
+       image = xmalloc_read(fd, maxsz_p);
+       if (!image)
+               bb_perror_msg("read error from '%s'", fname);
+       close(fd);
+
+       return image;
+}
+#endif