1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use super::abi::usercalls::{copy_user_buffer, alloc, ByteBuffer};
13 use sync::atomic::{AtomicUsize, Ordering};
15 use sys_common::FromInner;
18 static ARGS: AtomicUsize = AtomicUsize::new(0);
19 type ArgsStore = Vec<OsString>;
21 pub unsafe fn init(argc: isize, argv: *const *const u8) {
23 let args = alloc::User::<[ByteBuffer]>::from_raw_parts(argv as _, argc as _);
24 let args = args.iter()
25 .map( |a| OsString::from_inner(Buf { inner: copy_user_buffer(a) }) )
26 .collect::<ArgsStore>();
27 ARGS.store(Box::into_raw(Box::new(args)) as _, Ordering::Relaxed);
31 pub unsafe fn cleanup() {
32 let args = ARGS.swap(0, Ordering::Relaxed);
34 drop(Box::<ArgsStore>::from_raw(args as _))
38 pub fn args() -> Args {
39 let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
40 if let Some(args) = args {
47 pub struct Args(slice::Iter<'static, OsString>);
50 pub fn inner_debug(&self) -> &[OsString] {
55 impl Iterator for Args {
57 fn next(&mut self) -> Option<OsString> {
58 self.0.next().cloned()
60 fn size_hint(&self) -> (usize, Option<usize>) {
65 impl ExactSizeIterator for Args {
66 fn len(&self) -> usize {
71 impl DoubleEndedIterator for Args {
72 fn next_back(&mut self) -> Option<OsString> {
73 self.0.next_back().cloned()