Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */
| 147 | |
| 148 | /* Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */ |
| 149 | static char *read_file_alloc(const char *path, size_t *out_len) { |
| 150 | FILE *fp = cbm_fopen(path, "rb"); |
| 151 | if (!fp) { |
| 152 | return NULL; |
| 153 | } |
| 154 | (void)fseek(fp, 0, SEEK_END); |
| 155 | long sz = ftell(fp); |
| 156 | if (sz <= 0) { |
| 157 | (void)fclose(fp); |
| 158 | return NULL; |
| 159 | } |
| 160 | (void)fseek(fp, 0, SEEK_SET); |
| 161 | char *buf = malloc((size_t)sz); |
| 162 | if (!buf) { |
| 163 | (void)fclose(fp); |
| 164 | return NULL; |
| 165 | } |
| 166 | size_t rd = fread(buf, ART_NUL, (size_t)sz, fp); |
| 167 | (void)fclose(fp); |
| 168 | if ((long)rd != sz) { |
| 169 | free(buf); |
| 170 | return NULL; |
| 171 | } |
| 172 | *out_len = (size_t)sz; |
| 173 | return buf; |
| 174 | } |
| 175 | |
| 176 | /* Write buffer to file atomically (write to tmp, rename). Returns 0 on success. */ |
| 177 | static int write_file_atomic(const char *path, const char *data, size_t len, |
no test coverage detected