]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/vxworks/args.rs
New upstream version 1.48.0+dfsg1
[rustc.git] / library / std / src / 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
60 use crate::sys_common::mutex::StaticMutex;
61
62 static mut ARGC: isize = 0;
63 static mut ARGV: *const *const u8 = ptr::null();
64 static LOCK: StaticMutex = StaticMutex::new();
65
66 pub unsafe fn init(argc: isize, argv: *const *const u8) {
67 let _guard = LOCK.lock();
68 ARGC = argc;
69 ARGV = argv;
70 }
71
72 pub unsafe fn cleanup() {
73 let _guard = LOCK.lock();
74 ARGC = 0;
75 ARGV = ptr::null();
76 }
77
78 pub fn args() -> Args {
79 Args { iter: clone().into_iter(), _dont_send_or_sync_me: PhantomData }
80 }
81
82 fn clone() -> Vec<OsString> {
83 unsafe {
84 let _guard = LOCK.lock();
85 let ret = (0..ARGC)
86 .map(|i| {
87 let cstr = CStr::from_ptr(*ARGV.offset(i) as *const libc::c_char);
88 use crate::sys::vxworks::ext::ffi::OsStringExt;
89 OsStringExt::from_vec(cstr.to_bytes().to_vec())
90 })
91 .collect();
92 return ret;
93 }
94 }
95 }