]> git.ozlabs.org Git - ccan/blobdiff - ccan/string/string.c
commiting changes for test case of grab_file
[ccan] / ccan / string / string.c
index 7686813c9a392ab5cf94c8b624e3037e28fbd601..6e473af36cf3862580e6c1f706d46861e99dc874 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#include "noerr/noerr.h"
 
 char **strsplit(const void *ctx, const char *string, const char *delims,
                 unsigned int *nump)
@@ -67,3 +68,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_noerr(fd);
+       return buffer;
+}