/* ** From Whiskey :-) */ char *read_whole_file(int file_descriptor) { size_t block_size; char *string; size_t length; ssize_t read_char_count; string = NULL; length = 0; block_size = 1024 * 1024; while ((string = egc_realloc(string, length + block_size + 1)) != NULL) { read_char_count = read(file_descriptor, string + length, block_size); if (read_char_count > -1) string[read_char_count] = '\0'; length += read_char_count; if (read_char_count == -1 || string_get_length(string) != (int)length) return (NULL); if (read_char_count != (ssize_t)block_size) break ; } return (string); }
static void test_realloc(void) { char *data; data = egc_realloc(NULL, 10); check_allocated_block(data, 10); data[0] = 'a'; data = egc_realloc(data, 1); ASSERT(data[0] == 'a'); data = egc_realloc(data, 1); ASSERT(data[0] == 'a'); data = egc_realloc(data, 0); data = egc_realloc(data, 1); ASSERT(!data[0]); data[0] = 'a'; data = egc_realloc(data, 100); ASSERT(data[0] == 'a'); ASSERT(is_zero(data + 1, 99)); data[99] = 'b'; data = egc_realloc(data, 100); ASSERT(data[0] == 'a'); ASSERT(is_zero(data + 1, 98)); ASSERT(data[99] == 'b'); }