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.
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.
11 //! Global storage for command line arguments
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.
17 //! Only valid to call on Linux. Mac and Windows use syscalls to
18 //! discover the command line arguments.
20 //! FIXME #7756: Would be nice for this to not exist.
22 #![allow(dead_code)] // different code on OSX/linux/etc
24 /// One-time global initialization.
25 pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) }
27 /// One-time global cleanup.
28 pub unsafe fn cleanup() { imp::cleanup() }
30 /// Make a clone of the global arguments.
31 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
33 #[cfg(any(target_os = "linux",
34 target_os = "android",
35 target_os = "freebsd",
36 target_os = "dragonfly",
39 target_os = "openbsd",
40 target_os = "solaris",
41 target_os = "emscripten"))]
47 use sys_common::mutex::Mutex;
49 static mut GLOBAL_ARGS_PTR: usize = 0;
50 static LOCK: Mutex = Mutex::new();
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()
58 let ptr = get_global_ptr();
59 assert!((*ptr).is_none());
60 (*ptr) = Some(box args);
64 pub unsafe fn cleanup() {
66 *get_global_ptr() = None;
70 pub fn clone() -> Option<Vec<Vec<u8>>> {
73 let ptr = get_global_ptr();
74 let ret = (*ptr).as_ref().map(|s| (**s).clone());
80 fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
81 unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
86 #[cfg(any(target_os = "macos",
88 target_os = "windows"))]
90 pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
96 pub fn clone() -> Option<Vec<Vec<u8>>> {