aboutsummaryrefslogtreecommitdiff
path: root/src/graveyard/lin_file.c
diff options
context:
space:
mode:
authorfschildt <florian.schildt@protonmail.com>2025-08-22 15:23:11 +0200
committerfschildt <florian.schildt@protonmail.com>2025-08-22 15:23:11 +0200
commit2050c0e0576f05156f192aa4caf48834d2f28b14 (patch)
treeee58bd35b0df0a1bacfbc9700ed99ce80c99294e /src/graveyard/lin_file.c
first commitHEADmaster
Diffstat (limited to 'src/graveyard/lin_file.c')
-rw-r--r--src/graveyard/lin_file.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/graveyard/lin_file.c b/src/graveyard/lin_file.c
new file mode 100644
index 0000000..5f0e003
--- /dev/null
+++ b/src/graveyard/lin_file.c
@@ -0,0 +1,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;
+}