summaryrefslogtreecommitdiff
path: root/source/base/base_os.h
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2026-04-13 15:33:05 +0200
committernasr <nsrddyn@gmail.com>2026-04-13 15:36:24 +0200
commit9d09d66a273f68fae7efb71504bf40c664b91983 (patch)
tree41a46c52a01338bf22d5f3ebdf0bb27dc3d33cc1 /source/base/base_os.h
feature(main): init
feature(main): init feature(main): init feature(main): init
Diffstat (limited to 'source/base/base_os.h')
-rw-r--r--source/base/base_os.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h
new file mode 100644
index 0000000..388f665
--- /dev/null
+++ b/source/base/base_os.h
@@ -0,0 +1,67 @@
1#ifndef BASE_OS_H
2#define BASE_OS_H
3
4internal string8
5load_file(mem_arena *arena, const char *path)
6{
7 string8 result = {0};
8 struct stat sbuf = {0};
9
10 s32 file = open(path, O_RDONLY);
11 if(file == -1)
12 {
13 warn("fialed to open file. path could be invalid");
14 return (string8){0};
15 }
16
17 if(fstat(file, &sbuf) == -1)
18 {
19 warn("error: fstat failed");
20 close(file);
21 return (string8){0};
22 }
23
24
25 result = PushString(arena, sbuf.st_size);
26
27 result.size = (u64)sbuf.st_size;
28 if(result.size != 0)
29 {
30 result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0);
31 }
32
33 close(file);
34 return result;
35}
36
37internal string8
38write_file(const char *path, string8 data)
39{
40
41 string8 result = {0};
42 s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
43 if(file == -1)
44 {
45 warn("failed to open file for writing. path could be invalid");
46 return (string8){0};
47 }
48
49 u64 written = 0;
50 while(written < data.size)
51 {
52 s64 err = write(file, data.data + written, data.size - written);
53 if(err == -1)
54 {
55 warn("write syscall failed");
56 close(file);
57 return (string8){0};
58 }
59 written += err;
60 }
61
62 close(file);
63 result = data;
64 return result;
65}
66
67#endif /* BASE_OS_H */