diff options
author | dinesh <dinesh@dinesh-laptop> | 2008-08-03 23:55:18 +0530 |
---|---|---|
committer | dinesh <dinesh@dinesh-laptop> | 2008-08-03 23:55:18 +0530 |
commit | 909bf610f3dfdd5386c7cac24997fd2a72390ace (patch) | |
tree | 1ce821af28f479f3521b49149fa78d6aee8634c2 | |
parent | e52b97e4d99af6e2a564731c23c6c86e373aedaa (diff) |
Moving grab_file
-rw-r--r-- | ccan/string/string.c | 27 | ||||
-rw-r--r-- | tools/grab_file.c | 8 |
2 files changed, 31 insertions, 4 deletions
diff --git a/ccan/string/string.c b/ccan/string/string.c index 7686813c..9182ac06 100644 --- a/ccan/string/string.c +++ b/ccan/string/string.c @@ -47,6 +47,15 @@ char *strjoin(const void *ctx, char *strings[], const char *delim) return ret; } +static int close_no_errno(int fd) +{ + int ret = 0, serrno = errno; + if (close(fd) < 0) + ret = errno; + errno = serrno; + return ret; +} + void *grab_fd(const void *ctx, int fd) { int ret; @@ -67,3 +76,21 @@ void *grab_fd(const void *ctx, int fd) return buffer; } + +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_no_errno(fd); + return buffer; +} diff --git a/tools/grab_file.c b/tools/grab_file.c index 3c9eee6d..5a2ff69b 100644 --- a/tools/grab_file.c +++ b/tools/grab_file.c @@ -7,14 +7,14 @@ #include <unistd.h> #include <errno.h> -static int close_no_errno(int fd) +/*static int close_no_errno(int fd) { int ret = 0, serrno = errno; if (close(fd) < 0) ret = errno; errno = serrno; return ret; -} +}*/ /*void *grab_fd(const void *ctx, int fd) { @@ -38,7 +38,7 @@ static int close_no_errno(int fd) }*/ /* This version adds one byte (for nul term) */ -void *grab_file(const void *ctx, const char *filename) +/*void *grab_file(const void *ctx, const char *filename) { int fd; char *buffer; @@ -54,5 +54,5 @@ void *grab_file(const void *ctx, const char *filename) buffer = grab_fd(ctx, fd); close_no_errno(fd); return buffer; -} +}*/ |