lib.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //! unistd implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
  2. #![no_std]
  3. extern crate errno;
  4. extern crate platform;
  5. extern crate stdio;
  6. extern crate string;
  7. extern crate sys_time;
  8. use core::{ptr, slice};
  9. use platform::types::*;
  10. pub use brk::*;
  11. pub use getopt::*;
  12. pub use pathconf::*;
  13. mod brk;
  14. mod getopt;
  15. mod pathconf;
  16. pub const F_OK: c_int = 0;
  17. pub const R_OK: c_int = 4;
  18. pub const W_OK: c_int = 2;
  19. pub const X_OK: c_int = 1;
  20. pub const SEEK_SET: c_int = 0;
  21. pub const SEEK_CUR: c_int = 1;
  22. pub const SEEK_END: c_int = 2;
  23. pub const F_ULOCK: c_int = 0;
  24. pub const F_LOCK: c_int = 1;
  25. pub const F_TLOCK: c_int = 2;
  26. pub const F_TEST: c_int = 3;
  27. pub const STDIN_FILENO: c_int = 0;
  28. pub const STDOUT_FILENO: c_int = 1;
  29. pub const STDERR_FILENO: c_int = 2;
  30. const PATH_MAX: usize = 4096;
  31. #[no_mangle]
  32. pub extern "C" fn _exit(status: c_int) {
  33. platform::exit(status)
  34. }
  35. #[no_mangle]
  36. pub extern "C" fn access(path: *const c_char, mode: c_int) -> c_int {
  37. platform::access(path, mode)
  38. }
  39. #[no_mangle]
  40. pub extern "C" fn alarm(seconds: c_uint) -> c_uint {
  41. let mut timer = sys_time::itimerval {
  42. it_value: sys_time::timeval {
  43. tv_sec: seconds as time_t,
  44. tv_usec: 0,
  45. },
  46. ..Default::default()
  47. };
  48. let errno_backup = unsafe { platform::errno };
  49. let secs = if sys_time::setitimer(sys_time::ITIMER_REAL, &timer, &mut timer) < 0 {
  50. 0
  51. } else {
  52. timer.it_value.tv_sec as c_uint + if timer.it_value.tv_usec > 0 { 1 } else { 0 }
  53. };
  54. unsafe {
  55. platform::errno = errno_backup;
  56. }
  57. secs
  58. }
  59. #[no_mangle]
  60. pub extern "C" fn chdir(path: *const c_char) -> c_int {
  61. platform::chdir(path)
  62. }
  63. // #[no_mangle]
  64. pub extern "C" fn chroot(path: *const c_char) -> c_int {
  65. unimplemented!();
  66. }
  67. #[no_mangle]
  68. pub extern "C" fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  69. platform::chown(path, owner, group)
  70. }
  71. #[no_mangle]
  72. pub extern "C" fn close(fildes: c_int) -> c_int {
  73. platform::close(fildes)
  74. }
  75. // #[no_mangle]
  76. pub extern "C" fn confstr(name: c_int, buf: *mut c_char, len: size_t) -> size_t {
  77. unimplemented!();
  78. }
  79. // #[no_mangle]
  80. pub extern "C" fn crypt(key: *const c_char, salt: *const c_char) -> *mut c_char {
  81. unimplemented!();
  82. }
  83. #[no_mangle]
  84. pub extern "C" fn dup(fildes: c_int) -> c_int {
  85. platform::dup(fildes)
  86. }
  87. #[no_mangle]
  88. pub extern "C" fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  89. platform::dup2(fildes, fildes2)
  90. }
  91. // #[no_mangle]
  92. pub extern "C" fn encrypt(block: [c_char; 64], edflag: c_int) {
  93. unimplemented!();
  94. }
  95. // #[no_mangle]
  96. // pub extern "C" fn execl(path: *const c_char, args: *const *mut c_char) -> c_int {
  97. // unimplemented!();
  98. // }
  99. // #[no_mangle]
  100. // pub extern "C" fn execle(
  101. // path: *const c_char,
  102. // args: *const *mut c_char,
  103. // envp: *const *mut c_char,
  104. // ) -> c_int {
  105. // unimplemented!();
  106. // }
  107. // #[no_mangle]
  108. // pub extern "C" fn execlp(file: *const c_char, args: *const *mut c_char) -> c_int {
  109. // unimplemented!();
  110. // }
  111. #[no_mangle]
  112. pub unsafe extern "C" fn execv(path: *const c_char, argv: *const *mut c_char) -> c_int {
  113. execve(path, argv, platform::environ)
  114. }
  115. #[no_mangle]
  116. pub unsafe extern "C" fn execve(
  117. path: *const c_char,
  118. argv: *const *mut c_char,
  119. envp: *const *mut c_char,
  120. ) -> c_int {
  121. platform::execve(path, argv, envp)
  122. }
  123. // #[no_mangle]
  124. pub extern "C" fn execvp(file: *const c_char, argv: *const *mut c_char) -> c_int {
  125. unimplemented!();
  126. }
  127. #[no_mangle]
  128. pub extern "C" fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  129. platform::fchown(fildes, owner, group)
  130. }
  131. #[no_mangle]
  132. pub extern "C" fn fchdir(fildes: c_int) -> c_int {
  133. platform::fchdir(fildes)
  134. }
  135. // #[no_mangle]
  136. pub extern "C" fn fdatasync(fildes: c_int) -> c_int {
  137. unimplemented!();
  138. }
  139. #[no_mangle]
  140. pub extern "C" fn fork() -> pid_t {
  141. platform::fork()
  142. }
  143. #[no_mangle]
  144. pub extern "C" fn fsync(fildes: c_int) -> c_int {
  145. platform::fsync(fildes)
  146. }
  147. #[no_mangle]
  148. pub extern "C" fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  149. platform::ftruncate(fildes, length)
  150. }
  151. #[no_mangle]
  152. pub extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char {
  153. let alloc = buf.is_null();
  154. let mut stack_buf = [0; PATH_MAX];
  155. if alloc {
  156. buf = stack_buf.as_mut_ptr();
  157. size = stack_buf.len();
  158. }
  159. let ret = platform::getcwd(buf, size);
  160. if ret == ptr::null_mut() {
  161. return ptr::null_mut();
  162. }
  163. if alloc {
  164. let mut len = stack_buf.iter().position(|b| *b == 0).expect("no nul-byte in getcwd string") + 1;
  165. let mut heap_buf = unsafe { platform::alloc(len) as *mut c_char };
  166. for i in 0..len {
  167. unsafe {
  168. *heap_buf.offset(i as isize) = stack_buf[i];
  169. }
  170. }
  171. heap_buf
  172. } else {
  173. ret
  174. }
  175. }
  176. // #[no_mangle]
  177. pub extern "C" fn getdtablesize() -> c_int {
  178. unimplemented!();
  179. }
  180. #[no_mangle]
  181. pub extern "C" fn getegid() -> gid_t {
  182. platform::getegid()
  183. }
  184. #[no_mangle]
  185. pub extern "C" fn geteuid() -> uid_t {
  186. platform::geteuid()
  187. }
  188. #[no_mangle]
  189. pub extern "C" fn getgid() -> gid_t {
  190. platform::getgid()
  191. }
  192. // #[no_mangle]
  193. pub extern "C" fn getgroups(gidsetsize: c_int, grouplist: *mut gid_t) -> c_int {
  194. unimplemented!();
  195. }
  196. // #[no_mangle]
  197. pub extern "C" fn gethostid() -> c_long {
  198. unimplemented!();
  199. }
  200. #[no_mangle]
  201. pub unsafe extern "C" fn gethostname(name: *mut c_char, len: size_t) -> c_int {
  202. platform::gethostname(name, len)
  203. }
  204. // #[no_mangle]
  205. pub extern "C" fn getlogin() -> *mut c_char {
  206. unimplemented!();
  207. }
  208. // #[no_mangle]
  209. pub extern "C" fn getlogin_r(name: *mut c_char, namesize: size_t) -> c_int {
  210. unimplemented!();
  211. }
  212. // #[no_mangle]
  213. pub extern "C" fn getpagesize() -> c_int {
  214. unimplemented!();
  215. }
  216. // #[no_mangle]
  217. pub extern "C" fn getpass(prompt: *const c_char) -> *mut c_char {
  218. unimplemented!();
  219. }
  220. #[no_mangle]
  221. pub extern "C" fn getpgid(pid: pid_t) -> pid_t {
  222. platform::getpgid(pid)
  223. }
  224. #[no_mangle]
  225. pub extern "C" fn getpgrp() -> pid_t {
  226. platform::getpgid(platform::getpid())
  227. }
  228. #[no_mangle]
  229. pub extern "C" fn getpid() -> pid_t {
  230. platform::getpid()
  231. }
  232. #[no_mangle]
  233. pub extern "C" fn getppid() -> pid_t {
  234. platform::getppid()
  235. }
  236. // #[no_mangle]
  237. pub extern "C" fn getsid(pid: pid_t) -> pid_t {
  238. unimplemented!();
  239. }
  240. #[no_mangle]
  241. pub extern "C" fn getuid() -> uid_t {
  242. platform::getuid()
  243. }
  244. #[no_mangle]
  245. pub extern "C" fn getwd(path_name: *mut c_char) -> *mut c_char {
  246. getcwd(path_name, PATH_MAX)
  247. }
  248. #[no_mangle]
  249. pub extern "C" fn isatty(fd: c_int) -> c_int {
  250. platform::isatty(fd)
  251. }
  252. // #[no_mangle]
  253. pub extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
  254. unimplemented!();
  255. }
  256. #[no_mangle]
  257. pub extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int {
  258. platform::link(path1, path2)
  259. }
  260. // #[no_mangle]
  261. pub extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c_int {
  262. unimplemented!();
  263. }
  264. #[no_mangle]
  265. pub extern "C" fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
  266. platform::lseek(fildes, offset, whence)
  267. }
  268. // #[no_mangle]
  269. pub extern "C" fn nice(incr: c_int) -> c_int {
  270. unimplemented!();
  271. }
  272. // #[no_mangle]
  273. pub extern "C" fn pause() -> c_int {
  274. unimplemented!();
  275. }
  276. #[no_mangle]
  277. pub unsafe extern "C" fn pipe(fildes: *mut c_int) -> c_int {
  278. platform::pipe(slice::from_raw_parts_mut(fildes, 2))
  279. }
  280. // #[no_mangle]
  281. pub extern "C" fn pread(fildes: c_int, buf: *mut c_void, nbyte: size_t, offset: off_t) -> ssize_t {
  282. unimplemented!();
  283. }
  284. // #[no_mangle]
  285. pub extern "C" fn pthread_atfork(
  286. prepare: Option<extern "C" fn()>,
  287. parent: Option<extern "C" fn()>,
  288. child: Option<extern "C" fn()>,
  289. ) -> c_int {
  290. unimplemented!();
  291. }
  292. // #[no_mangle]
  293. pub extern "C" fn pwrite(
  294. fildes: c_int,
  295. buf: *const c_void,
  296. nbyte: size_t,
  297. offset: off_t,
  298. ) -> ssize_t {
  299. unimplemented!();
  300. }
  301. #[no_mangle]
  302. pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
  303. use core::slice;
  304. let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) };
  305. platform::read(fildes, buf)
  306. }
  307. // #[no_mangle]
  308. pub extern "C" fn readlink(path: *const c_char, buf: *mut c_char, bufsize: size_t) -> c_int {
  309. unimplemented!();
  310. }
  311. #[no_mangle]
  312. pub extern "C" fn rmdir(path: *const c_char) -> c_int {
  313. platform::rmdir(path)
  314. }
  315. #[no_mangle]
  316. pub extern "C" fn setgid(gid: gid_t) -> c_int {
  317. platform::setregid(gid, gid)
  318. }
  319. #[no_mangle]
  320. pub extern "C" fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
  321. platform::setpgid(pid, pgid)
  322. }
  323. // #[no_mangle]
  324. pub extern "C" fn setpgrp() -> pid_t {
  325. unimplemented!();
  326. }
  327. #[no_mangle]
  328. pub extern "C" fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
  329. platform::setregid(rgid, egid)
  330. }
  331. #[no_mangle]
  332. pub extern "C" fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
  333. platform::setreuid(ruid, euid)
  334. }
  335. // #[no_mangle]
  336. pub extern "C" fn setsid() -> pid_t {
  337. unimplemented!();
  338. }
  339. #[no_mangle]
  340. pub extern "C" fn setuid(uid: uid_t) -> c_int {
  341. platform::setreuid(uid, uid)
  342. }
  343. #[no_mangle]
  344. pub extern "C" fn sleep(seconds: c_uint) -> c_uint {
  345. let rqtp = timespec {
  346. tv_sec: seconds as i64,
  347. tv_nsec: 0,
  348. };
  349. let rmtp = ptr::null_mut();
  350. platform::nanosleep(&rqtp, rmtp);
  351. 0
  352. }
  353. // #[no_mangle]
  354. pub extern "C" fn swab(src: *const c_void, dest: *mut c_void, nbytes: ssize_t) {
  355. unimplemented!();
  356. }
  357. // #[no_mangle]
  358. pub extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int {
  359. unimplemented!();
  360. }
  361. // #[no_mangle]
  362. pub extern "C" fn sync() {
  363. unimplemented!();
  364. }
  365. // #[no_mangle]
  366. pub extern "C" fn sysconf(name: c_int) -> c_long {
  367. unimplemented!();
  368. }
  369. // #[no_mangle]
  370. pub extern "C" fn tcgetpgrp() -> pid_t {
  371. unimplemented!();
  372. }
  373. // #[no_mangle]
  374. pub extern "C" fn tcsetpgrp(fildes: c_int, pgid_id: pid_t) -> c_int {
  375. unimplemented!();
  376. }
  377. // #[no_mangle]
  378. pub extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int {
  379. unimplemented!();
  380. }
  381. // #[no_mangle]
  382. pub extern "C" fn ttyname(fildes: c_int) -> *mut c_char {
  383. unimplemented!();
  384. }
  385. // #[no_mangle]
  386. pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t) -> c_int {
  387. unimplemented!();
  388. }
  389. #[no_mangle]
  390. pub extern "C" fn ualarm(value: useconds_t, interval: useconds_t) -> useconds_t {
  391. let mut timer = sys_time::itimerval {
  392. it_value: sys_time::timeval {
  393. tv_sec: 0,
  394. tv_usec: value as suseconds_t,
  395. },
  396. it_interval: sys_time::timeval {
  397. tv_sec: 0,
  398. tv_usec: interval as suseconds_t,
  399. },
  400. };
  401. let errno_backup = unsafe { platform::errno };
  402. let usecs = if sys_time::setitimer(sys_time::ITIMER_REAL, &timer, &mut timer) < 0 {
  403. 0
  404. } else {
  405. timer.it_value.tv_sec as useconds_t * 1_000_000 + timer.it_value.tv_usec as useconds_t
  406. };
  407. unsafe {
  408. platform::errno = errno_backup;
  409. }
  410. usecs
  411. }
  412. #[no_mangle]
  413. pub extern "C" fn unlink(path: *const c_char) -> c_int {
  414. platform::unlink(path)
  415. }
  416. #[no_mangle]
  417. pub extern "C" fn usleep(useconds: useconds_t) -> c_int {
  418. let rqtp = timespec {
  419. tv_sec: (useconds / 1_000_000) as i64,
  420. tv_nsec: ((useconds % 1000) * 1000) as i64,
  421. };
  422. let rmtp = ptr::null_mut();
  423. platform::nanosleep(&rqtp, rmtp)
  424. }
  425. // #[no_mangle]
  426. pub extern "C" fn vfork() -> pid_t {
  427. unimplemented!();
  428. }
  429. #[no_mangle]
  430. pub extern "C" fn write(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
  431. use core::slice;
  432. let buf = unsafe { slice::from_raw_parts(buf as *const u8, nbyte as usize) };
  433. platform::write(fildes, buf)
  434. }
  435. /*
  436. #[no_mangle]
  437. pub extern "C" fn func(args) -> c_int {
  438. unimplemented!();
  439. }
  440. */