summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ccan/string/string.c27
-rw-r--r--tools/grab_file.c8
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;
-}
+}*/