From 909bf610f3dfdd5386c7cac24997fd2a72390ace Mon Sep 17 00:00:00 2001 From: dinesh Date: Sun, 3 Aug 2008 23:55:18 +0530 Subject: [PATCH 1/1] Moving grab_file --- ccan/string/string.c | 27 +++++++++++++++++++++++++++ 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 #include -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; -} +}*/ -- 2.39.2