]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/vxworks/args.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / vxworks / args.rs
CommitLineData
476ff2be 1#![allow(dead_code)] // runtime init functions not used during testing
532ac7d7
XL
2use crate::ffi::OsString;
3use crate::marker::PhantomData;
4use crate::vec;
476ff2be
SL
5
6/// One-time global initialization.
60c5eb7d
XL
7pub unsafe fn init(argc: isize, argv: *const *const u8) {
8 imp::init(argc, argv)
9}
476ff2be
SL
10
11/// One-time global cleanup.
60c5eb7d
XL
12pub unsafe fn cleanup() {
13 imp::cleanup()
14}
476ff2be
SL
15
16/// Returns the command line arguments
17pub fn args() -> Args {
18 imp::args()
19}
20
21pub struct Args {
22 iter: vec::IntoIter<OsString>,
23 _dont_send_or_sync_me: PhantomData<*mut ()>,
24}
25
041b39d2
XL
26impl Args {
27 pub fn inner_debug(&self) -> &[OsString] {
28 self.iter.as_slice()
29 }
30}
31
476ff2be
SL
32impl Iterator for Args {
33 type Item = OsString;
60c5eb7d
XL
34 fn next(&mut self) -> Option<OsString> {
35 self.iter.next()
36 }
37 fn size_hint(&self) -> (usize, Option<usize>) {
38 self.iter.size_hint()
39 }
476ff2be
SL
40}
41
42impl ExactSizeIterator for Args {
60c5eb7d
XL
43 fn len(&self) -> usize {
44 self.iter.len()
45 }
476ff2be
SL
46}
47
48impl DoubleEndedIterator for Args {
60c5eb7d
XL
49 fn next_back(&mut self) -> Option<OsString> {
50 self.iter.next_back()
51 }
476ff2be
SL
52}
53
54mod imp {
60c5eb7d 55 use super::Args;
532ac7d7
XL
56 use crate::ffi::{CStr, OsString};
57 use crate::marker::PhantomData;
60c5eb7d 58 use crate::ptr;
416331ca 59 use libc;
476ff2be 60
532ac7d7 61 use crate::sys_common::mutex::Mutex;
476ff2be 62
416331ca
XL
63 static mut ARGC: isize = 0;
64 static mut ARGV: *const *const u8 = ptr::null();
476ff2be
SL
65 static LOCK: Mutex = Mutex::new();
66
67 pub unsafe fn init(argc: isize, argv: *const *const u8) {
94b46f34 68 let _guard = LOCK.lock();
416331ca
XL
69 ARGC = argc;
70 ARGV = argv;
476ff2be
SL
71 }
72
73 pub unsafe fn cleanup() {
94b46f34 74 let _guard = LOCK.lock();
416331ca
XL
75 ARGC = 0;
76 ARGV = ptr::null();
476ff2be
SL
77 }
78
79 pub fn args() -> Args {
60c5eb7d 80 Args { iter: clone().into_iter(), _dont_send_or_sync_me: PhantomData }
476ff2be
SL
81 }
82
416331ca 83 fn clone() -> Vec<OsString> {
476ff2be 84 unsafe {
94b46f34 85 let _guard = LOCK.lock();
60c5eb7d
XL
86 let ret = (0..ARGC)
87 .map(|i| {
88 let cstr = CStr::from_ptr(*ARGV.offset(i) as *const libc::c_char);
89 use crate::sys::vxworks::ext::ffi::OsStringExt;
90 OsStringExt::from_vec(cstr.to_bytes().to_vec())
91 })
92 .collect();
93 return ret;
476ff2be
SL
94 }
95 }
476ff2be 96}