2
0

mod.rs 12 KB

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