1 // Copyright (2013) Jann Horn <jann@thejh.net>
2 // This code is licensed under the AGPLv3.
4 HEADER #include <sys/types.h>
15 // Wrapper for `read` that retries on partial reads.
16 // If last_res is non-NULL, it will be filled with the
17 // result of the last read() call (but all positive numbers
18 // become 1). In other words:
21 // - 1: no problems occured
22 PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) {
25 while (done < count) {
26 ssize_t part_res = read(fd, buf+done, count-done);
28 if (last_res) *last_res = part_res;
29 if (done) return done;
34 if (last_res) *last_res = 1;
38 // Wrapper for `write` that retries on partial writes.
39 // If last_res is non-NULL, it will be filled with the
40 // result of the last write() call (but all positive numbers
41 // become 1). In other words:
44 // - 1: no problems occured
45 PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
48 while (done < count) {
49 ssize_t part_res = write(fd, buf+done, count-done);
51 if (last_res) *last_res = part_res;
52 if (done) return done;
57 if (last_res) *last_res = 1;
61 // Read all data from the given file descriptor. Tries fstat()+read()
62 // first, but if fstat() doesn't work, falls back to multiple read()s.
63 // Specify JH_NO_STAT to prevent the fstat() call.
64 // The return value is a malloc'd buffer.
65 // The buffer will be null-terminated, so you can read text files with
66 // this and can specify len_out as NULL.
67 PUBLIC_CONST JH_SLURP_NO_STAT 1
68 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
69 PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
72 // Let's just guess that the file is 1023 bytes. Will become 1024 with
74 size_t size_guess = 1023;
75 bool trusted_guess = false; /* can we rely on the guess? */
77 // If we can determine the exact size, we don't have to guess. So try
78 // to determine the exact size.
79 if (!(flags&JH_SLURP_NO_STAT)) {
81 if (fstat(fd, &st) == 0) {
83 size_guess = st.st_size;
93 buf = realloc(buf, size_guess+1);
94 if (buf == NULL) return NULL;
96 ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
97 if (last_res == -1) { errno_=errno; free(buf); errno=errno_; return NULL; }
99 if (last_res == 0 || trusted_guess) {
102 if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
103 // Well, it'd be weird if shrinking could fail... but meh, I can't find
104 // an explicit statement about this being disallowed, so try staying on
106 char *buf_ = realloc(buf, done+1);
107 if (buf_) buf = buf_;
109 if (len_out) *len_out = done;
112 size_guess<<=1; // try two times the buffer size
116 PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
117 int fd = open(path, O_RDONLY|O_CLOEXEC);
118 if (fd == -1) return NULL;
119 char *res = slurp_fd(fd, len_out, flags);
126 // Write data into a file. len can be -1; that means that
127 // the real length is strlen(buf).
128 PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
129 if (len == -1) len = strlen(buf);
131 int fd = open(path, open_flags|O_CLOEXEC, 0777);
132 if (fd == -1) return 1;
133 ssize_t write_res = write_nointr(fd, buf, len, NULL);
134 int write_errno = errno;
135 int close_res = close(fd);
136 if (write_res != len) {
140 if (close_res) return 1;
144 PUBLIC_FN void *mmap_file(void *addr, size_t length, int prot, int flags, char *path, off_t offset) {
145 assert((flags&MAP_ANONYMOUS) == 0);
146 assert(prot&(PROT_EXEC|PROT_READ|PROT_WRITE));
147 int open_flags = O_CLOEXEC;
148 if ((prot & (PROT_READ|PROT_EXEC)) && !(prot&PROT_WRITE)) open_flags |= O_RDONLY;
149 else if ((prot & (PROT_READ|PROT_EXEC))) open_flags |= O_RDWR;
150 else open_flags |= O_WRONLY;
151 int fd = open(path, open_flags);
152 if (fd == -1) return NULL;
153 void *r = mmap(addr, length, prot, flags, fd, offset);
157 return (r==MAP_FAILED) ? NULL : r;