+HEADER #include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+
+// Wrapper for `read` that retries on partial reads.
+// If last_res is non-NULL, it will be filled with the
+// result of the last read() call (but all positive numbers
+// become 1). In other words:
+// - -1: error
+// - 0: stream ended
+// - 1: no problems occured
+PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) {
+ errno = 0;
+ size_t done = 0;
+ while (done < count) {
+ ssize_t part_res = read(fd, buf+done, count-done);
+ if (part_res <= 0) {
+ if (last_res) *last_res = part_res;
+ if (done) return done;
+ return part_res;
+ }
+ done += part_res;
+ }
+ if (last_res) *last_res = 1;
+ return done;
+}
+
+// Wrapper for `write` that retries on partial writes.
+// If last_res is non-NULL, it will be filled with the
+// result of the last write() call (but all positive numbers
+// become 1). In other words:
+// - -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) {
+ errno = 0;
+ size_t done = 0;
+ while (done < count) {
+ ssize_t part_res = write(fd, buf+done, count-done);
+ if (part_res <= 0) {
+ if (last_res) *last_res = part_res;
+ if (done) return done;
+ return part_res;
+ }
+ done += part_res;
+ }
+ if (last_res) *last_res = 1;
+ return done;
+}
+
+// Read all data from the given file descriptor. Tries fstat()+read()
+// first, but if fstat() doesn't work, falls back to multiple read()s.
+// Specify JH_NO_STAT to prevent the fstat() call.
+// The return value is a malloc'd buffer.
+// The buffer will be null-terminated, so you can read text files with
+// this and can specify len_out as NULL.
+PUBLIC_CONST JH_SLURP_NO_STAT 1
+PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
+PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
+ int errno_;
+
+ // Let's just guess that the file is 1023 bytes. Will become 1024 with
+ // the nullbyte.
+ size_t size_guess = 1023;
+ bool trusted_guess = false; /* can we rely on the guess? */
+
+ // If we can determine the exact size, we don't have to guess. So try
+ // to determine the exact size.
+ if (!(flags&JH_SLURP_NO_STAT)) {
+ struct stat st;
+ if (fstat(fd, &st) == 0) {
+ if (st.st_size > 0) {
+ size_guess = st.st_size;
+ trusted_guess = true;
+ }
+ }
+ }
+
+ char *buf = NULL;
+ int done = 0;
+
+ while (1) {
+ buf = realloc(buf, size_guess+1);
+ if (buf == NULL) return NULL;
+ int last_res;
+ ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
+ if (last_res == -1) { errno_=errno; free(buf); errno=errno_; return NULL; }
+ done += read_res;
+ if (last_res == 0 || trusted_guess) {
+ // out
+ buf[done] = '\0';
+ if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
+ // Well, it'd be weird if shrinking could fail... but meh, I can't find
+ // an explicit statement about this being disallowed, so try staying on
+ // the safe side.
+ char *buf_ = realloc(buf, done+1);
+ if (buf_) buf = buf_;
+ }
+ if (len_out) *len_out = done;
+ return buf;
+ }
+ size_guess<<=1; // try two times the buffer size
+ }
+}
+
+PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
+ int fd = open(path, O_RDONLY);
+ if (fd == -1) return NULL;
+ char *res = slurp_fd(fd, len_out, flags);
+ int errno_ = errno;
+ close(fd);
+ errno = errno_;
+ return res;
+}
+
+// Write data into a file. len can be -1; that means that
+// the real length is strlen(buf).
+PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
+ if (len == -1) len = strlen(buf);
+
+ int fd = open(path, open_flags, 0777);
+ if (fd == -1) return 1;
+ ssize_t write_res = write_nointr(fd, buf, len, NULL);
+ int write_errno = errno;
+ int close_res = close(fd);
+ if (write_res != len) {
+ errno = write_errno;
+ return 1;
+ }
+ if (close_res) return 1;
+ return 0;
+}