]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/args.rs
Rollup merge of #33852 - arielb1:autoderef-iterator, r=eddyb
[rust.git] / src / libstd / sys / common / args.rs
1 // Copyright 2012-2015 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.
4 //
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.
10
11 //! Global storage for command line arguments
12 //!
13 //! The current incarnation of the Rust runtime expects for
14 //! the processes `argc` and `argv` arguments to be stored
15 //! in a globally-accessible location for use by the `os` module.
16 //!
17 //! Only valid to call on Linux. Mac and Windows use syscalls to
18 //! discover the command line arguments.
19 //!
20 //! FIXME #7756: Would be nice for this to not exist.
21
22 #![allow(dead_code)] // different code on OSX/linux/etc
23
24 use vec::Vec;
25
26 /// One-time global initialization.
27 pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) }
28
29 /// One-time global cleanup.
30 pub unsafe fn cleanup() { imp::cleanup() }
31
32 /// Make a clone of the global arguments.
33 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
34
35 #[cfg(any(target_os = "linux",
36           target_os = "android",
37           target_os = "freebsd",
38           target_os = "dragonfly",
39           target_os = "bitrig",
40           target_os = "netbsd",
41           target_os = "openbsd",
42           target_os = "solaris",
43           target_os = "emscripten"))]
44 mod imp {
45     use prelude::v1::*;
46
47     use libc::c_char;
48     use mem;
49     use ffi::CStr;
50
51     use sys_common::mutex::Mutex;
52
53     static mut GLOBAL_ARGS_PTR: usize = 0;
54     static LOCK: Mutex = Mutex::new();
55
56     pub unsafe fn init(argc: isize, argv: *const *const u8) {
57         let args = (0..argc).map(|i| {
58             CStr::from_ptr(*argv.offset(i) as *const c_char).to_bytes().to_vec()
59         }).collect();
60
61         LOCK.lock();
62         let ptr = get_global_ptr();
63         assert!((*ptr).is_none());
64         (*ptr) = Some(box args);
65         LOCK.unlock();
66     }
67
68     pub unsafe fn cleanup() {
69         LOCK.lock();
70         *get_global_ptr() = None;
71         LOCK.unlock();
72     }
73
74     pub fn clone() -> Option<Vec<Vec<u8>>> {
75         unsafe {
76             LOCK.lock();
77             let ptr = get_global_ptr();
78             let ret = (*ptr).as_ref().map(|s| (**s).clone());
79             LOCK.unlock();
80             return ret
81         }
82     }
83
84     fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
85         unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
86     }
87
88 }
89
90 #[cfg(any(target_os = "macos",
91           target_os = "ios",
92           target_os = "windows"))]
93 mod imp {
94     use vec::Vec;
95
96     pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
97     }
98
99     pub fn cleanup() {
100     }
101
102     pub fn clone() -> Option<Vec<Vec<u8>>> {
103         panic!()
104     }
105 }