1 HEADER #include <sys/types.h>
10 // Wrapper for `read` that retries on partial reads.
11 // If last_res is non-NULL, it will be filled with the
12 // result of the last read() call (but all positive numbers
13 // become 1). In other words:
16 // - 1: no problems occured
17 PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) {
20 while (done < count) {
21 ssize_t part_res = read(fd, buf+done, count-done);
23 if (last_res) *last_res = part_res;
24 if (done) return done;
29 if (last_res) *last_res = 1;
33 // Wrapper for `write` that retries on partial writes.
34 // If last_res is non-NULL, it will be filled with the
35 // result of the last write() call (but all positive numbers
36 // become 1). In other words:
39 // - 1: no problems occured
40 PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
43 while (done < count) {
44 ssize_t part_res = write(fd, buf+done, count-done);
46 if (last_res) *last_res = part_res;
47 if (done) return done;
52 if (last_res) *last_res = 1;
56 // Read all data from the given file descriptor. Tries fstat()+read()
57 // first, but if fstat() doesn't work, falls back to multiple read()s.
58 // Specify JH_NO_STAT to prevent the fstat() call.
59 // The return value is a malloc'd buffer.
60 // The buffer will be null-terminated, so you can read text files with
61 // this and can specify len_out as NULL.
62 PUBLIC_CONST JH_SLURP_NO_STAT 1
63 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
64 PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
67 // Let's just guess that the file is 1023 bytes. Will become 1024 with
69 size_t size_guess = 1023;
70 bool trusted_guess = false; /* can we rely on the guess? */
72 // If we can determine the exact size, we don't have to guess. So try
73 // to determine the exact size.
74 if (!(flags&JH_SLURP_NO_STAT)) {
76 if (fstat(fd, &st) == 0) {
78 size_guess = st.st_size;
88 buf = realloc(buf, size_guess+1);
89 if (buf == NULL) return NULL;
91 ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
92 if (last_res == -1) { errno_=errno; free(buf); errno=errno_; return NULL; }
94 if (last_res == 0 || trusted_guess) {
97 if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
98 // Well, it'd be weird if shrinking could fail... but meh, I can't find
99 // an explicit statement about this being disallowed, so try staying on
101 char *buf_ = realloc(buf, done+1);
102 if (buf_) buf = buf_;
104 if (len_out) *len_out = done;
107 size_guess<<=1; // try two times the buffer size
111 PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
112 int fd = open(path, O_RDONLY);
113 if (fd == -1) return NULL;
114 char *res = slurp_fd(fd, len_out, flags);
121 // Write data into a file. len can be -1; that means that
122 // the real length is strlen(buf).
123 PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
124 if (len == -1) len = strlen(buf);
126 int fd = open(path, open_flags, 0777);
127 if (fd == -1) return 1;
128 ssize_t write_res = write_nointr(fd, buf, len, NULL);
129 int write_errno = errno;
130 int close_res = close(fd);
131 if (write_res != len) {
135 if (close_res) return 1;