From: Rusty Russell Date: Fri, 15 Aug 2008 00:48:06 +0000 (+1000) Subject: grab_fd and grab_file: add a size arg, use everywhere. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=8aeb80ab049612adefeafea65aaa038591200944;hp=74e9da4d1c0b968fbac4b8da165e6ad5318329dd grab_fd and grab_file: add a size arg, use everywhere. --- diff --git a/ccan/string/string.c b/ccan/string/string.c index 6e473af3..f0d0fd4f 100644 --- a/ccan/string/string.c +++ b/ccan/string/string.c @@ -48,33 +48,37 @@ char *strjoin(const void *ctx, char *strings[], const char *delim) return ret; } -void *grab_fd(const void *ctx, int fd) +void *grab_fd(const void *ctx, int fd, size_t *size) { int ret; - unsigned int max = 16384, size = 0; + size_t max = 16384, s; char *buffer; + if (!size) + size = &s; + *size = 0; + 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[size] = '\0'; + buffer[*size] = '\0'; 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; - if (streq(filename, "-")) + if (!filename) 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; - buffer = grab_fd(ctx, fd); + buffer = grab_fd(ctx, fd, size); close_noerr(fd); return buffer; } diff --git a/ccan/string/string.h b/ccan/string/string.h index 14a9c2c8..3bc7adfd 100644 --- a/ccan/string/string.h +++ b/ccan/string/string.h @@ -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); -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 */ diff --git a/ccan/string/test/run-grab.c b/ccan/string/test/run-grab.c index 96b9dac8..2b36a0fb 100644 --- a/ccan/string/test/run-grab.c +++ b/ccan/string/test/run-grab.c @@ -1,24 +1,4 @@ /* 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 @@ -37,7 +17,7 @@ main(int argc, char *argv[]) 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")); diff --git a/tools/_infotojson/infotojson.c b/tools/_infotojson/infotojson.c index f1493cba..4f21602c 100644 --- a/tools/_infotojson/infotojson.c +++ b/tools/_infotojson/infotojson.c @@ -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"); - file = grab_file(NULL, argv[2]); + file = grab_file(NULL, argv[2], NULL); if (!file) err(1, "Reading file %s", argv[2]); diff --git a/tools/ccanlint/Makefile b/tools/ccanlint/Makefile index 40c26a83..4cc50440 100644 --- a/tools/ccanlint/Makefile +++ b/tools/ccanlint/Makefile @@ -22,7 +22,6 @@ tools/ccanlint/ccanlint.o: tools/ccanlint/generated-init-tests 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 diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index 1341e57b..bee99597 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -13,8 +13,12 @@ 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; } diff --git a/tools/ccanlint/get_file_lines.c b/tools/ccanlint/get_file_lines.c deleted file mode 100644 index 2f27a012..00000000 --- a/tools/ccanlint/get_file_lines.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "get_file_lines.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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); -} diff --git a/tools/create_dep_tar b/tools/create_dep_tar index 715fb6ba..29f15400 100755 Binary files a/tools/create_dep_tar and b/tools/create_dep_tar differ diff --git a/tools/create_dep_tar.c b/tools/create_dep_tar.c index 3a84f8d9..78bdea6c 100644 --- a/tools/create_dep_tar.c +++ b/tools/create_dep_tar.c @@ -61,7 +61,7 @@ create_tar(char **deps, const char *dir, const char *targetdir) 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); diff --git a/tools/depends.c b/tools/depends.c index e8cb53b8..06e87c32 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -20,7 +20,7 @@ lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) 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);