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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
void platform_free_buffer(Platform_Buffer *buff)
{
free(buff->mem);
buff->size = 0;
}
bool platform_read_file(Platform_Buffer *buff, const char *pathname)
{
int fd = open(pathname, O_RDONLY);
if (fd == -1)
{
printf("error reading file %s\n", pathname);
return false;
}
struct stat statbuff;
if (fstat(fd, &statbuff) == -1)
{
printf("cant fstat file %s\n", pathname);
close(fd);
return false;
}
void *file_content = malloc(statbuff.st_size);
if (!file_content)
{
printf("error: out of memory\n");
close(fd);
return false;
}
ssize_t bytes_read = read(fd, file_content, statbuff.st_size);
if (bytes_read != statbuff.st_size)
{
printf("error: only read %ld/%ld bytes from file %s\n", bytes_read, statbuff.st_size, pathname);
close(fd);
free(file_content);
return false;
}
buff->size = statbuff.st_size;
buff->mem = file_content;
return true;
}
bool platform__change_to_runtime_dir()
{
// prepare home
char *home = getenv("HOME");
int home_len = strlen(home);
if (home_len == 0 || home_len >= 4096) {
printf("home dir invalid\n");
return false;
}
// prepare subdir
// Note: it might never end with '/', but let's be sure
bool home_ends_with_slash = home[home_len-1] == '/';
const char *subdir = home_ends_with_slash ? ".local/share/florilia" : "/.local/share/florilia";
int subdir_len = strlen(subdir);
if (home_len + subdir_len >= 4096) {
printf("home/subdir has invalid length\n");
return false;
}
// set run_tree
char run_tree[4096];
memcpy(run_tree, home, home_len);
strcpy(run_tree + home_len, subdir);
// change to run_tree
int changed = chdir(run_tree);
if (changed == -1) {
perror("chdir");
return false;
}
return true;
}
|