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);
if (fd < 0)
return NULL;
- buffer = grab_fd(ctx, fd);
+ buffer = grab_fd(ctx, fd, size);
close_noerr(fd);
return buffer;
}
*/
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 */
/* 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>
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"));
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]);
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
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;
}
+++ /dev/null
-#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);
-}
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 (!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);