-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
37 lines (30 loc) · 683 Bytes
/
Copy pathfile.c
File metadata and controls
37 lines (30 loc) · 683 Bytes
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
#include "file.h"
#include <stdio.h>
#include <stdlib.h>
static size_t file_length(FILE* file)
{
size_t cur = ftell(file);
fseek(file, 0, SEEK_END);
size_t result = ftell(file);
fseek(file, cur, SEEK_SET);
return result;
}
file_io_result_t read_file(const char* filename, unsigned char** buffer, size_t* size)
{
FILE* file = fopen(filename, "rb");
if (file == NULL)
return FILE_IO_NOT_FOUND;
*size = file_length(file);
*buffer = malloc(*size + 1);
(*buffer)[*size] = 0;
fread(*buffer, 1, *size, file);
if (ferror(file)) {
free(*buffer);
*buffer = NULL;
*size = 0;
fclose(file);
return FILE_IO_READ_FAILURE;
}
fclose(file);
return FILE_IO_SUCCESS;
}