os_linux.odin 8.0 KB

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