1 use super::abi::usercalls::{alloc, raw::ByteBuffer};
2 use crate::ffi::OsString;
3 use crate::sync::atomic::{AtomicUsize, Ordering};
4 use crate::sys::os_str::Buf;
5 use crate::sys_common::FromInner;
8 static ARGS: AtomicUsize = AtomicUsize::new(0);
9 type ArgsStore = Vec<OsString>;
11 pub unsafe fn init(argc: isize, argv: *const *const u8) {
13 let args = alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _);
14 let args = args.iter()
15 .map( |a| OsString::from_inner(Buf { inner: a.copy_user_buffer() }) )
16 .collect::<ArgsStore>();
17 ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
21 pub unsafe fn cleanup() {
22 let args = ARGS.swap(0, Ordering::Relaxed);
24 drop(Box::<ArgsStore>::from_raw(args as _))
28 pub fn args() -> Args {
29 let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
30 if let Some(args) = args {
37 pub struct Args(slice::Iter<'static, OsString>);
40 pub fn inner_debug(&self) -> &[OsString] {
45 impl Iterator for Args {
47 fn next(&mut self) -> Option<OsString> {
48 self.0.next().cloned()
50 fn size_hint(&self) -> (usize, Option<usize>) {
55 impl ExactSizeIterator for Args {
56 fn len(&self) -> usize {
61 impl DoubleEndedIterator for Args {
62 fn next_back(&mut self) -> Option<OsString> {
63 self.0.next_back().cloned()