summaryrefslogtreecommitdiff
path: root/source/base/base_os.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/base/base_os.h')
-rw-r--r--source/base/base_os.h96
1 files changed, 87 insertions, 9 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h
index 388f665..82aa70d 100644
--- a/source/base/base_os.h
+++ b/source/base/base_os.h
@@ -1,28 +1,35 @@
1#ifndef BASE_OS_H 1#ifndef BASE_OS_H
2#define BASE_OS_H 2#define BASE_OS_H
3 3
4#define STDIN_FD 0
5#define STDOUT_FD 1
6#define STDERR_FD 2
7
8#endif /* BASE_OS_H */
9
10#ifdef BASE_IMPLEMENTATION
11
4internal string8 12internal string8
5load_file(mem_arena *arena, const char *path) 13load_file(mem_arena *arena, const char *path)
6{ 14{
7 string8 result = {0}; 15 string8 result = {0};
8 struct stat sbuf = {0}; 16 struct stat sbuf = {0};
9 17
18 // TODO(nasr): abstract this to a platform layer
10 s32 file = open(path, O_RDONLY); 19 s32 file = open(path, O_RDONLY);
11 if(file == -1) 20 if(file == -1)
12 { 21 {
13 warn("fialed to open file. path could be invalid");
14 return (string8){0}; 22 return (string8){0};
15 } 23 }
16 24
17 if(fstat(file, &sbuf) == -1) 25 if(fstat(file, &sbuf) == -1)
18 { 26 {
19 warn("error: fstat failed");
20 close(file); 27 close(file);
21 return (string8){0}; 28 return (string8){0};
22 } 29 }
23 30
24 31
25 result = PushString(arena, sbuf.st_size); 32 result = PushString8(arena, sbuf.st_size);
26 33
27 result.size = (u64)sbuf.st_size; 34 result.size = (u64)sbuf.st_size;
28 if(result.size != 0) 35 if(result.size != 0)
@@ -37,31 +44,102 @@ load_file(mem_arena *arena, const char *path)
37internal string8 44internal string8
38write_file(const char *path, string8 data) 45write_file(const char *path, string8 data)
39{ 46{
40
41 string8 result = {0}; 47 string8 result = {0};
42 s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 48 s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
43 if(file == -1) 49 if(file == -1)
44 { 50 {
45 warn("failed to open file for writing. path could be invalid");
46 return (string8){0}; 51 return (string8){0};
47 } 52 }
48 53
49 u64 written = 0; 54 u64 written = 0;
50 while(written < data.size) 55 while(written < data.size)
51 { 56 {
52 s64 err = write(file, data.data + written, data.size - written); 57 s64 err = write(file, data.data + written, data.size - written);
53 if(err == -1) 58 if(err == -1)
54 { 59 {
55 warn("write syscall failed");
56 close(file); 60 close(file);
57 return (string8){0}; 61 return (string8){0};
58 } 62 }
59 written += err; 63 written += err;
60 } 64 }
61 65
62 close(file); 66 close(file);
63 result = data; 67 result = data;
64 return result; 68 return result;
65} 69}
66 70
67#endif /* BASE_OS_H */ 71#define os_write(buf, count) _os_write(STDIN_FD, buf, count)
72#define os_read(buf, count) _os_read(STDIN_FD, buf, count)
73
74internal s64
75_os_write(s32 fd, void const *buf, u64 count)
76{
77 return syscall(SYS_write, fd, buf, count);
78}
79
80internal s64
81_os_read(s32 fd, void *buf, u64 count)
82{
83 return syscall(SYS_read, fd, buf, count);
84}
85
86internal void
87log_s8(string8 s)
88{
89 os_write(s.data, s.size);
90}
91
92#if 1
93internal void
94_log(const char *str)
95{
96#ifdef BASE_LOGGING
97 s32 len = 0;
98 while (str[len]) len++;
99 os_write(str, len);
100#else
101 unused(str);
102#endif
103
104}
105#endif
106
107internal void
108write_string(const char *str)
109{
110 s32 len = 0;
111 while (str[len]) len++;
112 os_write(str, len);
113}
114
115internal void
116write_int(s32 num)
117{
118
119 if (num < 0)
120 {
121 write(STDERR_FILENO, "-", 1);
122 num = -num;
123 }
124 if (num >= 10) write_int(num / 10);
125
126 char digit = '0' + (num % 10);
127
128 write(STDERR_FILENO, &digit, 1);
129}
130
131internal inline void
132sleep_ms(long ms)
133{
134 struct timespec ts;
135 ts.tv_sec = ms / 1000;
136 ts.tv_nsec = (ms % 1000) * 1000000L;
137
138 while (nanosleep(&ts, &ts))
139 {
140 NULL;
141 }
142}
143
144
145#endif