os_linux.odin 8.0 KB

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