X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fstring%2Fstring.c;h=9182ac0650fb7b9c94ba72aa12872ca7f44310d0;hp=7686813c9a392ab5cf94c8b624e3037e28fbd601;hb=909bf610f3dfdd5386c7cac24997fd2a72390ace;hpb=e52b97e4d99af6e2a564731c23c6c86e373aedaa 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; +}