-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.c
More file actions
46 lines (36 loc) · 1.07 KB
/
util.c
File metadata and controls
46 lines (36 loc) · 1.07 KB
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
#include "util.h"
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
double seconds() {
struct timeval tv;
gettimeofday(&tv, NULL); // The null is for timezone information.
return tv.tv_sec + tv.tv_usec/1000000.0;
}
// Return the size of the given open file, which must be seekable
// (meaning that it's an ordinary file opened with fopen() - you can't
// pass stdin to this). Also, this must be called just after opening
// the file, as it resets the read position to the initial spot in the
// file.
long file_size(FILE* f) {
fseek(f, 0, SEEK_END); // Seek to end of file.
long size = ftell(f); // At which offset is that?
fseek(f, 0, SEEK_SET); // Now back to the start of the file.
return size;
}
unsigned char* read_file(const char* fname, long *size) {
FILE *in = fopen(fname, "r");
if (in == NULL) {
return NULL;
}
*size = file_size(in);
unsigned char *bytes = malloc(*size);
int read = fread(bytes, sizeof(unsigned char), *size, in);
fclose(in);
if (read != *size) {
free(bytes);
return NULL;
} else {
return bytes;
}
}