initial commit
[libjh.git] / io.c
1 HEADER #include <sys/types.h>
2 #include <unistd.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdbool.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <string.h>
9
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:
14 //  - -1: error
15 //  - 0: stream ended
16 //  - 1: no problems occured
17 PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) {
18   errno = 0;
19   size_t done = 0;
20   while (done < count) {
21     ssize_t part_res = read(fd, buf+done, count-done);
22     if (part_res <= 0) {
23       if (last_res) *last_res = part_res;
24       if (done) return done;
25       return part_res;
26     }
27     done += part_res;
28   }
29   if (last_res) *last_res = 1;
30   return done;
31 }
32
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:
37 //  - -1: error
38 //  - 0: stream ended
39 //  - 1: no problems occured
40 PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
41   errno = 0;
42   size_t done = 0;
43   while (done < count) {
44     ssize_t part_res = write(fd, buf+done, count-done);
45     if (part_res <= 0) {
46       if (last_res) *last_res = part_res;
47       if (done) return done;
48       return part_res;
49     }
50     done += part_res;
51   }
52   if (last_res) *last_res = 1;
53   return done;
54 }
55
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) {
65   int errno_;
66   
67   // Let's just guess that the file is 1023 bytes. Will become 1024 with
68   // the nullbyte.
69   size_t size_guess = 1023;
70   bool trusted_guess = false; /* can we rely on the guess? */
71   
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)) {
75     struct stat st;
76     if (fstat(fd, &st) == 0) {
77       if (st.st_size > 0) {
78         size_guess = st.st_size;
79         trusted_guess = true;
80       }
81     }
82   }
83   
84   char *buf = NULL;
85   int done = 0;
86   
87   while (1) {
88     buf = realloc(buf, size_guess+1);
89     if (buf == NULL) return NULL;
90     int last_res;
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; }
93     done += read_res;
94     if (last_res == 0 || trusted_guess) {
95       // out
96       buf[done] = '\0';
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
100         // the safe side.
101         char *buf_ = realloc(buf, done+1);
102         if (buf_) buf = buf_;
103       }
104       if (len_out) *len_out = done;
105       return buf;
106     }
107     size_guess<<=1; // try two times the buffer size
108   }
109 }
110
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);
115   int errno_ = errno;
116   close(fd);
117   errno = errno_;
118   return res;
119 }
120
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);
125   
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) {
132     errno = write_errno;
133     return 1;
134   }
135   if (close_res) return 1;
136   return 0;
137 }