os_linux.odin 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. OS :: "linux";
  7. Handle :: distinct i32;
  8. File_Time :: distinct u64;
  9. Errno :: distinct i32;
  10. O_RDONLY :: 0x00000;
  11. O_WRONLY :: 0x00001;
  12. O_RDWR :: 0x00002;
  13. O_CREATE :: 0x00040;
  14. O_EXCL :: 0x00080;
  15. O_NOCTTY :: 0x00100;
  16. O_TRUNC :: 0x00200;
  17. O_NONBLOCK :: 0x00800;
  18. O_APPEND :: 0x00400;
  19. O_SYNC :: 0x01000;
  20. O_ASYNC :: 0x02000;
  21. O_CLOEXEC :: 0x80000;
  22. SEEK_SET :: 0;
  23. SEEK_CUR :: 1;
  24. SEEK_END :: 2;
  25. SEEK_DATA :: 3;
  26. SEEK_HOLE :: 4;
  27. SEEK_MAX :: SEEK_HOLE;
  28. // NOTE(zangent): These are OS specific!
  29. // Do not mix these up!
  30. RTLD_LAZY :: 0x001;
  31. RTLD_NOW :: 0x002;
  32. RTLD_BINDING_MASK :: 0x3;
  33. RTLD_GLOBAL :: 0x100;
  34. // "Argv" arguments converted to Odin strings
  35. args := _alloc_command_line_arguments();
  36. _File_Time :: struct {
  37. seconds: i64,
  38. nanoseconds: i32,
  39. reserved: i32,
  40. }
  41. // Translated from
  42. // https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/jb-dev/sysroot/usr/include/bits/stat.h
  43. // Validity is not guaranteed.
  44. Stat :: struct {
  45. device_id: u64, // ID of device containing file
  46. serial: u64, // File serial number
  47. nlink: u32, // Number of hard links
  48. mode: u32, // Mode of the file
  49. uid: u32, // User ID of the file's owner
  50. gid: u32, // Group ID of the file's group
  51. _padding: i32, // 32 bits of padding
  52. rdev: u64, // Device ID, if device
  53. size: i64, // Size of the file, in bytes
  54. block_size: i64, // Optimal bllocksize for I/O
  55. blocks: i64, // Number of 512-byte blocks allocated
  56. last_access: _File_Time, // Time of last access
  57. modified: _File_Time, // Time of last modification
  58. status_change: _File_Time, // Time of last status change
  59. _reserve1,
  60. _reserve2,
  61. _reserve3: i64,
  62. serial_numbe: u64, // File serial number..? Maybe.
  63. _reserve4: i64,
  64. };
  65. // File type
  66. S_IFMT :: 0170000; // Type of file mask
  67. S_IFIFO :: 0010000; // Named pipe (fifo)
  68. S_IFCHR :: 0020000; // Character special
  69. S_IFDIR :: 0040000; // Directory
  70. S_IFBLK :: 0060000; // Block special
  71. S_IFREG :: 0100000; // Regular
  72. S_IFLNK :: 0120000; // Symbolic link
  73. S_IFSOCK :: 0140000; // Socket
  74. // File mode
  75. // Read, write, execute/search by owner
  76. S_IRWXU :: 0000700; // RWX mask for owner
  77. S_IRUSR :: 0000400; // R for owner
  78. S_IWUSR :: 0000200; // W for owner
  79. S_IXUSR :: 0000100; // X for owner
  80. // Read, write, execute/search by group
  81. S_IRWXG :: 0000070; // RWX mask for group
  82. S_IRGRP :: 0000040; // R for group
  83. S_IWGRP :: 0000020; // W for group
  84. S_IXGRP :: 0000010; // X for group
  85. // Read, write, execute/search by others
  86. S_IRWXO :: 0000007; // RWX mask for other
  87. S_IROTH :: 0000004; // R for other
  88. S_IWOTH :: 0000002; // W for other
  89. S_IXOTH :: 0000001; // X for other
  90. S_ISUID :: 0004000; // Set user id on execution
  91. S_ISGID :: 0002000; // Set group id on execution
  92. S_ISVTX :: 0001000; // Directory restrcted delete
  93. S_ISLNK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFLNK;
  94. S_ISREG :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFREG;
  95. S_ISDIR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFDIR;
  96. S_ISCHR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFCHR;
  97. S_ISBLK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFBLK;
  98. S_ISFIFO :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFIFO;
  99. S_ISSOCK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFSOCK;
  100. F_OK :: 0; // Test for file existance
  101. X_OK :: 1; // Test for execute permission
  102. W_OK :: 2; // Test for write permission
  103. R_OK :: 4; // Test for read permission
  104. foreign libc {
  105. @(link_name="open") _unix_open :: proc(path: cstring, mode: int) -> Handle ---;
  106. @(link_name="close") _unix_close :: proc(fd: Handle) -> i32 ---;
  107. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int ---;
  108. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int ---;
  109. @(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 ---;
  110. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  111. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^Stat) -> i32 ---;
  112. @(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> i32 ---;
  113. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
  114. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
  115. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  116. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
  117. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  118. @(link_name="exit") _unix_exit :: proc(status: int) -> ! ---;
  119. }
  120. foreign dl {
  121. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
  122. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  123. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
  124. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  125. }
  126. // TODO(zangent): Change this to just `open` when Bill fixes overloading.
  127. open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
  128. cstr := strings.new_cstring(path);
  129. handle := _unix_open(cstr, mode);
  130. delete(cstr);
  131. if(handle == -1) {
  132. return 0, 1;
  133. }
  134. return handle, 0;
  135. }
  136. // NOTE(zangent): This is here for compatability reasons. Should this be here?
  137. open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
  138. return open_simple(path, mode);
  139. }
  140. close :: proc(fd: Handle) {
  141. _unix_close(fd);
  142. }
  143. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  144. sz := _unix_read(fd, &data[0], len(data));
  145. return sz, 0;
  146. }
  147. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  148. sz := _unix_write(fd, &data[0], len(data));
  149. return sz, 0;
  150. }
  151. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  152. res := _unix_seek(fd, offset, i32(whence));
  153. return res, 0;
  154. }
  155. file_size :: proc(fd: Handle) -> (i64, Errno) {
  156. prev, _ := seek(fd, 0, SEEK_CUR);
  157. size, err := seek(fd, 0, SEEK_END);
  158. seek(fd, prev, SEEK_SET);
  159. return size, err;
  160. }
  161. // NOTE(bill): Uses startup to initialize it
  162. stdin: Handle = 0;
  163. stdout: Handle = 1;
  164. stderr: Handle = 2;
  165. /* TODO(zangent): Implement these!
  166. last_write_time :: proc(fd: Handle) -> File_Time {}
  167. last_write_time_by_name :: proc(name: string) -> File_Time {}
  168. */
  169. stat :: inline proc(path: string) -> (Stat, int) {
  170. cstr := strings.new_cstring(path);
  171. defer delete(cstr);
  172. s: Stat;
  173. ret_int := _unix_stat(cstr, &s);
  174. return s, int(ret_int);
  175. }
  176. access :: inline proc(path: string, mask: int) -> bool {
  177. cstr := strings.new_cstring(path);
  178. defer delete(cstr);
  179. return _unix_access(cstr, mask) == 0;
  180. }
  181. heap_alloc :: proc(size: int) -> rawptr {
  182. assert(size >= 0);
  183. return _unix_calloc(1, size);
  184. }
  185. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  186. return _unix_realloc(ptr, new_size);
  187. }
  188. heap_free :: proc(ptr: rawptr) {
  189. _unix_free(ptr);
  190. }
  191. getenv :: proc(name: string) -> (string, bool) {
  192. path_str := strings.new_cstring(name);
  193. defer delete(path_str);
  194. cstr := _unix_getenv(path_str);
  195. if cstr == nil {
  196. return "", false;
  197. }
  198. return string(cstr), true;
  199. }
  200. exit :: proc(code: int) -> ! {
  201. _unix_exit(code);
  202. }
  203. current_thread_id :: proc "contextless" () -> int {
  204. // return int(_unix_gettid());
  205. return 0;
  206. }
  207. dlopen :: inline proc(filename: string, flags: int) -> rawptr {
  208. cstr := strings.new_cstring(filename);
  209. defer delete(cstr);
  210. handle := _unix_dlopen(cstr, flags);
  211. return handle;
  212. }
  213. dlsym :: inline proc(handle: rawptr, symbol: string) -> rawptr {
  214. assert(handle != nil);
  215. cstr := strings.new_cstring(symbol);
  216. defer delete(cstr);
  217. proc_handle := _unix_dlsym(handle, cstr);
  218. return proc_handle;
  219. }
  220. dlclose :: inline proc(handle: rawptr) -> bool {
  221. assert(handle != nil);
  222. return _unix_dlclose(handle) == 0;
  223. }
  224. dlerror :: proc() -> string {
  225. return string(_unix_dlerror());
  226. }
  227. _alloc_command_line_arguments :: proc() -> []string {
  228. args := make([]string, len(runtime.args__));
  229. for arg, i in runtime.args__ {
  230. args[i] = string(arg);
  231. }
  232. return args;
  233. }