os_freebsd.odin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. import "core:c"
  7. Handle :: distinct i32;
  8. File_Time :: distinct u64;
  9. Errno :: distinct i32;
  10. Syscall :: distinct i32;
  11. INVALID_HANDLE :: ~Handle(0);
  12. ERROR_NONE: Errno : 0;
  13. EPERM: Errno : 1;
  14. ENOENT: Errno : 2;
  15. ESRCH: Errno : 3;
  16. EINTR: Errno : 4;
  17. EIO: Errno : 5;
  18. ENXIO: Errno : 6;
  19. E2BIG: Errno : 7;
  20. ENOEXEC: Errno : 8;
  21. EBADF: Errno : 9;
  22. ECHILD: Errno : 10;
  23. EBEADLK: Errno : 11;
  24. ENOMEM: Errno : 12;
  25. EACCESS: Errno : 13;
  26. EFAULT: Errno : 14;
  27. ENOTBLK: Errno : 15;
  28. EBUSY: Errno : 16;
  29. EEXIST: Errno : 17;
  30. EXDEV: Errno : 18;
  31. ENODEV: Errno : 19;
  32. ENOTDIR: Errno : 20;
  33. EISDIR: Errno : 21;
  34. EINVAL: Errno : 22;
  35. ENFILE: Errno : 23;
  36. EMFILE: Errno : 24;
  37. ENOTTY: Errno : 25;
  38. ETXTBSY: Errno : 26;
  39. EFBIG: Errno : 27;
  40. ENOSPC: Errno : 28;
  41. ESPIPE: Errno : 29;
  42. EROFS: Errno : 30;
  43. EMLINK: Errno : 31;
  44. EPIPE: Errno : 32;
  45. EDOM: Errno : 33;
  46. ERANGE: Errno : 34; /* Result too large */
  47. EAGAIN: Errno : 35;
  48. EINPROGRESS: Errno : 36;
  49. EALREADY: Errno : 37;
  50. ENOTSOCK: Errno : 38;
  51. EDESTADDRREQ: Errno : 39;
  52. EMSGSIZE: Errno : 40;
  53. EPROTOTYPE: Errno : 41;
  54. ENOPROTOOPT: Errno : 42;
  55. EPROTONOSUPPORT: Errno : 43;
  56. ESOCKTNOSUPPORT: Errno : 44;
  57. EOPNOTSUPP: Errno : 45;
  58. EPFNOSUPPORT: Errno : 46;
  59. EAFNOSUPPORT: Errno : 47;
  60. EADDRINUSE: Errno : 48;
  61. EADDRNOTAVAIL: Errno : 49;
  62. ENETDOWN: Errno : 50;
  63. ENETUNREACH: Errno : 51;
  64. ENETRESET: Errno : 52;
  65. ECONNABORTED: Errno : 53;
  66. ECONNRESET: Errno : 54;
  67. ENOBUFS: Errno : 55;
  68. EISCONN: Errno : 56;
  69. ENOTCONN: Errno : 57;
  70. ESHUTDOWN: Errno : 58;
  71. ETIMEDOUT: Errno : 60;
  72. ECONNREFUSED: Errno : 61;
  73. ELOOP: Errno : 62;
  74. ENAMETOOLING: Errno : 63;
  75. EHOSTDOWN: Errno : 64;
  76. EHOSTUNREACH: Errno : 65;
  77. ENOTEMPTY: Errno : 66;
  78. EPROCLIM: Errno : 67;
  79. EUSERS: Errno : 68;
  80. EDQUOT: Errno : 69;
  81. ESTALE: Errno : 70;
  82. EBADRPC: Errno : 72;
  83. ERPCMISMATCH: Errno : 73;
  84. EPROGUNAVAIL: Errno : 74;
  85. EPROGMISMATCH: Errno : 75;
  86. EPROCUNAVAIL: Errno : 76;
  87. ENOLCK: Errno : 77;
  88. ENOSYS: Errno : 78;
  89. EFTYPE: Errno : 79;
  90. EAUTH: Errno : 80;
  91. ENEEDAUTH: Errno : 81;
  92. EIDRM: Errno : 82;
  93. ENOMSG: Errno : 83;
  94. EOVERFLOW: Errno : 84;
  95. ECANCELED: Errno : 85;
  96. EILSEQ: Errno : 86;
  97. ENOATTR: Errno : 87;
  98. EDOOFUS: Errno : 88;
  99. EBADMSG: Errno : 89;
  100. EMULTIHOP: Errno : 90;
  101. ENOLINK: Errno : 91;
  102. EPROTO: Errno : 92;
  103. ENOTCAPABLE: Errno : 93;
  104. ECAPMODE: Errno : 94;
  105. ENOTRECOVERABLE: Errno : 95;
  106. EOWNERDEAD: Errno : 96;
  107. O_RDONLY :: 0x00000;
  108. O_WRONLY :: 0x00001;
  109. O_RDWR :: 0x00002;
  110. O_CREATE :: 0x00040;
  111. O_EXCL :: 0x00080;
  112. O_NOCTTY :: 0x00100;
  113. O_TRUNC :: 0x00200;
  114. O_NONBLOCK :: 0x00800;
  115. O_APPEND :: 0x00400;
  116. O_SYNC :: 0x01000;
  117. O_ASYNC :: 0x02000;
  118. O_CLOEXEC :: 0x80000;
  119. SEEK_SET :: 0;
  120. SEEK_CUR :: 1;
  121. SEEK_END :: 2;
  122. SEEK_DATA :: 3;
  123. SEEK_HOLE :: 4;
  124. SEEK_MAX :: SEEK_HOLE;
  125. // NOTE: These are OS specific!
  126. // Do not mix these up!
  127. RTLD_LAZY :: 0x001;
  128. RTLD_NOW :: 0x002;
  129. //RTLD_BINDING_MASK :: 0x3; // Called MODEMASK in dlfcn.h
  130. RTLD_GLOBAL :: 0x100;
  131. RTLD_LOCAL :: 0x000;
  132. RTLD_TRACE :: 0x200;
  133. RTLD_NODELETE :: 0x01000;
  134. RTLD_NOLOAD :: 0x02000;
  135. args := _alloc_command_line_arguments();
  136. Unix_File_Time :: struct {
  137. seconds: i64,
  138. nanoseconds: c.long,
  139. }
  140. pid_t :: u32;
  141. OS_Stat :: struct {
  142. device_id: u64,
  143. serial: u64,
  144. nlink: u64,
  145. mode: u32,
  146. _padding0: i16,
  147. uid: u32,
  148. gid: u32,
  149. _padding1: i32,
  150. rdev: u64,
  151. last_access: Unix_File_Time,
  152. modified: Unix_File_Time,
  153. status_change: Unix_File_Time,
  154. birthtime: Unix_File_Time,
  155. size: i64,
  156. blocks: i64,
  157. block_size: i32,
  158. flags: u32,
  159. gen: u64,
  160. lspare: i64,
  161. }
  162. // File type
  163. S_IFMT :: 0o170000; // Type of file mask
  164. S_IFIFO :: 0o010000; // Named pipe (fifo)
  165. S_IFCHR :: 0o020000; // Character special
  166. S_IFDIR :: 0o040000; // Directory
  167. S_IFBLK :: 0o060000; // Block special
  168. S_IFREG :: 0o100000; // Regular
  169. S_IFLNK :: 0o120000; // Symbolic link
  170. S_IFSOCK :: 0o140000; // Socket
  171. //S_ISVTX :: 0o001000; // Save swapped text even after use
  172. // File mode
  173. // Read, write, execute/search by owner
  174. S_IRWXU :: 0o0700; // RWX mask for owner
  175. S_IRUSR :: 0o0400; // R for owner
  176. S_IWUSR :: 0o0200; // W for owner
  177. S_IXUSR :: 0o0100; // X for owner
  178. // Read, write, execute/search by group
  179. S_IRWXG :: 0o0070; // RWX mask for group
  180. S_IRGRP :: 0o0040; // R for group
  181. S_IWGRP :: 0o0020; // W for group
  182. S_IXGRP :: 0o0010; // X for group
  183. // Read, write, execute/search by others
  184. S_IRWXO :: 0o0007; // RWX mask for other
  185. S_IROTH :: 0o0004; // R for other
  186. S_IWOTH :: 0o0002; // W for other
  187. S_IXOTH :: 0o0001; // X for other
  188. S_ISUID :: 0o4000; // Set user id on execution
  189. S_ISGID :: 0o2000; // Set group id on execution
  190. S_ISVTX :: 0o1000; // Directory restrcted delete
  191. S_ISLNK :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFLNK;
  192. S_ISREG :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFREG;
  193. S_ISDIR :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFDIR;
  194. S_ISCHR :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFCHR;
  195. S_ISBLK :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFBLK;
  196. S_ISFIFO :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFIFO;
  197. S_ISSOCK :: #force_inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFSOCK;
  198. F_OK :: 0; // Test for file existance
  199. X_OK :: 1; // Test for execute permission
  200. W_OK :: 2; // Test for write permission
  201. R_OK :: 4; // Test for read permission
  202. foreign libc {
  203. @(link_name="__error") __errno_location :: proc() -> ^int ---;
  204. @(link_name="syscall") syscall :: proc(number: Syscall, #c_vararg args: ..any) -> int ---;
  205. @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---;
  206. @(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---;
  207. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---;
  208. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---;
  209. @(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
  210. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  211. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
  212. @(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
  213. @(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
  214. @(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---;
  215. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---;
  216. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---;
  217. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  218. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---;
  219. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  220. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
  221. @(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
  222. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---;
  223. }
  224. foreign dl {
  225. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---;
  226. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  227. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---;
  228. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  229. @(link_name="pthread_getthreadid_np") pthread_getthreadid_np :: proc() -> c.int ---;
  230. }
  231. is_path_separator :: proc(r: rune) -> bool {
  232. return r == '/';
  233. }
  234. get_last_error :: proc() -> int {
  235. return __errno_location()^;
  236. }
  237. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  238. cstr := strings.clone_to_cstring(path);
  239. handle := _unix_open(cstr, c.int(flags), c.int(mode));
  240. delete(cstr);
  241. if handle == -1 {
  242. return INVALID_HANDLE, Errno(get_last_error());
  243. }
  244. return handle, ERROR_NONE;
  245. }
  246. close :: proc(fd: Handle) -> Errno {
  247. result := _unix_close(fd);
  248. if result == -1 {
  249. return Errno(get_last_error());
  250. }
  251. return ERROR_NONE;
  252. }
  253. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  254. bytes_read := _unix_read(fd, &data[0], c.size_t(len(data)));
  255. if bytes_read == -1 {
  256. return -1, Errno(get_last_error());
  257. }
  258. return int(bytes_read), ERROR_NONE;
  259. }
  260. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  261. if len(data) == 0 {
  262. return 0, ERROR_NONE;
  263. }
  264. bytes_written := _unix_write(fd, &data[0], c.size_t(len(data)));
  265. if bytes_written == -1 {
  266. return -1, Errno(get_last_error());
  267. }
  268. return int(bytes_written), ERROR_NONE;
  269. }
  270. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  271. res := _unix_seek(fd, offset, c.int(whence));
  272. if res == -1 {
  273. return -1, Errno(get_last_error());
  274. }
  275. return res, ERROR_NONE;
  276. }
  277. file_size :: proc(fd: Handle) -> (i64, Errno) {
  278. s, err := fstat(fd);
  279. if err != ERROR_NONE {
  280. return -1, err;
  281. }
  282. return s.size, ERROR_NONE;
  283. }
  284. stdin: Handle = 0;
  285. stdout: Handle = 1;
  286. stderr: Handle = 2;
  287. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  288. s, err := fstat(fd);
  289. if err != ERROR_NONE {
  290. return 0, err;
  291. }
  292. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds;
  293. return File_Time(modified), ERROR_NONE;
  294. }
  295. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  296. s, err := stat(name);
  297. if err != ERROR_NONE {
  298. return 0, err;
  299. }
  300. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds;
  301. return File_Time(modified), ERROR_NONE;
  302. }
  303. stat :: proc(path: string) -> (OS_Stat, Errno) {
  304. cstr := strings.clone_to_cstring(path);
  305. defer delete(cstr);
  306. s: OS_Stat;
  307. result := _unix_stat(cstr, &s);
  308. if result == -1 {
  309. return s, Errno(get_last_error());
  310. }
  311. return s, ERROR_NONE;
  312. }
  313. fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  314. s: OS_Stat;
  315. result := _unix_fstat(fd, &s);
  316. if result == -1 {
  317. return s, Errno(get_last_error());
  318. }
  319. return s, ERROR_NONE;
  320. }
  321. access :: proc(path: string, mask: int) -> (bool, Errno) {
  322. cstr := strings.clone_to_cstring(path);
  323. defer delete(cstr);
  324. result := _unix_access(cstr, c.int(mask));
  325. if result == -1 {
  326. return false, Errno(get_last_error());
  327. }
  328. return true, ERROR_NONE;
  329. }
  330. heap_alloc :: proc(size: int) -> rawptr {
  331. assert(size >= 0);
  332. return _unix_calloc(1, c.size_t(size));
  333. }
  334. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  335. return _unix_realloc(ptr, c.size_t(new_size));
  336. }
  337. heap_free :: proc(ptr: rawptr) {
  338. _unix_free(ptr);
  339. }
  340. getenv :: proc(name: string) -> (string, bool) {
  341. path_str := strings.clone_to_cstring(name);
  342. defer delete(path_str);
  343. cstr := _unix_getenv(path_str);
  344. if cstr == nil {
  345. return "", false;
  346. }
  347. return string(cstr), true;
  348. }
  349. get_current_directory :: proc() -> string {
  350. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  351. // an authoritative value for it across all systems.
  352. // The largest value I could find was 4096, so might as well use the page size.
  353. page_size := get_page_size();
  354. buf := make([dynamic]u8, page_size);
  355. #no_bounds_check for {
  356. cwd := _unix_getcwd(cstring(&buf[0]), c.size_t(len(buf)));
  357. if cwd != nil {
  358. return string(cwd);
  359. }
  360. if Errno(get_last_error()) != ERANGE {
  361. return "";
  362. }
  363. resize(&buf, len(buf)+page_size);
  364. }
  365. unreachable();
  366. }
  367. set_current_directory :: proc(path: string) -> (err: Errno) {
  368. cstr := strings.clone_to_cstring(path, context.temp_allocator);
  369. res := _unix_chdir(cstr);
  370. if res == -1 do return Errno(get_last_error());
  371. return ERROR_NONE;
  372. }
  373. exit :: proc "contextless" (code: int) -> ! {
  374. _unix_exit(c.int(code));
  375. }
  376. current_thread_id :: proc "contextless" () -> int {
  377. return cast(int) pthread_getthreadid_np();
  378. }
  379. dlopen :: proc(filename: string, flags: int) -> rawptr {
  380. cstr := strings.clone_to_cstring(filename);
  381. defer delete(cstr);
  382. handle := _unix_dlopen(cstr, c.int(flags));
  383. return handle;
  384. }
  385. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  386. assert(handle != nil);
  387. cstr := strings.clone_to_cstring(symbol);
  388. defer delete(cstr);
  389. proc_handle := _unix_dlsym(handle, cstr);
  390. return proc_handle;
  391. }
  392. dlclose :: proc(handle: rawptr) -> bool {
  393. assert(handle != nil);
  394. return _unix_dlclose(handle) == 0;
  395. }
  396. dlerror :: proc() -> string {
  397. return string(_unix_dlerror());
  398. }
  399. get_page_size :: proc() -> int {
  400. // NOTE(tetra): The page size never changes, so why do anything complicated
  401. // if we don't have to.
  402. @static page_size := -1;
  403. if page_size != -1 do return page_size;
  404. page_size = int(_unix_getpagesize());
  405. return page_size;
  406. }
  407. _alloc_command_line_arguments :: proc() -> []string {
  408. res := make([]string, len(runtime.args__));
  409. for arg, i in runtime.args__ {
  410. res[i] = string(arg);
  411. }
  412. return res;
  413. }