]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/args.rs
Auto merge of #35856 - phimuemue:master, r=brson
[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 /// One-time global initialization.
25 pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) }
26
27 /// One-time global cleanup.
28 pub unsafe fn cleanup() { imp::cleanup() }
29
30 /// Make a clone of the global arguments.
31 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
32
33 #[cfg(any(target_os = "linux",
34           target_os = "android",
35           target_os = "freebsd",
36           target_os = "dragonfly",
37           target_os = "bitrig",
38           target_os = "netbsd",
39           target_os = "openbsd",
40           target_os = "solaris",
41           target_os = "emscripten"))]
42 mod imp {
43     use libc::c_char;
44     use mem;
45     use ffi::CStr;
46
47     use sys_common::mutex::Mutex;
48
49     static mut GLOBAL_ARGS_PTR: usize = 0;
50     static LOCK: Mutex = Mutex::new();
51
52     pub unsafe fn init(argc: isize, argv: *const *const u8) {
53         let args = (0..argc).map(|i| {
54             CStr::from_ptr(*argv.offset(i) as *const c_char).to_bytes().to_vec()
55         }).collect();
56
57         LOCK.lock();
58         let ptr = get_global_ptr();
59         assert!((*ptr).is_none());
60         (*ptr) = Some(box args);
61         LOCK.unlock();
62     }
63
64     pub unsafe fn cleanup() {
65         LOCK.lock();
66         *get_global_ptr() = None;
67         LOCK.unlock();
68     }
69
70     pub fn clone() -> Option<Vec<Vec<u8>>> {
71         unsafe {
72             LOCK.lock();
73             let ptr = get_global_ptr();
74             let ret = (*ptr).as_ref().map(|s| (**s).clone());
75             LOCK.unlock();
76             return ret
77         }
78     }
79
80     fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
81         unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
82     }
83
84 }
85
86 #[cfg(any(target_os = "macos",
87           target_os = "ios",
88           target_os = "windows"))]
89 mod imp {
90     pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
91     }
92
93     pub fn cleanup() {
94     }
95
96     pub fn clone() -> Option<Vec<Vec<u8>>> {
97         panic!()
98     }
99 }