]> git.ozlabs.org Git - ccan/commitdiff
grab_fd and grab_file: add a size arg, use everywhere.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Aug 2008 00:48:06 +0000 (10:48 +1000)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Aug 2008 00:48:06 +0000 (10:48 +1000)
ccan/string/string.c
ccan/string/string.h
ccan/string/test/run-grab.c
tools/_infotojson/infotojson.c
tools/ccanlint/Makefile
tools/ccanlint/file_analysis.c
tools/ccanlint/get_file_lines.c [deleted file]
tools/create_dep_tar
tools/create_dep_tar.c
tools/depends.c

index 6e473af36cf3862580e6c1f706d46861e99dc874..f0d0fd4f3ff289ce8cb75f36ec3f7d7803a94649 100644 (file)
@@ -48,33 +48,37 @@ char *strjoin(const void *ctx, char *strings[], const char *delim)
        return ret;
 }
 
        return ret;
 }
 
-void *grab_fd(const void *ctx, int fd)
+void *grab_fd(const void *ctx, int fd, size_t *size)
 {
        int ret;
 {
        int ret;
-       unsigned int max = 16384, size = 0;
+       size_t max = 16384, s;
        char *buffer;
 
        char *buffer;
 
+       if (!size)
+               size = &s;
+       *size = 0;
+
        buffer = talloc_array(ctx, char, max+1);
        buffer = talloc_array(ctx, char, max+1);
-       while ((ret = read(fd, buffer + size, max - size)) > 0) {
-               size += ret;
-               if (size == max)
+       while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
+               *size += ret;
+               if (*size == max)
                        buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
        }
        if (ret < 0) {
                talloc_free(buffer);
                buffer = NULL;
        } else
                        buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
        }
        if (ret < 0) {
                talloc_free(buffer);
                buffer = NULL;
        } else
-               buffer[size] = '\0';
+               buffer[*size] = '\0';
 
        return buffer;
 }
 
 
        return buffer;
 }
 
-void *grab_file(const void *ctx, const char *filename)
+void *grab_file(const void *ctx, const char *filename, size_t *size)
 {
        int fd;
        char *buffer;
 
 {
        int fd;
        char *buffer;
 
-       if (streq(filename, "-"))
+       if (!filename)
                fd = dup(STDIN_FILENO);
        else
                fd = open(filename, O_RDONLY, 0);
                fd = dup(STDIN_FILENO);
        else
                fd = open(filename, O_RDONLY, 0);
@@ -82,7 +86,7 @@ void *grab_file(const void *ctx, const char *filename)
        if (fd < 0)
                return NULL;
 
        if (fd < 0)
                return NULL;
 
-       buffer = grab_fd(ctx, fd);
+       buffer = grab_fd(ctx, fd, size);
        close_noerr(fd);
        return buffer;
 }
        close_noerr(fd);
        return buffer;
 }
index 14a9c2c8fdba86eedbb4c0845c5cd24d69035b34..3bc7adfd006f034b037e9beea06b200bc51be392 100644 (file)
@@ -103,7 +103,57 @@ char **strsplit(const void *ctx, const char *string, const char *delims,
  */
 char *strjoin(const void *ctx, char *strings[], const char *delim);
 
  */
 char *strjoin(const void *ctx, char *strings[], const char *delim);
 
-void *grab_fd(const void *ctx, int fd);
+/**
+ * grab_fd - read all of a file descriptor into memory
+ * @ctx: the context to tallocate from (often NULL)
+ * @fd: the file descriptor to read from
+ * @size: the (optional) size of the file
+ *
+ * This function reads from the given file descriptor until no more
+ * input is available.  The content is talloced off @ctx, and the size
+ * of the file places in @size if it's non-NULL.  For convenience, the
+ * byte after the end of the content will always be NUL.
+ *
+ * Example:
+ *     // Return all of standard input, as lines.
+ *     char **read_as_lines(void)
+ *     {
+ *             char **lines, *all;
+ *
+ *             all = grab_fd(NULL, 0, NULL);
+ *             if (!all)
+ *                     return NULL;
+ *             lines = strsplit(NULL, all, "\n", NULL);
+ *             talloc_free(all);
+ *             return lines;
+ *     }
+ */
+void *grab_fd(const void *ctx, int fd, size_t *size);
 
 
-void *grab_file(const void *ctx, const char *filename);
+/**
+ * grab_file - read all of a file (or stdin) into memory
+ * @ctx: the context to tallocate from (often NULL)
+ * @filename: the file to read (NULL for stdin)
+ * @size: the (optional) size of the file
+ *
+ * This function reads from the given file until no more input is
+ * available.  The content is talloced off @ctx, and the size of the
+ * file places in @size if it's non-NULL.  For convenience, the byte
+ * after the end of the content will always be NUL.
+ *
+ * Example:
+ *     // Return all of a given file, as lines.
+ *     char **read_as_lines(const char *filename)
+ *     {
+ *             char **lines, *all;
+ *
+ *             all = grab_file(NULL, filename, NULL);
+ *             if (!all)
+ *                     return NULL;
+ *             lines = strsplit(NULL, all, "\n", NULL);
+ *             talloc_free(all);
+ *             return lines;
+ *     }
+ */
+void *grab_file(const void *ctx, const char *filename, size_t *size);
 #endif /* CCAN_STRING_H */
 #endif /* CCAN_STRING_H */
index 96b9dac852b428224171563e608c6126ec1e1f0f..2b36a0fb70d77ba03b59ae83cb64ab66e6acb56b 100644 (file)
@@ -1,24 +1,4 @@
 /* This is test for grab_file() function
 /* This is test for grab_file() function
- *
- * Example:
- * 
- * void *grab_file(const void *ctx, const char *filename)
- *     {
- *     int fd; 
- *     char *buffer;
- * 
- *     if (streq(filename, "-"))
- *             fd = dup(STDIN_FILENO);
- *     else
- *             fd = open(filename, O_RDONLY, 0); 
- *
- *     if (fd < 0)
- *             return NULL; 
- *
- *     buffer = grab_fd(ctx, fd);
- *     close_noerr(fd);
- *     return buffer;
- *     }
  */
 
 #include       <stdlib.h>
  */
 
 #include       <stdlib.h>
@@ -37,7 +17,7 @@ main(int argc, char *argv[])
        int             length;
        struct          stat st;
 
        int             length;
        struct          stat st;
 
-       str = grab_file(NULL, "ccan/string/test/run-grab.c");
+       str = grab_file(NULL, "ccan/string/test/run-grab.c", NULL);
        split = strsplit(NULL, str, "\n", NULL);
        length = strlen(split[0]);
        ok1(streq(split[0], "/* This is test for grab_file() function"));
        split = strsplit(NULL, str, "\n", NULL);
        length = strlen(split[0]);
        ok1(streq(split[0], "/* This is test for grab_file() function"));
index f1493cba337cd5222c77c7fb882217e31e709e41..4f21602c29eb8ca4c267c6a6c720772f524a8227 100644 (file)
@@ -129,7 +129,7 @@ int main(int argc, char *argv[])
                errx(1, "usage: infotojson dir_of_module info_filename target_json_file author [sqlitedb]\n"
                                 "Convert _info.c file to json file and optionally store to database");
                
                errx(1, "usage: infotojson dir_of_module info_filename target_json_file author [sqlitedb]\n"
                                 "Convert _info.c file to json file and optionally store to database");
                
-       file = grab_file(NULL, argv[2]);
+       file = grab_file(NULL, argv[2], NULL);
        if (!file)
                err(1, "Reading file %s", argv[2]);
 
        if (!file)
                err(1, "Reading file %s", argv[2]);
 
index 40c26a83978c5049e57258ac373e480b535e294d..4cc5044072d388c7a65ac477f587b663f887c06c 100644 (file)
@@ -22,7 +22,6 @@ tools/ccanlint/ccanlint.o: tools/ccanlint/generated-init-tests
 tools/ccanlint/ccanlint: \
        $(OBJS)                 \
        tools/ccanlint/ccanlint.o \
 tools/ccanlint/ccanlint: \
        $(OBJS)                 \
        tools/ccanlint/ccanlint.o \
-       tools/ccanlint/get_file_lines.o \
        tools/ccanlint/file_analysis.o \
        ccan/string/string.o ccan/talloc/talloc.o ccan/noerr/noerr.o
 
        tools/ccanlint/file_analysis.o \
        ccan/string/string.o ccan/talloc/talloc.o ccan/noerr/noerr.o
 
index 1341e57b11d3230780704ead95a0e123deb10042..bee99597310c346f5b8a291f40858de41b8605d5 100644 (file)
 
 char **get_ccan_file_lines(struct ccan_file *f)
 {
 
 char **get_ccan_file_lines(struct ccan_file *f)
 {
-       if (!f->lines)
-               f->lines = get_file_lines(f, f->name, &f->num_lines);
+       if (!f->lines) {
+               char *buffer = grab_file(f, f->name, NULL);
+               if (!buffer)
+                       err(1, "Getting file %s", f->name);
+               f->lines = strsplit(f, buffer, "\n", &f->num_lines);
+       }
        return f->lines;
 }
 
        return f->lines;
 }
 
diff --git a/tools/ccanlint/get_file_lines.c b/tools/ccanlint/get_file_lines.c
deleted file mode 100644 (file)
index 2f27a01..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "get_file_lines.h"
-#include <talloc/talloc.h>
-#include <string/string.h>
-#include <noerr/noerr.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <err.h>
-#include <dirent.h>
-
-static void *grab_fd(const void *ctx, int fd)
-{
-       int ret;
-       unsigned int max = 16384, size = 0;
-       char *buffer;
-
-       buffer = talloc_array(ctx, char, max+1);
-       while ((ret = read(fd, buffer + size, max - size)) > 0) {
-               size += ret;
-               if (size == max)
-                       buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
-       }
-       if (ret < 0) {
-               talloc_free(buffer);
-               buffer = NULL;
-       } else
-               buffer[size] = '\0';
-
-       return buffer;
-}
-
-/* This version adds one byte (for nul term) */
-static void *grab_file(const void *ctx, const char *filename)
-{
-       int fd;
-       char *buffer;
-
-       if (streq(filename, "-"))
-               fd = dup(STDIN_FILENO);
-       else
-               fd = open(filename, O_RDONLY, 0);
-
-       if (fd < 0)
-               return NULL;
-
-       buffer = grab_fd(ctx, fd);
-       close_noerr(fd);
-       return buffer;
-}
-
-char **get_file_lines(void *ctx, const char *name, unsigned int *num_lines)
-{
-       char *buffer = grab_file(ctx, name);
-
-       if (!buffer)
-               err(1, "Getting file %s", name);
-
-       return strsplit(buffer, buffer, "\n", num_lines);
-}
index 715fb6ba856c2d501e41284ea4852fe9ba7100db..29f15400ee8d0a48d584075527e9f52c2e8ecaff 100755 (executable)
Binary files a/tools/create_dep_tar and b/tools/create_dep_tar differ
index 3a84f8d9d4ef6933579ea1dfababd3ee358d1126..78bdea6c9031f833cc4da09f1b518a18956a288a 100644 (file)
@@ -61,7 +61,7 @@ create_tar(char **deps, const char *dir, const char *targetdir)
        if (!p)
                err(1, "Executing '%s'", cmd);
 
        if (!p)
                err(1, "Executing '%s'", cmd);
 
-       buffer = grab_fd(NULL, fileno(p));
+       buffer = grab_fd(NULL, fileno(p), NULL);
        if (!buffer)
                err(1, "Reading from '%s'", cmd);
        pclose(p);
        if (!buffer)
                err(1, "Reading from '%s'", cmd);
        pclose(p);
index e8cb53b8394c807aa9ddb6a7ba7a55777a06c38b..06e87c32e248d94223f9a67bb8bbdde9f6383745 100644 (file)
@@ -20,7 +20,7 @@ lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
        if (!p)
                err(1, "Executing '%s'", cmd);
 
        if (!p)
                err(1, "Executing '%s'", cmd);
 
-       buffer = grab_fd(ctx, fileno(p));
+       buffer = grab_fd(ctx, fileno(p), NULL);
        if (!buffer)
                err(1, "Reading from '%s'", cmd);
        pclose(p);
        if (!buffer)
                err(1, "Reading from '%s'", cmd);
        pclose(p);