os_linux.odin 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. foreign_system_library (
  2. dl "dl";
  3. libc "c";
  4. )
  5. import "strings.odin";
  6. Handle :: i32;
  7. FileTime :: u64;
  8. Errno :: i32;
  9. O_RDONLY :: 0x00000;
  10. O_WRONLY :: 0x00001;
  11. O_RDWR :: 0x00002;
  12. O_CREAT :: 0x00040;
  13. O_EXCL :: 0x00080;
  14. O_NOCTTY :: 0x00100;
  15. O_TRUNC :: 0x00200;
  16. O_NONBLOCK :: 0x00800;
  17. O_APPEND :: 0x00400;
  18. O_SYNC :: 0x01000;
  19. O_ASYNC :: 0x02000;
  20. O_CLOEXEC :: 0x80000;
  21. SEEK_SET :: 0;
  22. SEEK_CUR :: 1;
  23. SEEK_END :: 2;
  24. SEEK_DATA :: 3;
  25. SEEK_HOLE :: 4;
  26. SEEK_MAX :: SEEK_HOLE;
  27. // NOTE(zangent): These are OS specific!
  28. // Do not mix these up!
  29. RTLD_LAZY :: 0x001;
  30. RTLD_NOW :: 0x002;
  31. RTLD_BINDING_MASK :: 0x3;
  32. RTLD_GLOBAL :: 0x100;
  33. // "Argv" arguments converted to Odin strings
  34. args := _alloc_command_line_arguments();
  35. _FileTime :: struct #ordered {
  36. seconds: i64;
  37. nanoseconds: i32;
  38. reserved: i32;
  39. }
  40. // Translated from
  41. // 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
  42. // Validity is not guaranteed.
  43. Stat :: struct #ordered {
  44. device_id: u64; // ID of device containing file
  45. serial: u64; // File serial number
  46. nlink: u32; // Number of hard links
  47. mode: u32; // Mode of the file
  48. uid: u32; // User ID of the file's owner
  49. gid: u32; // Group ID of the file's group
  50. _padding: i32; // 32 bits of padding
  51. rdev: u64; // Device ID, if device
  52. size: i64; // Size of the file, in bytes
  53. block_size: i64; // Optimal bllocksize for I/O
  54. blocks: i64; // Number of 512-byte blocks allocated
  55. last_access: _FileTime; // Time of last access
  56. modified: _FileTime; // Time of last modification
  57. status_change: _FileTime; // Time of last status change
  58. _reserve1,
  59. _reserve2,
  60. _reserve3: i64;
  61. serial_numbe: u64; // File serial number...? Maybe.
  62. _reserve4: i64;
  63. };
  64. // File type
  65. S_IFMT :: 0170000; // Type of file mask
  66. S_IFIFO :: 0010000; // Named pipe (fifo)
  67. S_IFCHR :: 0020000; // Character special
  68. S_IFDIR :: 0040000; // Directory
  69. S_IFBLK :: 0060000; // Block special
  70. S_IFREG :: 0100000; // Regular
  71. S_IFLNK :: 0120000; // Symbolic link
  72. S_IFSOCK :: 0140000; // Socket
  73. // File mode
  74. // Read, write, execute/search by owner
  75. S_IRWXU :: 0000700; // RWX mask for owner
  76. S_IRUSR :: 0000400; // R for owner
  77. S_IWUSR :: 0000200; // W for owner
  78. S_IXUSR :: 0000100; // X for owner
  79. // Read, write, execute/search by group
  80. S_IRWXG :: 0000070; // RWX mask for group
  81. S_IRGRP :: 0000040; // R for group
  82. S_IWGRP :: 0000020; // W for group
  83. S_IXGRP :: 0000010; // X for group
  84. // Read, write, execute/search by others
  85. S_IRWXO :: 0000007; // RWX mask for other
  86. S_IROTH :: 0000004; // R for other
  87. S_IWOTH :: 0000002; // W for other
  88. S_IXOTH :: 0000001; // X for other
  89. S_ISUID :: 0004000; // Set user id on execution
  90. S_ISGID :: 0002000; // Set group id on execution
  91. S_ISVTX :: 0001000; // Directory restrcted delete
  92. S_ISLNK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFLNK; }
  93. S_ISREG :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFREG; }
  94. S_ISDIR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFDIR; }
  95. S_ISCHR :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFCHR; }
  96. S_ISBLK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFBLK; }
  97. S_ISFIFO :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFIFO; }
  98. S_ISSOCK :: proc(m: u32) -> bool #inline {return (m & S_IFMT) == S_IFSOCK;}
  99. F_OK :: 0; // Test for file existance
  100. X_OK :: 1; // Test for execute permission
  101. W_OK :: 2; // Test for write permission
  102. R_OK :: 4; // Test for read permission
  103. foreign libc {
  104. _unix_open :: proc(path: ^u8, mode: int) -> Handle #link_name "open" ---;
  105. _unix_close :: proc(fd: Handle) -> i32 #link_name "close" ---;
  106. _unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int #link_name "read" ---;
  107. _unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int #link_name "write" ---;
  108. _unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 #link_name "lseek64" ---;
  109. _unix_gettid :: proc() -> u64 #link_name "gettid" ---;
  110. _unix_stat :: proc(path: ^u8, stat: ^Stat) -> i32 #link_name "stat" ---;
  111. _unix_access :: proc(path: ^u8, mask: int) -> i32 #link_name "access" ---;
  112. _unix_malloc :: proc(size: int) -> rawptr #link_name "malloc" ---;
  113. _unix_free :: proc(ptr: rawptr) #link_name "free" ---;
  114. _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #link_name "realloc" ---;
  115. _unix_getenv :: proc(^u8) -> ^u8 #link_name "getenv" ---;
  116. _unix_exit :: proc(status: int) #link_name "exit" ---;
  117. }
  118. foreign dl {
  119. _unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #link_name "dlopen" ---;
  120. _unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym" ---;
  121. _unix_dlclose :: proc(handle: rawptr) -> int #link_name "dlclose" ---;
  122. _unix_dlerror :: proc() -> ^u8 #link_name "dlerror" ---;
  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: []u8) -> (int, Errno) {
  142. sz := _unix_read(fd, &data[0], len(data));
  143. return sz, 0;
  144. }
  145. write :: proc(fd: Handle, data: []u8) -> (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) -> FileTime {}
  165. last_write_time_by_name :: proc(name: string) -> FileTime {}
  166. */
  167. stat :: proc(path: string) -> (Stat, int) #inline {
  168. s: Stat;
  169. cstr := strings.new_c_string(path);
  170. defer free(cstr);
  171. ret_int := _unix_stat(cstr, &s);
  172. return s, int(ret_int);
  173. }
  174. access :: proc(path: string, mask: int) -> bool #inline {
  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_malloc(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. cstr: ^u8 = _unix_getenv(path_str);
  192. free(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 :: proc(filename: string, flags: int) -> rawptr #inline {
  206. cstr := strings.new_c_string(filename);
  207. handle := _unix_dlopen(cstr, flags);
  208. free(cstr);
  209. return handle;
  210. }
  211. dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
  212. assert(handle != nil);
  213. cstr := strings.new_c_string(symbol);
  214. proc_handle := _unix_dlsym(handle, cstr);
  215. free(cstr);
  216. return proc_handle;
  217. }
  218. dlclose :: proc(handle: rawptr) -> bool #inline {
  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. // TODO(bill):
  227. return nil;
  228. }