summaryrefslogtreecommitdiff
path: root/prb_linux.h
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-06-15 15:28:45 +0500
committerpryazha <pryadeiniv@mail.ru>2025-06-15 15:28:45 +0500
commite7f67b450d8034b532101445035d3b199e702621 (patch)
treed1193a6044d75800266cec11776358be7270cf8b /prb_linux.h
parent92850237f42cecfeba519bc15f7f5bb7a76cde5f (diff)
windows?
Diffstat (limited to 'prb_linux.h')
-rw-r--r--prb_linux.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/prb_linux.h b/prb_linux.h
new file mode 100644
index 0000000..3acc229
--- /dev/null
+++ b/prb_linux.h
@@ -0,0 +1,69 @@
+#include <sys/mman.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+void *sys_alloc(u64 length)
+{
+ assert(length);
+ void *result = mmap(0, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+ return result;
+}
+
+void sys_free(void *memory, u64 length)
+{
+ assert(memory);
+ assert(length);
+ munmap(memory, length);
+}
+
+u64 sys_read_file(arena_t *arena, char **buffer, const char *filename)
+{
+ FILE *file = fopen(filename, "rb");
+ if (!file)
+ return 0;
+
+ if (fseek(file, 0, SEEK_END) == -1)
+ goto error;
+ long len = ftell(file);
+ if (!len || (len == -1))
+ goto error;
+ if (fseek(file, 0, SEEK_SET) == -1)
+ goto error;
+
+ *buffer = push_arena(arena, len + 1);
+ if (!fread(*buffer, 1, len, file)) {
+ pop_arena(arena, len + 1);
+ *buffer = 0;
+ goto error;
+ }
+ (*buffer)[len] = 0;
+ fclose(file);
+
+ return len;
+error:
+ fclose(file);
+ *buffer = 0;
+ return 0;
+}
+
+char *sys_getbindir(arena_t *arena)
+{
+ char path[MAX_PATH];
+ i64 len = readlink("/proc/self/exe", path, MAX_PATH - 1);
+ if (len <= 0)
+ return 0;
+
+ path[len] = 0;
+
+ char *dir = strrchr(path, '/');
+ assert(dir);
+ assert(dir > path);
+
+ len = dir - path;
+ dir = push_arena(arena, len + 1);
+ prb_memmove(dir, path, len);
+ dir[len] = 0;
+
+ return dir;
+}