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
26 /// One-time global initialization.
27 pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) }
29 /// One-time global cleanup.
30 pub unsafe fn cleanup() { imp::cleanup() }
32 /// Make a clone of the global arguments.
33 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
35 #[cfg(any(target_os = "linux",
36 target_os = "android",
37 target_os = "freebsd",
38 target_os = "dragonfly",
41 target_os = "openbsd",
42 target_os = "solaris",
43 target_os = "emscripten"))]
51 use sys_common::mutex::Mutex;
53 static mut GLOBAL_ARGS_PTR: usize = 0;
54 static LOCK: Mutex = Mutex::new();
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()
62 let ptr = get_global_ptr();
63 assert!((*ptr).is_none());
64 (*ptr) = Some(box args);
68 pub unsafe fn cleanup() {
70 *get_global_ptr() = None;
74 pub fn clone() -> Option<Vec<Vec<u8>>> {
77 let ptr = get_global_ptr();
78 let ret = (*ptr).as_ref().map(|s| (**s).clone());
84 fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
85 unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
90 #[cfg(any(target_os = "macos",
92 target_os = "windows"))]
96 pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
102 pub fn clone() -> Option<Vec<Vec<u8>>> {