char * sol_util_load_file_fd_string(int fd, size_t *size) { int saved_errno; size_t size_read; char *data = NULL; struct sol_buffer *buffer = NULL; buffer = sol_util_load_file_raw(fd); if (!buffer) { data = strdup(""); size_read = 1; } else { buffer->flags = SOL_BUFFER_FLAGS_DEFAULT; if (sol_buffer_ensure_nul_byte(buffer) < 0) goto err; data = sol_buffer_steal(buffer, &size_read); if (!data) goto err; } sol_buffer_free(buffer); if (size) *size = size_read; return data; err: saved_errno = errno; free(data); sol_buffer_free(buffer); errno = saved_errno; if (size) *size = 0; return NULL; }
SOL_API struct sol_buffer * sol_util_load_file_fd_raw(int fd) { struct sol_buffer *buf; int r; buf = sol_buffer_new(); SOL_NULL_CHECK(buf, NULL); buf->flags |= SOL_BUFFER_FLAGS_NO_NUL_BYTE; r = sol_util_load_file_fd_buffer(fd, buf); SOL_INT_CHECK_GOTO(r, < 0, err_exit); return buf; err_exit: sol_buffer_free(buf); return NULL; }
struct sol_buffer * sol_util_load_file_raw(const int fd) { struct stat st; ssize_t ret; struct sol_buffer *buffer; if (fd < 0) return NULL; buffer = sol_buffer_new(); SOL_NULL_CHECK(buffer, NULL); buffer->flags = SOL_BUFFER_FLAGS_NO_NUL_BYTE; if (fstat(fd, &st) >= 0 && st.st_size) { ret = sol_util_fill_buffer(fd, buffer, st.st_size); } else { do { ret = sol_util_fill_buffer(fd, buffer, CHUNK_SIZE); } while (ret > 0); } if (ret < 0) goto err; if (sol_buffer_trim(buffer) < 0) goto err; return buffer; err: sol_buffer_free(buffer); return NULL; }