2015-01-27 20:20:58 +00:00
|
|
|
|
//! Inspection and manipulation of the process's environment.
|
|
|
|
|
//!
|
2017-01-13 04:10:38 +00:00
|
|
|
|
//! This module contains functions to inspect various aspects such as
|
2015-07-17 03:43:36 +00:00
|
|
|
|
//! environment variables, process arguments, the current directory, and various
|
2015-01-27 20:20:58 +00:00
|
|
|
|
//! other important directories.
|
2017-05-18 08:26:47 +00:00
|
|
|
|
//!
|
|
|
|
|
//! There are several functions and structs in this module that have a
|
|
|
|
|
//! counterpart ending in `os`. Those ending in `os` will return an [`OsString`]
|
2019-12-28 15:05:44 +00:00
|
|
|
|
//! and those without will return a [`String`].
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#![stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::error::Error;
|
|
|
|
|
use crate::ffi::{OsStr, OsString};
|
|
|
|
|
use crate::fmt;
|
|
|
|
|
use crate::io;
|
|
|
|
|
use crate::path::{Path, PathBuf};
|
|
|
|
|
use crate::sys;
|
|
|
|
|
use crate::sys::os as os_imp;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2017-02-07 18:43:22 +00:00
|
|
|
|
/// Returns the current working directory as a [`PathBuf`].
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
2017-02-07 18:43:22 +00:00
|
|
|
|
/// Returns an [`Err`] if the current working directory value is invalid.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// Possible cases:
|
|
|
|
|
///
|
|
|
|
|
/// * Current directory does not exist.
|
|
|
|
|
/// * There are insufficient permissions to access the current directory.
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
2018-05-14 02:32:27 +00:00
|
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
|
/// let path = env::current_dir()?;
|
|
|
|
|
/// println!("The current directory is {}", path.display());
|
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 18:59:17 +00:00
|
|
|
|
pub fn current_dir() -> io::Result<PathBuf> {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
os_imp::getcwd()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 21:36:24 +00:00
|
|
|
|
/// Changes the current working directory to the specified path.
|
|
|
|
|
///
|
|
|
|
|
/// Returns an [`Err`] if the operation fails.
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
2015-02-23 18:59:17 +00:00
|
|
|
|
/// use std::path::Path;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// let root = Path::new("/");
|
|
|
|
|
/// assert!(env::set_current_dir(&root).is_ok());
|
|
|
|
|
/// println!("Successfully changed working directory to {}!", root.display());
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2017-05-17 21:36:24 +00:00
|
|
|
|
pub fn set_current_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
|
|
|
|
os_imp::chdir(path.as_ref())
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An iterator over a snapshot of the environment variables of this process.
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// This structure is created by [`env::vars()`]. See its documentation for more.
|
2017-01-13 04:10:38 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::vars()`]: vars
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct Vars {
|
|
|
|
|
inner: VarsOs,
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// An iterator over a snapshot of the environment variables of this process.
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// This structure is created by [`env::vars_os()`]. See its documentation for more.
|
2017-01-13 04:10:38 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::vars()`]: vars
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct VarsOs {
|
|
|
|
|
inner: os_imp::Env,
|
|
|
|
|
}
|
2015-02-11 19:47:53 +00:00
|
|
|
|
|
|
|
|
|
/// Returns an iterator of (variable, value) pairs of strings, for all the
|
|
|
|
|
/// environment variables of the current process.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// The returned iterator contains a snapshot of the process's environment
|
2016-09-14 20:41:17 +00:00
|
|
|
|
/// variables at the time of this invocation. Modifications to environment
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// variables afterwards will not be reflected in the returned iterator.
|
|
|
|
|
///
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// While iterating, the returned iterator will panic if any key or value in the
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// environment is not valid unicode. If this is not desired, consider using
|
|
|
|
|
/// [`env::vars_os()`].
|
2015-02-11 19:47:53 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00: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 19:47:53 +00:00
|
|
|
|
/// println!("{}: {}", key, value);
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2020-08-26 14:30:54 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`env::vars_os()`]: vars_os
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub fn vars() -> Vars {
|
2015-02-11 19:47:53 +00:00
|
|
|
|
Vars { inner: vars_os() }
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 19:47:53 +00: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 20:41:17 +00:00
|
|
|
|
/// variables at the time of this invocation. Modifications to environment
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// variables afterwards will not be reflected in the returned iterator.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
2015-02-11 19:47:53 +00: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 20:20:58 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 19:47:53 +00:00
|
|
|
|
pub fn vars_os() -> VarsOs {
|
|
|
|
|
VarsOs { inner: os_imp::env() }
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 19:47:53 +00:00
|
|
|
|
impl Iterator for Vars {
|
|
|
|
|
type Item = (String, String);
|
|
|
|
|
fn next(&mut self) -> Option<(String, String)> {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
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()
|
2015-02-11 19:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Vars {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("Vars { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 19:47:53 +00:00
|
|
|
|
impl Iterator for VarsOs {
|
|
|
|
|
type Item = (OsString, OsString);
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn next(&mut self) -> Option<(OsString, OsString)> {
|
|
|
|
|
self.inner.next()
|
|
|
|
|
}
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.inner.size_hint()
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for VarsOs {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("VarsOs { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// Fetches the environment variable `key` from the current process.
|
|
|
|
|
///
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// # Errors
|
2017-02-18 13:44:56 +00:00
|
|
|
|
///
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// * Environment variable is not present
|
|
|
|
|
/// * Environment variable is not valid unicode
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2019-08-11 11:49:02 +00:00
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// 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-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// let key = "HOME";
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// match env::var(key) {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// Ok(val) => println!("{}: {:?}", key, val),
|
|
|
|
|
/// Err(e) => println!("couldn't interpret {}: {}", key, e),
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 23:06:21 +00:00
|
|
|
|
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
|
2015-09-09 19:37:59 +00:00
|
|
|
|
_var(key.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _var(key: &OsStr) -> Result<String, VarError> {
|
2015-02-11 19:47:53 +00:00
|
|
|
|
match var_os(key) {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
Some(s) => s.into_string().map_err(VarError::NotUnicode),
|
2017-05-17 21:36:24 +00:00
|
|
|
|
None => Err(VarError::NotPresent),
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// Fetches the environment variable `key` from the current process, returning
|
2017-02-18 13:44:56 +00:00
|
|
|
|
/// [`None`] if the variable isn't set.
|
|
|
|
|
///
|
2019-08-11 11:49:02 +00:00
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// 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-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-02-11 19:47:53 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-02-11 19:47:53 +00: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 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 23:06:21 +00:00
|
|
|
|
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
|
2015-09-09 19:37:59 +00:00
|
|
|
|
_var_os(key.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _var_os(key: &OsStr) -> Option<OsString> {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
os_imp::getenv(key)
|
|
|
|
|
.unwrap_or_else(|e| panic!("failed to get environment variable `{:?}`: {}", key, e))
|
2015-02-11 19:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// The error type for operations interacting with environment variables.
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// Possibly returned from [`env::var()`].
|
2017-01-13 04:10:38 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::var()`]: var
|
2015-01-27 20:20:58 +00:00
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub enum VarError {
|
|
|
|
|
/// The specified environment variable was not present in the current
|
|
|
|
|
/// process's environment.
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00: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 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2016-02-20 00:08:36 +00:00
|
|
|
|
NotUnicode(#[stable(feature = "env", since = "1.0.0")] OsString),
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl fmt::Display for VarError {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
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 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl Error for VarError {
|
2019-12-01 04:01:48 +00:00
|
|
|
|
#[allow(deprecated)]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
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 20:59:36 +00:00
|
|
|
|
/// Note that while concurrent access to environment variables is safe in Rust,
|
|
|
|
|
/// some platforms only expose inherently unsafe non-threadsafe APIs for
|
2019-09-05 16:15:28 +00:00
|
|
|
|
/// inspecting the environment. As a result, extra care needs to be taken when
|
2015-04-23 20:59:36 +00:00
|
|
|
|
/// 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-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// let key = "KEY";
|
|
|
|
|
/// env::set_var(key, "VALUE");
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 23:06:21 +00:00
|
|
|
|
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(k: K, v: V) {
|
2015-09-09 19:37:59 +00:00
|
|
|
|
_set_var(k.as_ref(), v.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _set_var(k: &OsStr, v: &OsStr) {
|
2015-10-25 17:19:35 +00:00
|
|
|
|
os_imp::setenv(k, v).unwrap_or_else(|e| {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
panic!("failed to set environment variable `{:?}` to `{:?}`: {}", k, v, e)
|
2015-10-25 17:19:35 +00:00
|
|
|
|
})
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
|
/// Removes an environment variable from the environment of the currently running process.
|
2015-03-31 04:19:31 +00:00
|
|
|
|
///
|
2015-04-23 20:59:36 +00: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 04:19:31 +00: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 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-05-05 23:06:21 +00:00
|
|
|
|
pub fn remove_var<K: AsRef<OsStr>>(k: K) {
|
2015-09-09 19:37:59 +00:00
|
|
|
|
_remove_var(k.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _remove_var(k: &OsStr) {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
os_imp::unsetenv(k)
|
|
|
|
|
.unwrap_or_else(|e| panic!("failed to remove environment variable `{:?}`: {}", k, e))
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// An iterator that splits an environment variable into paths according to
|
|
|
|
|
/// platform-specific conventions.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2019-04-16 21:37:07 +00:00
|
|
|
|
/// The iterator element type is [`PathBuf`].
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// This structure is created by [`env::split_paths()`]. See its
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// documentation for more.
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::split_paths()`]: split_paths
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct SplitPaths<'a> {
|
|
|
|
|
inner: os_imp::SplitPaths<'a>,
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Parses input according to platform conventions for the `PATH`
|
|
|
|
|
/// environment variable.
|
|
|
|
|
///
|
2019-04-16 21:37:07 +00:00
|
|
|
|
/// Returns an iterator over the paths contained in `unparsed`. The iterator
|
|
|
|
|
/// element type is [`PathBuf`].
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// let key = "PATH";
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// match env::var_os(key) {
|
2015-01-27 20:20:58 +00: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 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-03-01 08:34:11 +00:00
|
|
|
|
pub fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths<'_> {
|
2015-03-30 18:00:05 +00:00
|
|
|
|
SplitPaths { inner: os_imp::split_paths(unparsed.as_ref()) }
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl<'a> Iterator for SplitPaths<'a> {
|
2015-02-23 18:59:17 +00:00
|
|
|
|
type Item = PathBuf;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn next(&mut self) -> Option<PathBuf> {
|
|
|
|
|
self.inner.next()
|
|
|
|
|
}
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.inner.size_hint()
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
|
impl fmt::Debug for SplitPaths<'_> {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("SplitPaths { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// The error type for operations on the `PATH` variable. Possibly returned from
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::join_paths()`].
|
2017-05-18 08:26:47 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::join_paths()`]: join_paths
|
2015-01-27 20:20:58 +00:00
|
|
|
|
#[derive(Debug)]
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub struct JoinPathsError {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
inner: os_imp::JoinPathsError,
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 17:42:01 +00:00
|
|
|
|
/// Joins a collection of [`Path`]s appropriately for the `PATH`
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// environment variable.
|
|
|
|
|
///
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// # Errors
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2020-08-17 13:42:23 +00:00
|
|
|
|
/// Returns an [`Err`] (containing an error message) if one of the input
|
2017-02-08 17:42:01 +00:00
|
|
|
|
/// [`Path`]s contains an invalid character for constructing the `PATH`
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// variable (a double quote on Windows or a colon on Unix).
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2017-06-19 02:21:17 +00:00
|
|
|
|
/// Joining paths on a Unix-like platform:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::env;
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
/// use std::path::Path;
|
|
|
|
|
///
|
2018-05-14 02:32:27 +00:00
|
|
|
|
/// fn main() -> Result<(), env::JoinPathsError> {
|
|
|
|
|
/// # if cfg!(unix) {
|
|
|
|
|
/// let paths = [Path::new("/bin"), Path::new("/usr/bin")];
|
|
|
|
|
/// let path_os_string = env::join_paths(paths.iter())?;
|
|
|
|
|
/// assert_eq!(path_os_string, OsString::from("/bin:/usr/bin"));
|
2017-06-19 02:21:17 +00:00
|
|
|
|
/// # }
|
2018-05-14 02:32:27 +00:00
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
2017-06-19 02:21:17 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// Joining a path containing a colon on a Unix-like platform results in an
|
|
|
|
|
/// error:
|
2017-06-19 02:21:17 +00:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # if cfg!(unix) {
|
|
|
|
|
/// use std::env;
|
|
|
|
|
/// use std::path::Path;
|
|
|
|
|
///
|
|
|
|
|
/// let paths = [Path::new("/bin"), Path::new("/usr/bi:n")];
|
|
|
|
|
/// assert!(env::join_paths(paths.iter()).is_err());
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// Using `env::join_paths()` with [`env::split_paths()`] to append an item to
|
|
|
|
|
/// the `PATH` environment variable:
|
2017-06-19 02:21:17 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
2015-02-23 18:59:17 +00:00
|
|
|
|
/// use std::path::PathBuf;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2018-05-14 02:32:27 +00:00
|
|
|
|
/// fn main() -> Result<(), env::JoinPathsError> {
|
|
|
|
|
/// if let Some(path) = env::var_os("PATH") {
|
|
|
|
|
/// let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
|
|
|
|
/// paths.push(PathBuf::from("/home/xyz/bin"));
|
|
|
|
|
/// let new_path = env::join_paths(paths)?;
|
|
|
|
|
/// env::set_var("PATH", &new_path);
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// Ok(())
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2019-05-24 01:35:27 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::split_paths()`]: split_paths
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
|
2019-11-27 18:29:00 +00:00
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
|
T: AsRef<OsStr>,
|
2015-01-27 20:20:58 +00:00
|
|
|
|
{
|
2019-11-27 18:29:00 +00:00
|
|
|
|
os_imp::join_paths(paths.into_iter()).map_err(|e| JoinPathsError { inner: e })
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl fmt::Display for JoinPathsError {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
self.inner.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl Error for JoinPathsError {
|
2019-12-01 04:01:48 +00:00
|
|
|
|
#[allow(deprecated, deprecated_in_future)]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
self.inner.description()
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// Returns the path of the current user's home directory if known.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Unix
|
|
|
|
|
///
|
2018-07-05 15:32:09 +00:00
|
|
|
|
/// - Returns the value of the 'HOME' environment variable if it is set
|
|
|
|
|
/// (including to an empty string).
|
|
|
|
|
/// - Otherwise, it tries to determine the home directory by invoking the `getpwuid_r` function
|
|
|
|
|
/// using the UID of the current user. An empty home directory field returned from the
|
|
|
|
|
/// `getpwuid_r` function is considered to be a valid value.
|
|
|
|
|
/// - Returns `None` if the current user has no entry in the /etc/passwd file.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Windows
|
|
|
|
|
///
|
2018-07-05 15:32:09 +00:00
|
|
|
|
/// - Returns the value of the 'HOME' environment variable if it is set
|
|
|
|
|
/// (including to an empty string).
|
|
|
|
|
/// - Otherwise, returns the value of the 'USERPROFILE' environment variable if it is set
|
|
|
|
|
/// (including to an empty string).
|
|
|
|
|
/// - If both do not exist, [`GetUserProfileDirectory`][msdn] is used to return the path.
|
2015-10-10 18:50:26 +00:00
|
|
|
|
///
|
2019-12-25 15:35:54 +00:00
|
|
|
|
/// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/userenv/nf-userenv-getuserprofiledirectorya
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// match env::home_dir() {
|
2018-07-05 15:32:09 +00:00
|
|
|
|
/// Some(path) => println!("Your home directory, probably: {}", path.display()),
|
2016-02-25 21:19:47 +00:00
|
|
|
|
/// None => println!("Impossible to get your home dir!"),
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2019-11-27 18:29:00 +00:00
|
|
|
|
#[rustc_deprecated(
|
|
|
|
|
since = "1.29.0",
|
2018-07-05 15:32:09 +00:00
|
|
|
|
reason = "This function's behavior is unexpected and probably not what you want. \
|
2020-05-02 02:51:20 +00:00
|
|
|
|
Consider using a crate from crates.io instead."
|
2019-11-27 18:29:00 +00:00
|
|
|
|
)]
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 18:59:17 +00:00
|
|
|
|
pub fn home_dir() -> Option<PathBuf> {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
os_imp::home_dir()
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// Returns the path of a temporary directory.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// # Unix
|
|
|
|
|
///
|
|
|
|
|
/// Returns the value of the `TMPDIR` environment variable if it is
|
2016-05-12 04:05:25 +00:00
|
|
|
|
/// 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`.
|
|
|
|
|
///
|
2017-05-18 08:26:47 +00:00
|
|
|
|
/// # Windows
|
|
|
|
|
///
|
|
|
|
|
/// Returns the value of, in order, the `TMP`, `TEMP`,
|
2016-05-12 04:05:25 +00:00
|
|
|
|
/// `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-10 18:50:26 +00:00
|
|
|
|
///
|
2019-12-25 15:35:54 +00:00
|
|
|
|
/// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha
|
2015-03-31 04:19:31 +00:00
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// ```no_run
|
2015-03-31 04:19:31 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
/// use std::fs::File;
|
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
|
/// let mut dir = env::temp_dir();
|
|
|
|
|
/// dir.push("foo.txt");
|
2015-03-31 04:19:31 +00:00
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
|
/// let f = File::create(dir)?;
|
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
2015-03-31 04:19:31 +00:00
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 18:59:17 +00:00
|
|
|
|
pub fn temp_dir() -> PathBuf {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
os_imp::temp_dir()
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// Returns the full filesystem path of the current running executable.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// # Platform-specific behavior
|
|
|
|
|
///
|
|
|
|
|
/// If the executable was invoked through a symbolic link, some platforms will
|
|
|
|
|
/// return the path of the symbolic link and other platforms will return the
|
|
|
|
|
/// path of the symbolic link’s target.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// Acquiring the path of the current executable is a platform-specific operation
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// that can fail for a good number of reasons. Some errors can include, but not
|
2016-02-25 20:52:02 +00:00
|
|
|
|
/// be limited to, filesystem operations failing or general syscall failures.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2016-05-09 23:45:12 +00:00
|
|
|
|
/// # Security
|
|
|
|
|
///
|
2016-07-19 16:32:56 +00: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")
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// And you make a hard link of the program:
|
2016-07-19 16:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
/// ```bash
|
|
|
|
|
/// $ ln foo bar
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// When you run it, you won’t get the path of the original executable, you’ll
|
|
|
|
|
/// get the path of the hard link:
|
2016-07-19 16:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
/// ```bash
|
|
|
|
|
/// $ ./bar
|
|
|
|
|
/// Ok("/home/alex/bar")
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2016-07-20 20:43:53 +00:00
|
|
|
|
/// This sort of behavior has been known to [lead to privilege escalation] when
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// used incorrectly.
|
2016-07-19 16:32:56 +00:00
|
|
|
|
///
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// [lead to privilege escalation]: https://securityvulns.com/Wdocument183.html
|
2016-05-09 23:45:12 +00:00
|
|
|
|
///
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// match env::current_exe() {
|
|
|
|
|
/// Ok(exe_path) => println!("Path of this executable is: {}",
|
2017-12-24 19:29:13 +00:00
|
|
|
|
/// exe_path.display()),
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// Err(e) => println!("failed to get current exe path: {}", e),
|
|
|
|
|
/// };
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-23 18:59:17 +00:00
|
|
|
|
pub fn current_exe() -> io::Result<PathBuf> {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
os_imp::current_exe()
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-10 06:19:28 +00:00
|
|
|
|
/// An iterator over the arguments of a process, yielding a [`String`] value for
|
|
|
|
|
/// each argument.
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// This struct is created by [`env::args()`]. See its documentation
|
|
|
|
|
/// for more.
|
2016-11-21 22:12:14 +00:00
|
|
|
|
///
|
2017-03-05 21:39:24 +00:00
|
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2017-06-10 06:19:28 +00:00
|
|
|
|
/// set to arbitrary text, and may not even exist. This means this property
|
|
|
|
|
/// should not be relied upon for security purposes.
|
2017-03-05 21:39:24 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::args()`]: args
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct Args {
|
|
|
|
|
inner: ArgsOs,
|
|
|
|
|
}
|
2015-02-11 19:47:53 +00:00
|
|
|
|
|
2016-11-21 22:12:14 +00:00
|
|
|
|
/// An iterator over the arguments of a process, yielding an [`OsString`] value
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// for each argument.
|
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// This struct is created by [`env::args_os()`]. See its documentation
|
|
|
|
|
/// for more.
|
2016-11-21 22:12:14 +00:00
|
|
|
|
///
|
2017-03-05 21:39:24 +00:00
|
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2017-06-10 06:19:28 +00:00
|
|
|
|
/// set to arbitrary text, and may not even exist. This means this property
|
|
|
|
|
/// should not be relied upon for security purposes.
|
2017-03-05 21:39:24 +00:00
|
|
|
|
///
|
2020-08-26 14:30:54 +00:00
|
|
|
|
/// [`env::args_os()`]: args_os
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub struct ArgsOs {
|
|
|
|
|
inner: sys::args::Args,
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
|
/// via the command line).
|
|
|
|
|
///
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2016-02-18 20:59:03 +00:00
|
|
|
|
/// set to arbitrary text, and may not even exist. This means this property should
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
|
///
|
2017-11-07 12:17:04 +00:00
|
|
|
|
/// On Unix systems shell usually expands unquoted arguments with glob patterns
|
|
|
|
|
/// (such as `*` and `?`). On Windows this is not done, and such arguments are
|
|
|
|
|
/// passed as-is.
|
|
|
|
|
///
|
2020-03-19 10:35:28 +00:00
|
|
|
|
/// On glibc Linux systems, arguments are retrieved by placing a function in ".init_array".
|
|
|
|
|
/// Glibc passes argc, argv, and envp to functions in ".init_array", as a non-standard extension.
|
2019-11-21 18:34:31 +00:00
|
|
|
|
/// This allows `std::env::args` to work even in a `cdylib` or `staticlib`, as it does on macOS
|
|
|
|
|
/// and Windows.
|
|
|
|
|
///
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// The returned iterator will panic during iteration if any argument to the
|
2016-02-18 21:13:22 +00:00
|
|
|
|
/// process is not valid unicode. If this is not desired,
|
2016-11-21 22:12:14 +00:00
|
|
|
|
/// use the [`args_os`] function instead.
|
2015-02-11 19:47:53 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-01-27 20:20:58 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// // Prints each argument on a separate line
|
|
|
|
|
/// for argument in env::args() {
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// println!("{}", argument);
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub fn args() -> Args {
|
2015-02-11 19:47:53 +00:00
|
|
|
|
Args { inner: args_os() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
|
/// via the command line).
|
|
|
|
|
///
|
2016-03-30 07:01:21 +00:00
|
|
|
|
/// The first element is traditionally the path of the executable, but it can be
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// set to arbitrary text, and it may not even exist, so this property should
|
|
|
|
|
/// not be relied upon for security purposes.
|
|
|
|
|
///
|
2020-03-19 10:35:28 +00:00
|
|
|
|
/// On glibc Linux systems, arguments are retrieved by placing a function in ".init_array".
|
|
|
|
|
/// Glibc passes argc, argv, and envp to functions in ".init_array", as a non-standard extension.
|
2019-11-21 18:34:31 +00:00
|
|
|
|
/// This allows `std::env::args` to work even in a `cdylib` or `staticlib`, as it does on macOS
|
|
|
|
|
/// and Windows.
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-02-11 19:47:53 +00:00
|
|
|
|
///
|
2015-03-13 02:42:38 +00:00
|
|
|
|
/// ```
|
2015-02-11 19:47:53 +00:00
|
|
|
|
/// use std::env;
|
|
|
|
|
///
|
|
|
|
|
/// // Prints each argument on a separate line
|
|
|
|
|
/// for argument in env::args_os() {
|
|
|
|
|
/// println!("{:?}", argument);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 19:47:53 +00:00
|
|
|
|
pub fn args_os() -> ArgsOs {
|
2016-09-29 22:00:44 +00:00
|
|
|
|
ArgsOs { inner: sys::args::args() }
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 23:35:09 +00:00
|
|
|
|
#[stable(feature = "env_unimpl_send_sync", since = "1.26.0")]
|
2018-02-04 19:40:39 +00:00
|
|
|
|
impl !Send for Args {}
|
|
|
|
|
|
2018-04-04 23:35:09 +00:00
|
|
|
|
#[stable(feature = "env_unimpl_send_sync", since = "1.26.0")]
|
2018-02-04 19:40:39 +00:00
|
|
|
|
impl !Sync for Args {}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
impl Iterator for Args {
|
2015-02-11 19:47:53 +00:00
|
|
|
|
type Item = String;
|
|
|
|
|
fn next(&mut self) -> Option<String> {
|
|
|
|
|
self.inner.next().map(|s| s.into_string().unwrap())
|
|
|
|
|
}
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.inner.size_hint()
|
|
|
|
|
}
|
2015-02-11 19:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-16 10:15:30 +00:00
|
|
|
|
impl ExactSizeIterator for Args {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
|
self.inner.len()
|
|
|
|
|
}
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
|
self.inner.is_empty()
|
|
|
|
|
}
|
2015-02-16 10:15:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 07:38:39 +00:00
|
|
|
|
#[stable(feature = "env_iterators", since = "1.12.0")]
|
2016-04-30 14:37:44 +00:00
|
|
|
|
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 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Args {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
f.debug_struct("Args").field("inner", &self.inner.inner.inner_debug()).finish()
|
2016-11-25 18:21:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 23:35:09 +00:00
|
|
|
|
#[stable(feature = "env_unimpl_send_sync", since = "1.26.0")]
|
2018-02-04 19:40:39 +00:00
|
|
|
|
impl !Send for ArgsOs {}
|
|
|
|
|
|
2018-04-04 23:35:09 +00:00
|
|
|
|
#[stable(feature = "env_unimpl_send_sync", since = "1.26.0")]
|
2018-02-04 19:40:39 +00:00
|
|
|
|
impl !Sync for ArgsOs {}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-11 19:47:53 +00:00
|
|
|
|
impl Iterator for ArgsOs {
|
2015-01-27 20:20:58 +00:00
|
|
|
|
type Item = OsString;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn next(&mut self) -> Option<OsString> {
|
|
|
|
|
self.inner.next()
|
|
|
|
|
}
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.inner.size_hint()
|
|
|
|
|
}
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-02-16 10:15:30 +00:00
|
|
|
|
impl ExactSizeIterator for ArgsOs {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
|
self.inner.len()
|
|
|
|
|
}
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
|
self.inner.is_empty()
|
|
|
|
|
}
|
2015-02-16 10:15:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 07:38:39 +00:00
|
|
|
|
#[stable(feature = "env_iterators", since = "1.12.0")]
|
2016-04-30 14:37:44 +00:00
|
|
|
|
impl DoubleEndedIterator for ArgsOs {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn next_back(&mut self) -> Option<OsString> {
|
|
|
|
|
self.inner.next_back()
|
|
|
|
|
}
|
2016-04-30 14:37:44 +00:00
|
|
|
|
}
|
2016-11-25 18:21:49 +00:00
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for ArgsOs {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
f.debug_struct("ArgsOs").field("inner", &self.inner.inner_debug()).finish()
|
2016-11-25 18:21:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
/// Constants associated with the current target
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
pub mod consts {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::sys::env::os;
|
2016-09-21 19:50:30 +00:00
|
|
|
|
|
2016-02-18 20:46:03 +00:00
|
|
|
|
/// A string describing the architecture of the CPU that is currently
|
2015-02-27 18:59:59 +00:00
|
|
|
|
/// in use.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - x86
|
|
|
|
|
/// - x86_64
|
|
|
|
|
/// - arm
|
|
|
|
|
/// - aarch64
|
|
|
|
|
/// - mips
|
2016-08-27 06:39:29 +00:00
|
|
|
|
/// - mips64
|
2015-05-21 03:19:55 +00:00
|
|
|
|
/// - powerpc
|
2015-12-28 21:09:06 +00:00
|
|
|
|
/// - powerpc64
|
2019-11-23 07:22:05 +00:00
|
|
|
|
/// - riscv64
|
2016-09-09 21:00:23 +00:00
|
|
|
|
/// - s390x
|
2016-12-06 21:57:43 +00:00
|
|
|
|
/// - sparc64
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2020-05-21 23:43:59 +00:00
|
|
|
|
pub const ARCH: &str = env!("STD_ENV_ARCH");
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// The family of the operating system. Example value is `unix`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - unix
|
|
|
|
|
/// - windows
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const FAMILY: &str = os::FAMILY;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// A string describing the specific operating system in use.
|
|
|
|
|
/// Example value is `linux`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - linux
|
|
|
|
|
/// - macos
|
|
|
|
|
/// - ios
|
|
|
|
|
/// - freebsd
|
|
|
|
|
/// - dragonfly
|
2015-07-01 03:37:11 +00:00
|
|
|
|
/// - netbsd
|
2015-05-21 03:19:55 +00:00
|
|
|
|
/// - openbsd
|
2016-01-28 11:02:31 +00:00
|
|
|
|
/// - solaris
|
2015-05-21 03:19:55 +00:00
|
|
|
|
/// - android
|
|
|
|
|
/// - windows
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const OS: &str = os::OS;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// platform. Example value is `lib`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - lib
|
|
|
|
|
/// - `""` (an empty string)
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const DLL_PREFIX: &str = os::DLL_PREFIX;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// platform. Example value is `.so`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - .so
|
|
|
|
|
/// - .dylib
|
|
|
|
|
/// - .dll
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const DLL_SUFFIX: &str = os::DLL_SUFFIX;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Specifies the file extension used for shared libraries on this
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// platform that goes after the dot. Example value is `so`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
2015-12-01 21:53:48 +00:00
|
|
|
|
/// - so
|
|
|
|
|
/// - dylib
|
|
|
|
|
/// - dll
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const DLL_EXTENSION: &str = os::DLL_EXTENSION;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// platform. Example value is `.exe`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
2015-12-01 21:53:48 +00:00
|
|
|
|
/// - .exe
|
|
|
|
|
/// - .nexe
|
|
|
|
|
/// - .pexe
|
2015-05-21 03:19:55 +00:00
|
|
|
|
/// - `""` (an empty string)
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const EXE_SUFFIX: &str = os::EXE_SUFFIX;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
2016-04-05 15:55:14 +00:00
|
|
|
|
/// on this platform. Example value is `exe`.
|
2015-05-21 03:19:55 +00:00
|
|
|
|
///
|
|
|
|
|
/// Some possible values:
|
|
|
|
|
///
|
|
|
|
|
/// - exe
|
|
|
|
|
/// - `""` (an empty string)
|
2015-02-27 18:59:59 +00:00
|
|
|
|
#[stable(feature = "env", since = "1.0.0")]
|
2018-12-04 09:21:42 +00:00
|
|
|
|
pub const EXE_EXTENSION: &str = os::EXE_EXTENSION;
|
2016-09-25 04:38:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2015-03-17 20:33:26 +00:00
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::path::Path;
|
2015-01-27 20:20:58 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2019-03-12 17:58:30 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "emscripten", target_env = "sgx"), ignore)]
|
2015-01-27 20:20:58 +00: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]
|
|
|
|
|
fn test() {
|
|
|
|
|
assert!((!Path::new("test-path").is_absolute()));
|
|
|
|
|
|
2019-03-12 17:58:30 +00:00
|
|
|
|
#[cfg(not(target_env = "sgx"))]
|
2015-01-27 20:20:58 +00:00
|
|
|
|
current_dir().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn split_paths_windows() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::path::PathBuf;
|
2018-01-11 10:20:50 +00:00
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
split_paths(unparsed).collect::<Vec<_>>()
|
|
|
|
|
== parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>()
|
2015-01-27 20:20:58 +00: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:\", ""]));
|
2019-11-27 18:29:00 +00:00
|
|
|
|
assert!(check_parse(r"c:\;c:\Program Files\", &mut [r"c:\", r"c:\Program Files\"]));
|
2015-01-27 20:20:58 +00:00
|
|
|
|
assert!(check_parse(r#"c:\;c:\"foo"\"#, &mut [r"c:\", r"c:\foo\"]));
|
2019-11-27 18:29:00 +00:00
|
|
|
|
assert!(check_parse(
|
|
|
|
|
r#"c:\;c:\"foo;bar"\;c:\baz"#,
|
|
|
|
|
&mut [r"c:\", r"c:\foo;bar\", r"c:\baz"]
|
|
|
|
|
));
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn split_paths_unix() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::path::PathBuf;
|
2018-01-11 10:20:50 +00:00
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
split_paths(unparsed).collect::<Vec<_>>()
|
|
|
|
|
== parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>()
|
2015-01-27 20:20:58 +00: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() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::ffi::OsStr;
|
2018-01-11 10:20:50 +00:00
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
&*join_paths(input.iter().cloned()).unwrap() == OsStr::new(output)
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(test_eq(&[], ""));
|
2019-11-27 18:29:00 +00:00
|
|
|
|
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 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn join_paths_windows() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::ffi::OsStr;
|
2018-01-11 10:20:50 +00:00
|
|
|
|
|
2015-01-27 20:20:58 +00:00
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
&*join_paths(input.iter().cloned()).unwrap() == OsStr::new(output)
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(test_eq(&[], ""));
|
2019-11-27 18:29:00 +00:00
|
|
|
|
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 20:20:58 +00:00
|
|
|
|
}
|
2017-06-21 12:40:45 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn args_debug() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
format!("Args {{ inner: {:?} }}", args().collect::<Vec<_>>()),
|
2019-11-27 18:29:00 +00:00
|
|
|
|
format!("{:?}", args())
|
|
|
|
|
);
|
2017-06-21 12:40:45 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
format!("ArgsOs {{ inner: {:?} }}", args_os().collect::<Vec<_>>()),
|
2019-11-27 18:29:00 +00:00
|
|
|
|
format!("{:?}", args_os())
|
|
|
|
|
);
|
2015-01-27 20:20:58 +00:00
|
|
|
|
}
|
2017-06-21 12:40:45 +00:00
|
|
|
|
}
|