]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/vxworks/args.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / vxworks / args.rs
1 #![allow(dead_code)] // runtime init functions not used during testing
2 use crate::ffi::OsString;
3 use crate::marker::PhantomData;
4 use crate::vec;
5
6 /// One-time global initialization.
7 pub unsafe fn init(argc: isize, argv: *const *const u8) {
8 imp::init(argc, argv)
9 }
10
11 /// One-time global cleanup.
12 pub unsafe fn cleanup() {
13 imp::cleanup()
14 }
15
16 /// Returns the command line arguments
17 pub fn args() -> Args {
18 imp::args()
19 }
20
21 pub struct Args {
22 iter: vec::IntoIter<OsString>,
23 _dont_send_or_sync_me: PhantomData<*mut ()>,
24 }
25
26 impl Args {
27 pub fn inner_debug(&self) -> &[OsString] {
28 self.iter.as_slice()
29 }
30 }
31
32 impl Iterator for Args {
33 type Item = OsString;
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 }
40 }
41
42 impl ExactSizeIterator for Args {
43 fn len(&self) -> usize {
44 self.iter.len()
45 }
46 }
47
48 impl DoubleEndedIterator for Args {
49 fn next_back(&mut self) -> Option<OsString> {
50 self.iter.next_back()
51 }
52 }
53
54 mod imp {
55 use super::Args;
56 use crate::ffi::{CStr, OsString};
57 use crate::marker::PhantomData;
58 use crate::ptr;
59 use libc;
60
61 use crate::sys_common::mutex::Mutex;
62
63 static mut ARGC: isize = 0;
64 static mut ARGV: *const *const u8 = ptr::null();
65 static LOCK: Mutex = Mutex::new();
66
67 pub unsafe fn init(argc: isize, argv: *const *const u8) {
68 let _guard = LOCK.lock();
69 ARGC = argc;
70 ARGV = argv;
71 }
72
73 pub unsafe fn cleanup() {
74 let _guard = LOCK.lock();
75 ARGC = 0;
76 ARGV = ptr::null();
77 }
78
79 pub fn args() -> Args {
80 Args { iter: clone().into_iter(), _dont_send_or_sync_me: PhantomData }
81 }
82
83 fn clone() -> Vec<OsString> {
84 unsafe {
85 let _guard = LOCK.lock();
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;
94 }
95 }
96 }