X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=io.c;h=1ef3d3df9361126266e73b0fe45d0934457d2e4f;hp=d2b3197027b69cd4fc0e40f1333a123887a38a7f;hb=0fa47e6453747e3ef4ab707a5bcb34980a465a75;hpb=95317cdba6d5b393db2e68f44487456ea0d99664 diff --git a/io.c b/io.c index d2b3197..1ef3d3d 100644 --- a/io.c +++ b/io.c @@ -1,15 +1,13 @@ // Copyright (2013) Jann Horn -// This code is licensed under the AGPLv3. HEADER #include #include #include #include -#include +HEADER #include #include #include #include -#include #include // Wrapper for `read` that retries on partial reads. @@ -24,6 +22,7 @@ PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) { size_t done = 0; while (done < count) { ssize_t part_res = read(fd, buf+done, count-done); + if (part_res == -1 && errno == EINTR) continue; if (part_res <= 0) { if (last_res) *last_res = part_res; if (done) return done; @@ -42,11 +41,13 @@ PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) { // - -1: error // - 0: stream ended // - 1: no problems occured -PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) { +PUBLIC_FN ssize_t write_nointr(int fd, void *buf, ssize_t count, int *last_res) { + if (count == -1) count = strlen(buf); errno = 0; size_t done = 0; while (done < count) { ssize_t part_res = write(fd, buf+done, count-done); + if (part_res == -1 && errno == EINTR) continue; if (part_res <= 0) { if (last_res) *last_res = part_res; if (done) return done; @@ -67,6 +68,9 @@ PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) { PUBLIC_CONST JH_SLURP_NO_STAT 1 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */ PUBLIC_CONST JH_SLURP_8BYTE_PAD 4 /* pad buffer with eight nullbytes, not one */ +PUBLIC_CONST JH_SLURP_FIXSIZE 8 /* len_out is actually input and specifies +HEADER the expected length. if there is more data, +HEADER that might be ignored silently. */ PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) { int errno_; @@ -75,6 +79,13 @@ PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) { size_t size_guess = 1023; bool trusted_guess = false; /* can we rely on the guess? */ + if (flags&JH_SLURP_FIXSIZE) { + size_guess = *len_out; + trusted_guess = true; + // if we want a fixed size, of course we don't want to stat + flags |= JH_SLURP_NO_STAT; + } + int padlen = (flags&JH_SLURP_8BYTE_PAD)?8:1; // If we can determine the exact size, we don't have to guess. So try @@ -113,6 +124,12 @@ PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) { char *buf_ = realloc(buf, done+padlen); if (buf_) buf = buf_; } + if (done != size_guess && (flags&JH_SLURP_FIXSIZE)) { + free(buf); + // not the most correct error message ever, but whatever. + errno = EFBIG; + return NULL; + } if (len_out) *len_out = done; return buf; } @@ -163,3 +180,37 @@ PUBLIC_FN void *mmap_file(void *addr, size_t length, int prot, int flags, char * errno = errno_; return (r==MAP_FAILED) ? NULL : r; } + +// on error, fd remains unchanged, no matter whether you specified "keep" or not +HEADER #define JH_OBS_KEEPFD 1 +HEADER #define JH_OBS_BUFFER 2 +PUBLIC_FN int fopen_bistream(FILE **in, FILE **out, int fd, int flags) { + // check input + if (fd < 0) goto err_out; + bool keep = flags & JH_OBS_KEEPFD; + bool buffer = flags & JH_OBS_BUFFER; + + // create input FILE* + int in_fd = dup(fd); + if (in_fd < 0) goto err_out; + *in = fdopen(in_fd, "r"); + if (!*in) { close(in_fd); goto err_out; } + + // create output FILE* + int out_fd = keep ? dup(fd) : fd; + if (out_fd < 0) goto err_dup_out; + *out = fdopen(out_fd, "w"); + if (!*out) { if (keep) close(out_fd); goto err_dup_out; } + + // avoid buffering data unless the caller really wants it + if (!buffer) setbuf(*out, NULL); + + // success! + return 0; + + +err_dup_out: + fclose(*in); +err_out: + return EAI_SYSTEM; +}