2015-01-27 12:20:58 -08:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Inspection and manipulation of the process's environment.
|
|
|
|
//!
|
2017-01-12 23:10:38 -05:00
|
|
|
//! This module contains functions to inspect various aspects such as
|
2015-07-16 20:43:36 -07:00
|
|
|
//! environment variables, process arguments, the current directory, and various
|
2015-01-27 12:20:58 -08:00
|
|
|
//! other important directories.
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#![stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
use error::Error;
|
2015-03-30 11:00:05 -07:00
|
|
|
use ffi::{OsStr, OsString};
|
2015-01-27 12:20:58 -08:00
|
|
|
use fmt;
|
2015-02-23 10:59:17 -08:00
|
|
|
use io;
|
2015-03-23 15:18:40 -07:00
|
|
|
use path::{Path, PathBuf};
|
2016-09-29 22:00:44 +00:00
|
|
|
use sys;
|
2015-01-27 12:20:58 -08:00
|
|
|
use sys::os as os_imp;
|
|
|
|
|
2017-02-07 19:43:22 +01:00
|
|
|
/// Returns the current working directory as a [`PathBuf`].
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2017-02-07 19:43:22 +01:00
|
|
|
/// Returns an [`Err`] if the current working directory value is invalid.
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Possible cases:
|
|
|
|
///
|
|
|
|
/// * Current directory does not exist.
|
|
|
|
/// * There are insufficient permissions to access the current directory.
|
|
|
|
///
|
2017-02-07 19:43:22 +01:00
|
|
|
/// [`PathBuf`]: ../../std/path/struct.PathBuf.html
|
|
|
|
/// [`Err`]: ../../std/result/enum.Result.html#method.err
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// // We assume that we are in a valid directory.
|
|
|
|
/// let p = env::current_dir().unwrap();
|
|
|
|
/// println!("The current directory is {}", p.display());
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 10:59:17 -08:00
|
|
|
pub fn current_dir() -> io::Result<PathBuf> {
|
2015-01-27 12:20:58 -08:00
|
|
|
os_imp::getcwd()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Changes the current working directory to the specified path, returning
|
|
|
|
/// whether the change was completed successfully or not.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
2015-02-23 10:59:17 -08:00
|
|
|
/// use std::path::Path;
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
|
|
|
/// let root = Path::new("/");
|
|
|
|
/// assert!(env::set_current_dir(&root).is_ok());
|
|
|
|
/// println!("Successfully changed working directory to {}!", root.display());
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 16:06:21 -07:00
|
|
|
pub fn set_current_dir<P: AsRef<Path>>(p: P) -> io::Result<()> {
|
2015-03-18 09:14:54 -07:00
|
|
|
os_imp::chdir(p.as_ref())
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over a snapshot of the environment variables of this process.
|
|
|
|
///
|
2017-01-12 23:10:38 -05:00
|
|
|
/// This structure is created through the [`std::env::vars`] function.
|
|
|
|
///
|
|
|
|
/// [`std::env::vars`]: fn.vars.html
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
pub struct Vars { inner: VarsOs }
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
/// An iterator over a snapshot of the environment variables of this process.
|
|
|
|
///
|
2017-01-12 23:10:38 -05:00
|
|
|
/// This structure is created through the [`std::env::vars_os`] function.
|
|
|
|
///
|
|
|
|
/// [`std::env::vars_os`]: fn.vars_os.html
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
pub struct VarsOs { inner: os_imp::Env }
|
|
|
|
|
|
|
|
/// Returns an iterator of (variable, value) pairs of strings, for all the
|
|
|
|
/// environment variables of the current process.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
|
|
|
/// The returned iterator contains a snapshot of the process's environment
|
2016-09-14 22:41:17 +02:00
|
|
|
/// variables at the time of this invocation. Modifications to environment
|
2015-01-27 12:20:58 -08:00
|
|
|
/// variables afterwards will not be reflected in the returned iterator.
|
|
|
|
///
|
2015-02-11 11:47:53 -08:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// While iterating, the returned iterator will panic if any key or value in the
|
|
|
|
/// environment is not valid unicode. If this is not desired, consider using the
|
2017-02-18 14:44:56 +01:00
|
|
|
/// [`env::vars_os`] function.
|
|
|
|
///
|
|
|
|
/// [`env::vars_os`]: fn.vars_os.html
|
2015-02-11 11:47:53 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// // We will iterate through the references to the element returned by
|
|
|
|
/// // env::vars();
|
|
|
|
/// for (key, value) in env::vars() {
|
2015-02-11 11:47:53 -08:00
|
|
|
/// println!("{}: {}", key, value);
|
2015-01-27 12:20:58 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub fn vars() -> Vars {
|
2015-02-11 11:47:53 -08:00
|
|
|
Vars { inner: vars_os() }
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
/// Returns an iterator of (variable, value) pairs of OS strings, for all the
|
|
|
|
/// environment variables of the current process.
|
|
|
|
///
|
|
|
|
/// The returned iterator contains a snapshot of the process's environment
|
2016-09-14 22:41:17 +02:00
|
|
|
/// variables at the time of this invocation. Modifications to environment
|
2015-02-11 11:47:53 -08:00
|
|
|
/// variables afterwards will not be reflected in the returned iterator.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
2015-02-11 11:47:53 -08:00
|
|
|
/// // We will iterate through the references to the element returned by
|
|
|
|
/// // env::vars_os();
|
|
|
|
/// for (key, value) in env::vars_os() {
|
|
|
|
/// println!("{:?}: {:?}", key, value);
|
2015-01-27 12:20:58 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
pub fn vars_os() -> VarsOs {
|
|
|
|
VarsOs { inner: os_imp::env() }
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
impl Iterator for Vars {
|
|
|
|
type Item = (String, String);
|
|
|
|
fn next(&mut self) -> Option<(String, String)> {
|
|
|
|
self.inner.next().map(|(a, b)| {
|
|
|
|
(a.into_string().unwrap(), b.into_string().unwrap())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Vars {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Vars { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
impl Iterator for VarsOs {
|
|
|
|
type Item = (OsString, OsString);
|
|
|
|
fn next(&mut self) -> Option<(OsString, OsString)> { self.inner.next() }
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for VarsOs {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("VarsOs { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Fetches the environment variable `key` from the current process.
|
|
|
|
///
|
2017-02-18 14:44:56 +01:00
|
|
|
/// The returned result is [`Ok(s)`] if the environment variable is present and is
|
2015-01-27 12:20:58 -08:00
|
|
|
/// valid unicode. If the environment variable is not present, or it is not
|
2017-02-18 14:44:56 +01:00
|
|
|
/// valid unicode, then [`Err`] will be returned.
|
|
|
|
///
|
|
|
|
/// [`Ok(s)`]: ../result/enum.Result.html#variant.Ok
|
|
|
|
/// [`Err`]: ../result/enum.Result.html#variant.Err
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// let key = "HOME";
|
2015-02-11 11:47:53 -08:00
|
|
|
/// match env::var(key) {
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Ok(val) => println!("{}: {:?}", key, val),
|
|
|
|
/// Err(e) => println!("couldn't interpret {}: {}", key, e),
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 16:06:21 -07:00
|
|
|
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
|
2015-09-09 22:37:59 +03:00
|
|
|
_var(key.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _var(key: &OsStr) -> Result<String, VarError> {
|
2015-02-11 11:47:53 -08:00
|
|
|
match var_os(key) {
|
2015-01-27 12:20:58 -08:00
|
|
|
Some(s) => s.into_string().map_err(VarError::NotUnicode),
|
|
|
|
None => Err(VarError::NotPresent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
/// Fetches the environment variable `key` from the current process, returning
|
2017-02-18 14:44:56 +01:00
|
|
|
/// [`None`] if the variable isn't set.
|
|
|
|
///
|
|
|
|
/// [`None`]: ../option/enum.Option.html#variant.None
|
2015-02-11 11:47:53 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-02-11 11:47:53 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-02-11 11:47:53 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// let key = "HOME";
|
|
|
|
/// match env::var_os(key) {
|
|
|
|
/// Some(val) => println!("{}: {:?}", key, val),
|
|
|
|
/// None => println!("{} is not defined in the environment.", key)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 16:06:21 -07:00
|
|
|
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
|
2015-09-09 22:37:59 +03:00
|
|
|
_var_os(key.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _var_os(key: &OsStr) -> Option<OsString> {
|
2015-10-25 10:19:35 -07:00
|
|
|
os_imp::getenv(key).unwrap_or_else(|e| {
|
|
|
|
panic!("failed to get environment variable `{:?}`: {}", key, e)
|
|
|
|
})
|
2015-02-11 11:47:53 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 23:10:38 -05:00
|
|
|
/// Possible errors from the [`env::var`] function.
|
|
|
|
///
|
2017-01-27 18:08:51 +00:00
|
|
|
/// [`env::var`]: fn.var.html
|
2015-01-27 12:20:58 -08:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub enum VarError {
|
|
|
|
/// The specified environment variable was not present in the current
|
|
|
|
/// process's environment.
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
NotPresent,
|
|
|
|
|
|
|
|
/// The specified environment variable was found, but it did not contain
|
|
|
|
/// valid unicode data. The found data is returned as a payload of this
|
|
|
|
/// variant.
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-02-19 16:08:36 -08:00
|
|
|
NotUnicode(#[stable(feature = "env", since = "1.0.0")] OsString),
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl fmt::Display for VarError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
VarError::NotPresent => write!(f, "environment variable not found"),
|
|
|
|
VarError::NotUnicode(ref s) => {
|
|
|
|
write!(f, "environment variable was not valid unicode: {:?}", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl Error for VarError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
VarError::NotPresent => "environment variable not found",
|
|
|
|
VarError::NotUnicode(..) => "environment variable was not valid unicode",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the environment variable `k` to the value `v` for the currently running
|
|
|
|
/// process.
|
|
|
|
///
|
2015-04-23 16:59:36 -04:00
|
|
|
/// Note that while concurrent access to environment variables is safe in Rust,
|
|
|
|
/// some platforms only expose inherently unsafe non-threadsafe APIs for
|
|
|
|
/// inspecting the environment. As a result extra care needs to be taken when
|
|
|
|
/// auditing calls to unsafe external FFI functions to ensure that any external
|
|
|
|
/// environment accesses are properly synchronized with accesses in Rust.
|
|
|
|
///
|
|
|
|
/// Discussion of this unsafety on Unix may be found in:
|
|
|
|
///
|
|
|
|
/// - [Austin Group Bugzilla](http://austingroupbugs.net/view.php?id=188)
|
|
|
|
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
|
|
|
|
///
|
2015-10-25 12:04:29 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
2015-10-25 20:03:42 +00:00
|
|
|
/// This function may panic if `key` is empty, contains an ASCII equals sign
|
|
|
|
/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
|
|
|
|
/// character.
|
2015-10-25 12:04:29 +00:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// let key = "KEY";
|
|
|
|
/// env::set_var(key, "VALUE");
|
2015-02-11 11:47:53 -08:00
|
|
|
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
|
2015-01-27 12:20:58 -08:00
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 16:06:21 -07:00
|
|
|
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(k: K, v: V) {
|
2015-09-09 22:37:59 +03:00
|
|
|
_set_var(k.as_ref(), v.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _set_var(k: &OsStr, v: &OsStr) {
|
2015-10-25 10:19:35 -07:00
|
|
|
os_imp::setenv(k, v).unwrap_or_else(|e| {
|
|
|
|
panic!("failed to set environment variable `{:?}` to `{:?}`: {}",
|
|
|
|
k, v, e)
|
|
|
|
})
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Removes an environment variable from the environment of the currently running process.
|
2015-03-31 00:19:31 -04:00
|
|
|
///
|
2015-04-23 16:59:36 -04:00
|
|
|
/// Note that while concurrent access to environment variables is safe in Rust,
|
|
|
|
/// some platforms only expose inherently unsafe non-threadsafe APIs for
|
|
|
|
/// inspecting the environment. As a result extra care needs to be taken when
|
|
|
|
/// auditing calls to unsafe external FFI functions to ensure that any external
|
|
|
|
/// environment accesses are properly synchronized with accesses in Rust.
|
|
|
|
///
|
|
|
|
/// Discussion of this unsafety on Unix may be found in:
|
|
|
|
///
|
|
|
|
/// - [Austin Group Bugzilla](http://austingroupbugs.net/view.php?id=188)
|
|
|
|
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
|
|
|
|
///
|
2015-10-25 12:04:29 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
2015-10-25 20:03:42 +00:00
|
|
|
/// This function may panic if `key` is empty, contains an ASCII equals sign
|
|
|
|
/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
|
|
|
|
/// character.
|
2015-10-25 12:04:29 +00:00
|
|
|
///
|
2015-03-31 00:19:31 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// let key = "KEY";
|
|
|
|
/// env::set_var(key, "VALUE");
|
|
|
|
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
|
|
|
|
///
|
|
|
|
/// env::remove_var(key);
|
|
|
|
/// assert!(env::var(key).is_err());
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 16:06:21 -07:00
|
|
|
pub fn remove_var<K: AsRef<OsStr>>(k: K) {
|
2015-09-09 22:37:59 +03:00
|
|
|
_remove_var(k.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _remove_var(k: &OsStr) {
|
2015-10-25 10:19:35 -07:00
|
|
|
os_imp::unsetenv(k).unwrap_or_else(|e| {
|
|
|
|
panic!("failed to remove environment variable `{:?}`: {}", k, e)
|
|
|
|
})
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-11-29 13:36:01 +00:00
|
|
|
/// An iterator over `PathBuf` instances for parsing an environment variable
|
2015-01-27 12:20:58 -08:00
|
|
|
/// according to platform-specific conventions.
|
|
|
|
///
|
|
|
|
/// This structure is returned from `std::env::split_paths`.
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub struct SplitPaths<'a> { inner: os_imp::SplitPaths<'a> }
|
|
|
|
|
|
|
|
/// Parses input according to platform conventions for the `PATH`
|
|
|
|
/// environment variable.
|
|
|
|
///
|
|
|
|
/// Returns an iterator over the paths contained in `unparsed`.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// let key = "PATH";
|
2015-02-11 11:47:53 -08:00
|
|
|
/// match env::var_os(key) {
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Some(paths) => {
|
|
|
|
/// for path in env::split_paths(&paths) {
|
|
|
|
/// println!("'{}'", path.display());
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// None => println!("{} is not defined in the environment.", key)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-03-30 11:00:05 -07:00
|
|
|
pub fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths {
|
|
|
|
SplitPaths { inner: os_imp::split_paths(unparsed.as_ref()) }
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl<'a> Iterator for SplitPaths<'a> {
|
2015-02-23 10:59:17 -08:00
|
|
|
type Item = PathBuf;
|
|
|
|
fn next(&mut self) -> Option<PathBuf> { self.inner.next() }
|
2015-01-27 12:20:58 -08:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl<'a> fmt::Debug for SplitPaths<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("SplitPaths { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Error type returned from `std::env::join_paths` when paths fail to be
|
|
|
|
/// joined.
|
|
|
|
#[derive(Debug)]
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub struct JoinPathsError {
|
|
|
|
inner: os_imp::JoinPathsError
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:42:01 +01:00
|
|
|
/// Joins a collection of [`Path`]s appropriately for the `PATH`
|
2015-01-27 12:20:58 -08:00
|
|
|
/// environment variable.
|
|
|
|
///
|
2017-02-08 18:42:01 +01:00
|
|
|
/// Returns an [`OsString`] on success.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2017-02-08 18:42:01 +01:00
|
|
|
/// Returns an [`Err`][err] (containing an error message) if one of the input
|
|
|
|
/// [`Path`]s contains an invalid character for constructing the `PATH`
|
2015-01-27 12:20:58 -08:00
|
|
|
/// variable (a double quote on Windows or a colon on Unix).
|
|
|
|
///
|
2017-02-08 18:42:01 +01:00
|
|
|
/// [`Path`]: ../../std/path/struct.Path.html
|
|
|
|
/// [`OsString`]: ../../std/ffi/struct.OsString.html
|
|
|
|
/// [err]: ../../std/result/enum.Result.html#variant.Err
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
2015-02-23 10:59:17 -08:00
|
|
|
/// use std::path::PathBuf;
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-02-11 11:47:53 -08:00
|
|
|
/// if let Some(path) = env::var_os("PATH") {
|
2015-01-27 12:20:58 -08:00
|
|
|
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
2015-03-23 15:54:39 -07:00
|
|
|
/// paths.push(PathBuf::from("/home/xyz/bin"));
|
2015-06-10 17:22:20 +01:00
|
|
|
/// let new_path = env::join_paths(paths).unwrap();
|
2015-01-27 12:20:58 -08:00
|
|
|
/// env::set_var("PATH", &new_path);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
|
2015-03-30 11:00:05 -07:00
|
|
|
where I: IntoIterator<Item=T>, T: AsRef<OsStr>
|
2015-01-27 12:20:58 -08:00
|
|
|
{
|
2015-03-09 08:49:10 -07:00
|
|
|
os_imp::join_paths(paths.into_iter()).map_err(|e| {
|
2015-01-27 12:20:58 -08:00
|
|
|
JoinPathsError { inner: e }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl fmt::Display for JoinPathsError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl Error for JoinPathsError {
|
|
|
|
fn description(&self) -> &str { self.inner.description() }
|
|
|
|
}
|
|
|
|
|
2016-03-30 09:01:21 +02:00
|
|
|
/// Returns the path of the current user's home directory if known.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
|
|
|
/// # Unix
|
|
|
|
///
|
|
|
|
/// Returns the value of the 'HOME' environment variable if it is set
|
2015-11-08 15:34:16 +00:00
|
|
|
/// and not equal to the empty string. Otherwise, it tries to determine the
|
|
|
|
/// home directory by invoking the `getpwuid_r` function on the UID of the
|
|
|
|
/// current user.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
|
|
|
/// # Windows
|
|
|
|
///
|
|
|
|
/// Returns the value of the 'HOME' environment variable if it is
|
|
|
|
/// set and not equal to the empty string. Otherwise, returns the value of the
|
|
|
|
/// 'USERPROFILE' environment variable if it is set and not equal to the empty
|
2015-10-11 03:50:26 +09:00
|
|
|
/// string. If both do not exist, [`GetUserProfileDirectory`][msdn] is used to
|
|
|
|
/// return the appropriate path.
|
|
|
|
///
|
|
|
|
/// [msdn]: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762280(v=vs.85).aspx
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// match env::home_dir() {
|
2016-02-25 23:21:55 +02:00
|
|
|
/// Some(path) => println!("{}", path.display()),
|
2016-02-25 23:19:47 +02:00
|
|
|
/// None => println!("Impossible to get your home dir!"),
|
2015-01-27 12:20:58 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 10:59:17 -08:00
|
|
|
pub fn home_dir() -> Option<PathBuf> {
|
2015-01-27 12:20:58 -08:00
|
|
|
os_imp::home_dir()
|
|
|
|
}
|
|
|
|
|
2016-03-30 09:01:21 +02:00
|
|
|
/// Returns the path of a temporary directory.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2016-05-12 00:05:25 -04:00
|
|
|
/// On Unix, returns the value of the `TMPDIR` environment variable if it is
|
|
|
|
/// set, otherwise for non-Android it returns `/tmp`. If Android, since there
|
|
|
|
/// is no global temporary folder (it is usually allocated per-app), it returns
|
|
|
|
/// `/data/local/tmp`.
|
|
|
|
///
|
|
|
|
/// On Windows, returns the value of, in order, the `TMP`, `TEMP`,
|
|
|
|
/// `USERPROFILE` environment variable if any are set and not the empty
|
|
|
|
/// string. Otherwise, `temp_dir` returns the path of the Windows directory.
|
|
|
|
/// This behavior is identical to that of [`GetTempPath`][msdn], which this
|
|
|
|
/// function uses internally.
|
2015-10-11 03:50:26 +09:00
|
|
|
///
|
|
|
|
/// [msdn]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992(v=vs.85).aspx
|
2015-03-31 00:19:31 -04:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::env;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> std::io::Result<()> {
|
|
|
|
/// let mut dir = env::temp_dir();
|
|
|
|
/// dir.push("foo.txt");
|
|
|
|
///
|
2016-12-28 14:32:35 +05:30
|
|
|
/// let f = File::create(dir)?;
|
2015-03-31 00:19:31 -04:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 10:59:17 -08:00
|
|
|
pub fn temp_dir() -> PathBuf {
|
2015-01-27 12:20:58 -08:00
|
|
|
os_imp::temp_dir()
|
|
|
|
}
|
|
|
|
|
2016-03-30 09:01:21 +02:00
|
|
|
/// Returns the full filesystem path of the current running executable.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2016-03-30 09:01:21 +02:00
|
|
|
/// The path returned is not necessarily a "real path" of the executable as
|
2015-01-27 12:20:58 -08:00
|
|
|
/// there may be intermediate symlinks.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
2016-03-30 09:01:21 +02:00
|
|
|
/// Acquiring the path of the current executable is a platform-specific operation
|
2015-01-27 12:20:58 -08:00
|
|
|
/// that can fail for a good number of reasons. Some errors can include, but not
|
2016-02-25 22:52:02 +02:00
|
|
|
/// be limited to, filesystem operations failing or general syscall failures.
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2016-05-09 19:45:12 -04:00
|
|
|
/// # Security
|
|
|
|
///
|
2016-07-19 12:32:56 -04:00
|
|
|
/// The output of this function should not be used in anything that might have
|
|
|
|
/// security implications. For example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn main() {
|
|
|
|
/// println!("{:?}", std::env::current_exe());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// On Linux systems, if this is compiled as `foo`:
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// $ rustc foo.rs
|
|
|
|
/// $ ./foo
|
|
|
|
/// Ok("/home/alex/foo")
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// And you make a symbolic link of the program:
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// $ ln foo bar
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// When you run it, you won't get the original executable, you'll get the
|
|
|
|
/// symlink:
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// $ ./bar
|
|
|
|
/// Ok("/home/alex/bar")
|
|
|
|
/// ```
|
|
|
|
///
|
2016-07-20 16:43:53 -04:00
|
|
|
/// This sort of behavior has been known to [lead to privilege escalation] when
|
2016-07-19 12:32:56 -04:00
|
|
|
/// used incorrectly, for example.
|
|
|
|
///
|
2016-08-11 19:04:11 -04:00
|
|
|
/// [lead to privilege escalation]: http://securityvulns.com/Wdocument183.html
|
2016-05-09 19:45:12 -04:00
|
|
|
///
|
2015-01-27 12:20:58 -08:00
|
|
|
/// # Examples
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// match env::current_exe() {
|
|
|
|
/// Ok(exe_path) => println!("Path of this executable is: {}",
|
|
|
|
/// exe_path.display()),
|
|
|
|
/// Err(e) => println!("failed to get current exe path: {}", e),
|
|
|
|
/// };
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 10:59:17 -08:00
|
|
|
pub fn current_exe() -> io::Result<PathBuf> {
|
2015-01-27 12:20:58 -08:00
|
|
|
os_imp::current_exe()
|
|
|
|
}
|
|
|
|
|
2016-11-21 16:12:14 -06:00
|
|
|
/// An iterator over the arguments of a process, yielding a [`String`] value
|
2015-01-27 12:20:58 -08:00
|
|
|
/// for each argument.
|
|
|
|
///
|
2017-01-12 23:10:38 -05:00
|
|
|
/// This structure is created through the [`std::env::args`] function.
|
2016-11-21 16:12:14 -06:00
|
|
|
///
|
2017-03-05 16:39:24 -05:00
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
|
|
|
/// set to arbitrary text, and may not even exist. This means this property should
|
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
///
|
2016-11-21 16:12:14 -06:00
|
|
|
/// [`String`]: ../string/struct.String.html
|
|
|
|
/// [`std::env::args`]: ./fn.args.html
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
pub struct Args { inner: ArgsOs }
|
|
|
|
|
2016-11-21 16:12:14 -06:00
|
|
|
/// An iterator over the arguments of a process, yielding an [`OsString`] value
|
2015-02-11 11:47:53 -08:00
|
|
|
/// for each argument.
|
|
|
|
///
|
2017-01-12 23:10:38 -05:00
|
|
|
/// This structure is created through the [`std::env::args_os`] function.
|
2016-11-21 16:12:14 -06:00
|
|
|
///
|
2017-03-05 16:39:24 -05:00
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
|
|
|
/// set to arbitrary text, and may not even exist. This means this property should
|
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
///
|
2016-11-21 16:12:14 -06:00
|
|
|
/// [`OsString`]: ../ffi/struct.OsString.html
|
|
|
|
/// [`std::env::args_os`]: ./fn.args_os.html
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-29 22:00:44 +00:00
|
|
|
pub struct ArgsOs { inner: sys::args::Args }
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
/// via the command line).
|
|
|
|
///
|
2016-03-30 09:01:21 +02:00
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2016-02-18 22:59:03 +02:00
|
|
|
/// set to arbitrary text, and may not even exist. This means this property should
|
2015-01-27 12:20:58 -08:00
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
///
|
2015-02-11 11:47:53 -08:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// The returned iterator will panic during iteration if any argument to the
|
2016-02-18 23:13:22 +02:00
|
|
|
/// process is not valid unicode. If this is not desired,
|
2016-11-21 16:12:14 -06:00
|
|
|
/// use the [`args_os`] function instead.
|
2015-02-11 11:47:53 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-27 12:20:58 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 12:20:58 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// // Prints each argument on a separate line
|
|
|
|
/// for argument in env::args() {
|
2015-02-11 11:47:53 -08:00
|
|
|
/// println!("{}", argument);
|
2015-01-27 12:20:58 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-11-21 16:12:14 -06:00
|
|
|
///
|
|
|
|
/// [`args_os`]: ./fn.args_os.html
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub fn args() -> Args {
|
2015-02-11 11:47:53 -08:00
|
|
|
Args { inner: args_os() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
/// via the command line).
|
|
|
|
///
|
2016-03-30 09:01:21 +02:00
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2015-02-11 11:47:53 -08:00
|
|
|
/// set to arbitrary text, and it may not even exist, so this property should
|
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-02-11 11:47:53 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-02-11 11:47:53 -08:00
|
|
|
/// use std::env;
|
|
|
|
///
|
|
|
|
/// // Prints each argument on a separate line
|
|
|
|
/// for argument in env::args_os() {
|
|
|
|
/// println!("{:?}", argument);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
pub fn args_os() -> ArgsOs {
|
2016-09-29 22:00:44 +00:00
|
|
|
ArgsOs { inner: sys::args::args() }
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
impl Iterator for Args {
|
2015-02-11 11:47:53 -08:00
|
|
|
type Item = String;
|
|
|
|
fn next(&mut self) -> Option<String> {
|
|
|
|
self.inner.next().map(|s| s.into_string().unwrap())
|
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-16 12:15:30 +02:00
|
|
|
impl ExactSizeIterator for Args {
|
|
|
|
fn len(&self) -> usize { self.inner.len() }
|
2016-12-03 21:59:00 +01:00
|
|
|
fn is_empty(&self) -> bool { self.inner.is_empty() }
|
2015-02-16 12:15:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 16:37:44 +02:00
|
|
|
#[stable(feature = "env_iterators", since = "1.11.0")]
|
|
|
|
impl DoubleEndedIterator for Args {
|
|
|
|
fn next_back(&mut self) -> Option<String> {
|
|
|
|
self.inner.next_back().map(|s| s.into_string().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for Args {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Args { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 11:47:53 -08:00
|
|
|
impl Iterator for ArgsOs {
|
2015-01-27 12:20:58 -08:00
|
|
|
type Item = OsString;
|
|
|
|
fn next(&mut self) -> Option<OsString> { self.inner.next() }
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-16 12:15:30 +02:00
|
|
|
impl ExactSizeIterator for ArgsOs {
|
|
|
|
fn len(&self) -> usize { self.inner.len() }
|
2016-12-03 21:59:00 +01:00
|
|
|
fn is_empty(&self) -> bool { self.inner.is_empty() }
|
2015-02-16 12:15:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 16:37:44 +02:00
|
|
|
#[stable(feature = "env_iterators", since = "1.11.0")]
|
|
|
|
impl DoubleEndedIterator for ArgsOs {
|
|
|
|
fn next_back(&mut self) -> Option<OsString> { self.inner.next_back() }
|
|
|
|
}
|
2016-11-25 13:21:49 -05:00
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
impl fmt::Debug for ArgsOs {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("ArgsOs { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
/// Constants associated with the current target
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod consts {
|
2016-09-21 19:50:30 +00:00
|
|
|
use sys::env::os;
|
|
|
|
|
2016-02-18 22:46:03 +02:00
|
|
|
/// A string describing the architecture of the CPU that is currently
|
2015-02-27 10:59:59 -08:00
|
|
|
/// in use.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - x86
|
|
|
|
/// - x86_64
|
|
|
|
/// - arm
|
|
|
|
/// - aarch64
|
|
|
|
/// - mips
|
2016-08-27 01:39:29 -05:00
|
|
|
/// - mips64
|
2015-05-20 23:19:55 -04:00
|
|
|
/// - powerpc
|
2015-12-28 21:09:06 +00:00
|
|
|
/// - powerpc64
|
2016-09-09 23:00:23 +02:00
|
|
|
/// - s390x
|
2016-12-06 15:57:43 -06:00
|
|
|
/// - sparc64
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
|
|
|
pub const ARCH: &'static str = super::arch::ARCH;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2016-04-05 17:55:14 +02:00
|
|
|
/// The family of the operating system. Example value is `unix`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - unix
|
|
|
|
/// - windows
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const FAMILY: &'static str = os::FAMILY;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
2016-04-05 17:55:14 +02:00
|
|
|
/// A string describing the specific operating system in use.
|
|
|
|
/// Example value is `linux`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - linux
|
|
|
|
/// - macos
|
|
|
|
/// - ios
|
|
|
|
/// - freebsd
|
|
|
|
/// - dragonfly
|
|
|
|
/// - bitrig
|
2015-06-30 20:37:11 -07:00
|
|
|
/// - netbsd
|
2015-05-20 23:19:55 -04:00
|
|
|
/// - openbsd
|
2016-01-28 14:02:31 +03:00
|
|
|
/// - solaris
|
2015-05-20 23:19:55 -04:00
|
|
|
/// - android
|
|
|
|
/// - windows
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const OS: &'static str = os::OS;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
2016-04-05 17:55:14 +02:00
|
|
|
/// platform. Example value is `lib`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - lib
|
|
|
|
/// - `""` (an empty string)
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const DLL_PREFIX: &'static str = os::DLL_PREFIX;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
2016-04-05 17:55:14 +02:00
|
|
|
/// platform. Example value is `.so`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - .so
|
|
|
|
/// - .dylib
|
|
|
|
/// - .dll
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const DLL_SUFFIX: &'static str = os::DLL_SUFFIX;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Specifies the file extension used for shared libraries on this
|
2016-04-05 17:55:14 +02:00
|
|
|
/// platform that goes after the dot. Example value is `so`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
2015-12-01 16:53:48 -05:00
|
|
|
/// - so
|
|
|
|
/// - dylib
|
|
|
|
/// - dll
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const DLL_EXTENSION: &'static str = os::DLL_EXTENSION;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
2016-04-05 17:55:14 +02:00
|
|
|
/// platform. Example value is `.exe`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
2015-12-01 16:53:48 -05:00
|
|
|
/// - .exe
|
|
|
|
/// - .nexe
|
|
|
|
/// - .pexe
|
2015-05-20 23:19:55 -04:00
|
|
|
/// - `""` (an empty string)
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const EXE_SUFFIX: &'static str = os::EXE_SUFFIX;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
2016-04-05 17:55:14 +02:00
|
|
|
/// on this platform. Example value is `exe`.
|
2015-05-20 23:19:55 -04:00
|
|
|
///
|
|
|
|
/// Some possible values:
|
|
|
|
///
|
|
|
|
/// - exe
|
|
|
|
/// - `""` (an empty string)
|
2015-02-27 10:59:59 -08:00
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-09-21 19:50:30 +00:00
|
|
|
pub const EXE_EXTENSION: &'static str = os::EXE_EXTENSION;
|
2016-09-24 23:38:56 -05:00
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
#[cfg(target_arch = "x86")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "x86";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "x86_64";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "arm")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "arm";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "aarch64";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "mips";
|
|
|
|
}
|
|
|
|
|
2016-08-27 01:39:29 -05:00
|
|
|
#[cfg(target_arch = "mips64")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "mips64";
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
#[cfg(target_arch = "powerpc")]
|
2015-02-27 10:59:59 -08:00
|
|
|
mod arch {
|
2015-01-27 12:20:58 -08:00
|
|
|
pub const ARCH: &'static str = "powerpc";
|
|
|
|
}
|
|
|
|
|
2015-12-28 21:09:06 +00:00
|
|
|
#[cfg(target_arch = "powerpc64")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "powerpc64";
|
|
|
|
}
|
|
|
|
|
2016-09-09 23:00:23 +02:00
|
|
|
#[cfg(target_arch = "s390x")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "s390x";
|
|
|
|
}
|
|
|
|
|
2016-12-06 15:57:43 -06:00
|
|
|
#[cfg(target_arch = "sparc64")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "sparc64";
|
|
|
|
}
|
|
|
|
|
2015-10-24 20:51:34 -05:00
|
|
|
#[cfg(target_arch = "le32")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "le32";
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:05:10 +00:00
|
|
|
#[cfg(target_arch = "asmjs")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "asmjs";
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:41:50 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
mod arch {
|
|
|
|
pub const ARCH: &'static str = "wasm32";
|
|
|
|
}
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2015-03-17 13:33:26 -07:00
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
use iter::repeat;
|
|
|
|
use rand::{self, Rng};
|
|
|
|
use ffi::{OsString, OsStr};
|
2015-03-17 13:33:26 -07:00
|
|
|
use path::{Path, PathBuf};
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
fn make_rand_name() -> OsString {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let n = format!("TEST{}", rng.gen_ascii_chars().take(10)
|
|
|
|
.collect::<String>());
|
2015-03-30 11:00:05 -07:00
|
|
|
let n = OsString::from(n);
|
2015-02-11 11:47:53 -08:00
|
|
|
assert!(var_os(&n).is_none());
|
2015-01-27 12:20:58 -08:00
|
|
|
n
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq(a: Option<OsString>, b: Option<&str>) {
|
2015-04-17 23:45:55 -07:00
|
|
|
assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "VALUE");
|
2015-02-11 11:47:53 -08:00
|
|
|
eq(var_os(&n), Some("VALUE"));
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "VALUE");
|
|
|
|
remove_var(&n);
|
2015-02-11 11:47:53 -08:00
|
|
|
eq(var_os(&n), None);
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_var_overwrite() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "1");
|
|
|
|
set_var(&n, "2");
|
2015-02-11 11:47:53 -08:00
|
|
|
eq(var_os(&n), Some("2"));
|
2015-01-27 12:20:58 -08:00
|
|
|
set_var(&n, "");
|
2015-02-11 11:47:53 -08:00
|
|
|
eq(var_os(&n), Some(""));
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-01-27 12:20:58 -08:00
|
|
|
fn test_var_big() {
|
|
|
|
let mut s = "".to_string();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < 100 {
|
|
|
|
s.push_str("aaaaaaaaaa");
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
let n = make_rand_name();
|
2015-03-07 18:08:48 -08:00
|
|
|
set_var(&n, &s);
|
|
|
|
eq(var_os(&n), Some(&s));
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-01-27 12:20:58 -08:00
|
|
|
fn test_self_exe_path() {
|
|
|
|
let path = current_exe();
|
|
|
|
assert!(path.is_ok());
|
|
|
|
let path = path.unwrap();
|
|
|
|
|
|
|
|
// Hard to test this function
|
|
|
|
assert!(path.is_absolute());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-09-07 05:34:15 +00:00
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
2015-01-27 12:20:58 -08:00
|
|
|
fn test_env_set_get_huge() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
let s = repeat("x").take(10000).collect::<String>();
|
|
|
|
set_var(&n, &s);
|
2015-03-07 18:08:48 -08:00
|
|
|
eq(var_os(&n), Some(&s));
|
2015-01-27 12:20:58 -08:00
|
|
|
remove_var(&n);
|
2015-02-11 11:47:53 -08:00
|
|
|
eq(var_os(&n), None);
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_env_set_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
let mut e = vars_os();
|
2015-01-27 12:20:58 -08:00
|
|
|
set_var(&n, "VALUE");
|
|
|
|
assert!(!e.any(|(k, v)| {
|
|
|
|
&*k == &*n && &*v == "VALUE"
|
|
|
|
}));
|
|
|
|
|
2015-02-11 11:47:53 -08:00
|
|
|
assert!(vars_os().any(|(k, v)| {
|
2015-01-27 12:20:58 -08:00
|
|
|
&*k == &*n && &*v == "VALUE"
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
assert!((!Path::new("test-path").is_absolute()));
|
|
|
|
|
|
|
|
current_dir().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn split_paths_windows() {
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
|
|
|
split_paths(unparsed).collect::<Vec<_>>() ==
|
2015-03-23 17:29:51 -07:00
|
|
|
parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>()
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(check_parse("", &mut [""]));
|
|
|
|
assert!(check_parse(r#""""#, &mut [""]));
|
|
|
|
assert!(check_parse(";;", &mut ["", "", ""]));
|
|
|
|
assert!(check_parse(r"c:\", &mut [r"c:\"]));
|
|
|
|
assert!(check_parse(r"c:\;", &mut [r"c:\", ""]));
|
|
|
|
assert!(check_parse(r"c:\;c:\Program Files\",
|
|
|
|
&mut [r"c:\", r"c:\Program Files\"]));
|
|
|
|
assert!(check_parse(r#"c:\;c:\"foo"\"#, &mut [r"c:\", r"c:\foo\"]));
|
|
|
|
assert!(check_parse(r#"c:\;c:\"foo;bar"\;c:\baz"#,
|
|
|
|
&mut [r"c:\", r"c:\foo;bar\", r"c:\baz"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn split_paths_unix() {
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
|
|
|
split_paths(unparsed).collect::<Vec<_>>() ==
|
2015-03-23 15:54:39 -07:00
|
|
|
parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>()
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(check_parse("", &mut [""]));
|
|
|
|
assert!(check_parse("::", &mut ["", "", ""]));
|
|
|
|
assert!(check_parse("/", &mut ["/"]));
|
|
|
|
assert!(check_parse("/:", &mut ["/", ""]));
|
|
|
|
assert!(check_parse("/:/usr/local", &mut ["/", "/usr/local"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn join_paths_unix() {
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2015-02-13 07:33:44 +00:00
|
|
|
&*join_paths(input.iter().cloned()).unwrap() ==
|
2015-04-17 23:45:55 -07:00
|
|
|
OsStr::new(output)
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(test_eq(&[], ""));
|
|
|
|
assert!(test_eq(&["/bin", "/usr/bin", "/usr/local/bin"],
|
|
|
|
"/bin:/usr/bin:/usr/local/bin"));
|
|
|
|
assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""],
|
|
|
|
":/bin:::/usr/bin:"));
|
2015-02-13 07:33:44 +00:00
|
|
|
assert!(join_paths(["/te:st"].iter().cloned()).is_err());
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn join_paths_windows() {
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2015-02-13 07:33:44 +00:00
|
|
|
&*join_paths(input.iter().cloned()).unwrap() ==
|
2015-04-17 23:45:55 -07:00
|
|
|
OsStr::new(output)
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(test_eq(&[], ""));
|
|
|
|
assert!(test_eq(&[r"c:\windows", r"c:\"],
|
|
|
|
r"c:\windows;c:\"));
|
|
|
|
assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""],
|
|
|
|
r";c:\windows;;;c:\;"));
|
|
|
|
assert!(test_eq(&[r"c:\te;st", r"c:\"],
|
|
|
|
r#""c:\te;st";c:\"#));
|
2015-02-13 07:33:44 +00:00
|
|
|
assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err());
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
}
|