Browse Source

grab_fd and grab_file: add a size arg, use everywhere.

Rusty Russell 17 years ago
parent
commit
8aeb80ab04

+ 13 - 9
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;
 }

+ 52 - 2
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 */

+ 1 - 21
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 	<stdlib.h>
@@ -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"));

+ 1 - 1
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]);
 

+ 0 - 1
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
 

+ 6 - 2
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;
 }
 

+ 0 - 60
tools/ccanlint/get_file_lines.c

@@ -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);
-}

BIN
tools/create_dep_tar


+ 1 - 1
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);

+ 1 - 1
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);