diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-16 22:00:14 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-16 22:00:14 +0200 |
| commit | 13d9e4df0f4555079d16313a54c6035e22d575a6 (patch) | |
| tree | a0a80aedf61a892bf90daf02690de9335e6fc7a8 /source/base/base_os.h | |
| parent | 8ea6a3c8621287d11296b8300029f32a27743d9a (diff) | |
feature(base): attempt at generating a random number
Diffstat (limited to 'source/base/base_os.h')
| -rw-r--r-- | source/base/base_os.h | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h index b87c946..536119e 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h | |||
| @@ -1,9 +1,6 @@ | |||
| 1 | #ifndef BASE_OS_H | 1 | #ifndef BASE_OS_H |
| 2 | #define BASE_OS_H | 2 | #define BASE_OS_H |
| 3 | 3 | ||
| 4 | #ifdef OS_LINUX | ||
| 5 | |||
| 6 | |||
| 7 | #define STDIN_FD 0 | 4 | #define STDIN_FD 0 |
| 8 | #define STDOUT_FD 1 | 5 | #define STDOUT_FD 1 |
| 9 | #define STDERR_FD 2 | 6 | #define STDERR_FD 2 |
| @@ -22,12 +19,11 @@ load_file(mem_arena *arena, const char *path) | |||
| 22 | s32 file = open(path, O_RDONLY); | 19 | s32 file = open(path, O_RDONLY); |
| 23 | if(file == -1) | 20 | if(file == -1) |
| 24 | { | 21 | { |
| 25 | warn("fialed to open file. path could be invalid"); return (string8){0}; | 22 | return (string8){0}; |
| 26 | } | 23 | } |
| 27 | 24 | ||
| 28 | if(fstat(file, &sbuf) == -1) | 25 | if(fstat(file, &sbuf) == -1) |
| 29 | { | 26 | { |
| 30 | warn("error: fstat failed"); | ||
| 31 | close(file); | 27 | close(file); |
| 32 | return (string8){0}; | 28 | return (string8){0}; |
| 33 | } | 29 | } |
| @@ -52,7 +48,6 @@ write_file(const char *path, string8 data) | |||
| 52 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | 48 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 53 | if(file == -1) | 49 | if(file == -1) |
| 54 | { | 50 | { |
| 55 | warn("failed to open file for writing. path could be invalid"); | ||
| 56 | return (string8){0}; | 51 | return (string8){0}; |
| 57 | } | 52 | } |
| 58 | 53 | ||
| @@ -62,7 +57,6 @@ write_file(const char *path, string8 data) | |||
| 62 | s64 err = write(file, data.data + written, data.size - written); | 57 | s64 err = write(file, data.data + written, data.size - written); |
| 63 | if(err == -1) | 58 | if(err == -1) |
| 64 | { | 59 | { |
| 65 | warn("write syscall failed"); | ||
| 66 | close(file); | 60 | close(file); |
| 67 | return (string8){0}; | 61 | return (string8){0}; |
| 68 | } | 62 | } |
| @@ -74,8 +68,8 @@ write_file(const char *path, string8 data) | |||
| 74 | return result; | 68 | return result; |
| 75 | } | 69 | } |
| 76 | 70 | ||
| 77 | #define os_write(void const *buf, u64 count) _os_write(STDIN, buf, count) | 71 | #define os_write(buf, count) _os_write(STDIN_FD, buf, count) |
| 78 | #define os_read(void const *buf, u64 count) _os_read(STDIN, buf, count) | 72 | #define os_read(buf, count) _os_read(STDIN_FD, buf, count) |
| 79 | 73 | ||
| 80 | internal s64 | 74 | internal s64 |
| 81 | _os_write(s32 fd, void const *buf, u64 count) | 75 | _os_write(s32 fd, void const *buf, u64 count) |
| @@ -90,26 +84,26 @@ _os_read(s32 fd, void *buf, u64 count) | |||
| 90 | } | 84 | } |
| 91 | 85 | ||
| 92 | internal void | 86 | internal void |
| 93 | print_s8(string8 s) | 87 | log_s8(string8 s) |
| 94 | { | 88 | { |
| 95 | os_write(s.data, s.size); | 89 | os_write(s.data, s.size); |
| 96 | } | 90 | } |
| 97 | 91 | ||
| 98 | internal void | 92 | internal void |
| 99 | print(const char *str) | 93 | _log(const char *str) |
| 100 | { | 94 | { |
| 101 | s32 len = 0; | 95 | s32 len = 0; |
| 102 | while (str[len]) len++; | 96 | while (str[len]) len++; |
| 103 | os_write(STDOUT_FILENO, str, len); | 97 | os_write(str, len); |
| 104 | 98 | ||
| 105 | } | 99 | } |
| 106 | 100 | ||
| 107 | internal void | 101 | internal void |
| 108 | write_string(s32 fd, const char *str) | 102 | write_string(const char *str) |
| 109 | { | 103 | { |
| 110 | s32 len = 0; | 104 | s32 len = 0; |
| 111 | while (str[len]) len++; | 105 | while (str[len]) len++; |
| 112 | os_write(fd, str, len); | 106 | os_write(str, len); |
| 113 | } | 107 | } |
| 114 | 108 | ||
| 115 | internal void | 109 | internal void |
| @@ -128,5 +122,18 @@ write_int(s32 num) | |||
| 128 | write(STDERR_FILENO, &digit, 1); | 122 | write(STDERR_FILENO, &digit, 1); |
| 129 | } | 123 | } |
| 130 | 124 | ||
| 131 | #endif | 125 | internal inline void |
| 126 | sleep_ms(long ms) | ||
| 127 | { | ||
| 128 | struct timespec ts; | ||
| 129 | ts.tv_sec = ms / 1000; | ||
| 130 | ts.tv_nsec = (ms % 1000) * 1000000L; | ||
| 131 | |||
| 132 | while (nanosleep(&ts, &ts)) | ||
| 133 | { | ||
| 134 | NULL; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 132 | #endif | 139 | #endif |
