1 use super::abi::usercalls::{alloc, raw::ByteBuffer};
2 use crate::ffi::OsString;
5 use crate::sync::atomic::{AtomicUsize, Ordering};
6 use crate::sys::os_str::Buf;
7 use crate::sys_common::FromInner;
9 #[cfg_attr(test, linkage = "available_externally")]
10 #[export_name = "_ZN16__rust_internals3std3sys3sgx4args4ARGSE"]
11 static ARGS: AtomicUsize = AtomicUsize::new(0);
12 type ArgsStore = Vec<OsString>;
14 #[cfg_attr(test, allow(dead_code))]
15 pub unsafe fn init(argc: isize, argv: *const *const u8) {
17 let args = unsafe { alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _) };
20 .map(|a| OsString::from_inner(Buf { inner: a.copy_user_buffer() }))
21 .collect::<ArgsStore>();
22 ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
26 pub fn args() -> Args {
27 let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
28 if let Some(args) = args { Args(args.iter()) } else { Args([].iter()) }
31 pub struct Args(slice::Iter<'static, OsString>);
33 impl fmt::Debug for Args {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 self.0.as_slice().fmt(f)
39 impl Iterator for Args {
41 fn next(&mut self) -> Option<OsString> {
42 self.0.next().cloned()
44 fn size_hint(&self) -> (usize, Option<usize>) {
49 impl ExactSizeIterator for Args {
50 fn len(&self) -> usize {
55 impl DoubleEndedIterator for Args {
56 fn next_back(&mut self) -> Option<OsString> {
57 self.0.next_back().cloned()