-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmca_region.c
42 lines (37 loc) · 1.11 KB
/
mca_region.c
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
#include "mca_region.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static region_header_t region_header;
void resolve_linux_home(const char *filename, char *dest)
{
#ifdef __linux__
if (*filename == '~') {
sprintf(dest, "%s%s", getenv("HOME"), filename + 1);
} else {
sprintf(dest, "%s", filename);
}
#else
sprintf(dest, "%s", filename);
#endif
}
region_header_t *read_region_header(const char *file)
{
char resolved_path[8192];
resolve_linux_home(file, resolved_path);
FILE* region_file = fopen(resolved_path, "r");
if (errno) {
fprintf(stderr, "%s %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
region_header.locations = calloc(N_LOCATIONS_IN_REGION, sizeof(location_t));
size_t ret = fread(region_header.locations, sizeof(location_t), N_LOCATIONS_IN_REGION, region_file);
fclose(region_file);
if (ret != N_LOCATIONS_IN_REGION) {
fprintf(stderr, "fread() failed: read %zu, expected %u\n", ret, N_LOCATIONS_IN_REGION);
exit(EXIT_FAILURE);
}
return ®ion_header;
}