-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.h
More file actions
31 lines (30 loc) · 844 Bytes
/
file.h
File metadata and controls
31 lines (30 loc) · 844 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
//
#include <stdio.h>
#include <malloc.h>
#include <sys/stat.h>
#include <string.h>
static size_t read_file(FILE* fp, char** output) {
size_t smart_size, count;
size_t length = 0;
//make it faster
if (fp == stdin) {
smart_size = stdin->_bufsiz;
}
else { //unstable for stdin!
struct stat filestats;
int fd = fileno(fp);
fstat(fd, &filestats);
smart_size = filestats.st_size + 1; // +1 to get EOF, BIG file
}
//
*output = calloc(1, 1); //just in case
while (!feof(fp)) {
*output = realloc(*output, length + smart_size + 1);
count = fread(*output + length, 1, smart_size, fp);
memset(*output + length + count, 0, 1); // append 0
length = strlen(*output);
}
*output = realloc(*output, length + 1);
//
return length;
}