diff options
Diffstat (limited to 'client/file.c')
-rw-r--r-- | client/file.c | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/client/file.c b/client/file.c index c109bd5..66d5f63 100644 --- a/client/file.c +++ b/client/file.c @@ -1,9 +1,8 @@ -/* $Id$ - * +/* * File handling for the wallet client. * * Written by Russ Allbery <rra@stanford.edu> - * Copyright 2007, 2008 Board of Trustees, Leland Stanford Jr. University + * Copyright 2007, 2008, 2010 Board of Trustees, Leland Stanford Jr. University * * See LICENSE for licensing terms. */ @@ -16,7 +15,9 @@ #include <sys/stat.h> #include <client/internal.h> -#include <util/util.h> +#include <util/concat.h> +#include <util/messages.h> +#include <util/xmalloc.h> /* * Given a filename, some data, and a length, write that data to the given @@ -114,14 +115,13 @@ get_file(struct remctl *r, const char *prefix, const char *type, /* - * Read all of a file into memory and return the contents as a newly allocated - * string. Handles a file name of "-" to mean standard input. Dies on any - * failure. - * - * This will need modification later when we want to handle nul characters. + * Read all of a file into memory and return the contents in newly allocated + * memory. Returns the size of the file contents in the second argument if + * it's not NULL. Handles a file name of "-" to mean standard input. Dies on + * any failure. */ -char * -read_file(const char *name) +void * +read_file(const char *name, size_t *length) { char *contents; size_t size, offset; @@ -139,7 +139,7 @@ read_file(const char *name) sysdie("cannot open file %s", name); if (fstat(fd, &st) < 0) sysdie("cannot stat file %s", name); - size = st.st_size + 1; + size = st.st_size; contents = xmalloc(size); } offset = 0; @@ -156,8 +156,7 @@ read_file(const char *name) offset += status; } while (status > 0); close(fd); - contents[offset] = '\0'; - if (memchr(contents, '\0', offset) != NULL) - die("cannot yet handle file data containing nul characters"); + if (length != NULL) + *length = offset; return contents; } |