1#include <stdio.h>
 2#include <stdlib.h>
 3
 4#include "file.h"
 5
 6struct FileContent read_entire_file(const char *file_path) {
 7	struct FileContent file_data;
 8	file_data.content = NULL;
 9	file_data.count = 0;
10
11	FILE *file = fopen(file_path, "rb");
12	if (file == NULL) {
13		perror("Error opening file");
14		return file_data;
15	}
16
17	fseek(file, 0, SEEK_END);
18	long raw_size = ftell(file);
19	fseek(file, 0, SEEK_SET);
20
21	if (raw_size == -1) {
22		perror("Error getting file size");
23		fclose(file);
24		return file_data;
25	}
26
27	size_t file_size = (size_t)raw_size;
28	char *content = (char *)malloc(file_size + 1);
29	if (content == NULL) {
30		perror("Error allocating memory");
31		fclose(file);
32		return file_data;
33	}
34
35	size_t bytes_read = fread(content, 1, file_size, file);
36	if (bytes_read != file_size) {
37		perror("Error reading file");
38		free(content);
39		fclose(file);
40		return file_data;
41	}
42
43	content[file_size] = '\0';
44	file_data.content = content;
45	file_data.count = file_size;
46
47	fclose(file);
48	return file_data;
49}