summaryrefslogtreecommitdiff
path: root/file.c
blob: 2c04ff6017ad09575937097c12ceb0f962dd6b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>

#include "file.h"

struct FileContent read_entire_file(const char *file_path) {
	struct FileContent file_data;
	file_data.content = NULL;
	file_data.count = 0;

	FILE *file = fopen(file_path, "rb");
	if (file == NULL) {
		perror("Error opening file");
		return file_data;
	}

	fseek(file, 0, SEEK_END);
	long raw_size = ftell(file);
	fseek(file, 0, SEEK_SET);

	if (raw_size == -1) {
		perror("Error getting file size");
		fclose(file);
		return file_data;
	}

	size_t file_size = (size_t)raw_size;
	char *content = (char *)malloc(file_size + 1);
	if (content == NULL) {
		perror("Error allocating memory");
		fclose(file);
		return file_data;
	}

	size_t bytes_read = fread(content, 1, file_size, file);
	if (bytes_read != file_size) {
		perror("Error reading file");
		free(content);
		fclose(file);
		return file_data;
	}

	content[file_size] = '\0';
	file_data.content = content;
	file_data.count = file_size;

	fclose(file);
	return file_data;
}