os_x.odin 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. foreign_system_library (
  2. dl "dl";
  3. libc "c";
  4. )
  5. import "strings.odin";
  6. Handle :: i32;
  7. FileTime :: u64;
  8. Errno :: int;
  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 :: 0x1;
  30. RTLD_NOW :: 0x2;
  31. RTLD_LOCAL :: 0x4;
  32. RTLD_GLOBAL :: 0x8;
  33. RTLD_NODELETE :: 0x80;
  34. RTLD_NOLOAD :: 0x10;
  35. RTLD_FIRST :: 0x100;
  36. args: [dynamic]string;
  37. _FileTime :: struct #ordered {
  38. seconds: i64;
  39. nanoseconds: i64;
  40. }
  41. Stat :: struct #ordered {
  42. device_id: i32; // ID of device containing file
  43. mode: u16; // Mode of the file
  44. nlink: u16; // Number of hard links
  45. serial: u64; // File serial number
  46. uid: u32; // User ID of the file's owner
  47. gid: u32; // Group ID of the file's group
  48. rdev: i32; // Device ID, if device
  49. last_access: FileTime; // Time of last access
  50. modified: FileTime; // Time of last modification
  51. status_change: FileTime; // Time of last status change
  52. created: FileTime; // Time of creation
  53. size: i64; // Size of the file, in bytes
  54. blocks: i64; // Number of blocks allocated for the file
  55. block_size: i32; // Optimal blocksize for I/O
  56. flags: u32; // User-defined flags for the file
  57. gen_num: u32; // File generation number ...?
  58. _spare: i32; // RESERVED
  59. _reserve1,
  60. _reserve2: i64; // RESERVED
  61. };
  62. // File type
  63. S_IFMT :: 0170000; // Type of file mask
  64. S_IFIFO :: 0010000; // Named pipe (fifo)
  65. S_IFCHR :: 0020000; // Character special
  66. S_IFDIR :: 0040000; // Directory
  67. S_IFBLK :: 0060000; // Block special
  68. S_IFREG :: 0100000; // Regular
  69. S_IFLNK :: 0120000; // Symbolic link
  70. S_IFSOCK :: 0140000; // Socket
  71. // File mode
  72. // Read, write, execute/search by owner
  73. S_IRWXU :: 0000700; // RWX mask for owner
  74. S_IRUSR :: 0000400; // R for owner
  75. S_IWUSR :: 0000200; // W for owner
  76. S_IXUSR :: 0000100; // X for owner
  77. // Read, write, execute/search by group
  78. S_IRWXG :: 0000070; // RWX mask for group
  79. S_IRGRP :: 0000040; // R for group
  80. S_IWGRP :: 0000020; // W for group
  81. S_IXGRP :: 0000010; // X for group
  82. // Read, write, execute/search by others
  83. S_IRWXO :: 0000007; // RWX mask for other
  84. S_IROTH :: 0000004; // R for other
  85. S_IWOTH :: 0000002; // W for other
  86. S_IXOTH :: 0000001; // X for other
  87. S_ISUID :: 0004000; // Set user id on execution
  88. S_ISGID :: 0002000; // Set group id on execution
  89. S_ISVTX :: 0001000; // Directory restrcted delete
  90. S_ISLNK :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFLNK;
  91. S_ISREG :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFREG;
  92. S_ISDIR :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFDIR;
  93. S_ISCHR :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFCHR;
  94. S_ISBLK :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFBLK;
  95. S_ISFIFO :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFIFO;
  96. S_ISSOCK :: proc(m: u32) -> bool #inline do return (m & S_IFMT) == S_IFSOCK;
  97. R_OK :: 4; // Test for read permission
  98. W_OK :: 2; // Test for write permission
  99. X_OK :: 1; // Test for execute permission
  100. F_OK :: 0; // Test for file existance
  101. foreign libc {
  102. unix_open :: proc(path: ^u8, mode: int) -> Handle #link_name "open" ---;
  103. unix_close :: proc(handle: Handle) #link_name "close" ---;
  104. unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int #link_name "read" ---;
  105. unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int #link_name "write" ---;
  106. unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int #link_name "lseek" ---;
  107. unix_gettid :: proc() -> u64 #link_name "gettid" ---;
  108. unix_stat :: proc(path: ^u8, stat: ^Stat) -> int #link_name "stat" ---;
  109. unix_access :: proc(path: ^u8, mask: int) -> int #link_name "access" ---;
  110. unix_malloc :: proc(size: int) -> rawptr #link_name "malloc" ---;
  111. unix_free :: proc(ptr: rawptr) #link_name "free" ---;
  112. unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #link_name "realloc" ---;
  113. unix_getenv :: proc(^u8) -> ^u8 #link_name "getenv" ---;
  114. unix_exit :: proc(status: int) #link_name "exit" ---;
  115. }
  116. foreign dl {
  117. unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #link_name "dlopen" ---;
  118. unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #link_name "dlsym" ---;
  119. unix_dlclose :: proc(handle: rawptr) -> int #link_name "dlclose" ---;
  120. unix_dlerror :: proc() -> ^u8 #link_name "dlerror" ---;
  121. }
  122. // TODO(zangent): Change this to just `open` when Bill fixes overloading.
  123. open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
  124. cstr := strings.new_c_string(path);
  125. handle := unix_open(cstr, mode);
  126. free(cstr);
  127. if(handle == -1) {
  128. return 0, 1;
  129. }
  130. return handle, 0;
  131. }
  132. // NOTE(zangent): This is here for compatability reasons. Should this be here?
  133. open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
  134. return open_simple(path, mode);
  135. }
  136. close :: proc(fd: Handle) {
  137. unix_close(fd);
  138. }
  139. write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  140. assert(fd != -1);
  141. bytes_written := unix_write(fd, &data[0], len(data));
  142. if(bytes_written == -1) {
  143. return 0, 1;
  144. }
  145. return bytes_written, 0;
  146. }
  147. read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  148. assert(fd != -1);
  149. bytes_read := unix_read(fd, &data[0], len(data));
  150. if(bytes_read == -1) {
  151. return 0, 1;
  152. }
  153. return bytes_read, 0;
  154. }
  155. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  156. assert(fd != -1);
  157. final_offset := i64(unix_lseek(fd, offset, whence));
  158. if(final_offset == -1) {
  159. return 0, 1;
  160. }
  161. return final_offset, 0;
  162. }
  163. file_size :: proc(fd: Handle) -> (i64, Errno) {
  164. prev, _ := seek(fd, 0, SEEK_CUR);
  165. size, err := seek(fd, 0, SEEK_END);
  166. seek(fd, prev, SEEK_SET);
  167. return i64(size), err;
  168. }
  169. // NOTE(bill): Uses startup to initialize it
  170. stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
  171. stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
  172. stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
  173. /* TODO(zangent): Implement these!
  174. last_write_time :: proc(fd: Handle) -> FileTime {}
  175. last_write_time_by_name :: proc(name: string) -> FileTime {}
  176. */
  177. stat :: proc(path: string) -> (Stat, bool) #inline {
  178. s: Stat;
  179. cstr := strings.new_c_string(path);
  180. defer free(cstr);
  181. ret_int := unix_stat(cstr, &s);
  182. return s, ret_int==0;
  183. }
  184. access :: proc(path: string, mask: int) -> bool #inline {
  185. cstr := strings.new_c_string(path);
  186. defer free(cstr);
  187. return unix_access(cstr, mask) == 0;
  188. }
  189. heap_alloc :: proc(size: int) -> rawptr #inline {
  190. assert(size > 0);
  191. return unix_malloc(size);
  192. }
  193. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr #inline {
  194. return unix_realloc(ptr, new_size);
  195. }
  196. heap_free :: proc(ptr: rawptr) #inline {
  197. unix_free(ptr);
  198. }
  199. getenv :: proc(name: string) -> (string, bool) {
  200. path_str := strings.new_c_string(name);
  201. cstr: ^u8 = unix_getenv(path_str);
  202. free(path_str);
  203. if(cstr == nil) {
  204. return "", false;
  205. }
  206. return strings.to_odin_string(cstr), true;
  207. }
  208. exit :: proc(code: int) #inline {
  209. unix_exit(code);
  210. }
  211. current_thread_id :: proc() -> int {
  212. // return cast(int) unix_gettid();
  213. return 0;
  214. }
  215. dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
  216. cstr := strings.new_c_string(filename);
  217. handle := unix_dlopen(cstr, flags);
  218. free(cstr);
  219. return handle;
  220. }
  221. dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
  222. assert(handle != nil);
  223. cstr := strings.new_c_string(symbol);
  224. proc_handle := unix_dlsym(handle, cstr);
  225. free(cstr);
  226. return proc_handle;
  227. }
  228. dlclose :: proc(handle: rawptr) -> bool #inline {
  229. assert(handle != nil);
  230. return unix_dlclose(handle) == 0;
  231. }
  232. dlerror :: proc() -> string {
  233. return strings.to_odin_string(unix_dlerror());
  234. }