1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
//! [![github]](https://github.com/dtolnay/argv) [![crates-io]](https://crates.io/crates/argv) [![docs-rs]](https://docs.rs/argv)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! <br>
//!
//! Command line arguments by reference: `Iterator<Item = &'static OsStr>`
//!
//! The standard library's [`std::env::args_os`] iterator produces an owned
//! string (`OsString`) for each argument. In some use cases it can be more
//! convenient for the arguments to be produced by static reference (`&'static
//! OsStr`).
//!
//! [`std::env::args_os`]: https://doc.rust-lang.org/std/env/fn.args_os.html
//!
//! # Examples
//!
//! ```
//! fn main() {
//! for arg in argv::iter() {
//! // arg is a &'static OsStr.
//! println!("{}", arg.to_string_lossy());
//! }
//! }
//! ```
//!
//! # Portability
//!
//! This crate is intended to be used on Linux and macOS, on which command line
//! arguments naturally live for the duration of the program. This crate
//! implements the same API on other platforms as well, such as Windows, but
//! leaks memory on platforms other than Linux and macOS.
#![doc(html_root_url = "https://docs.rs/argv/0.1.11")]
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(
clippy::cast_sign_loss,
clippy::extra_unused_type_parameters,
clippy::let_underscore_untyped,
clippy::must_use_candidate,
clippy::needless_doctest_main,
clippy::similar_names
)]
use std::ffi::OsStr;
/// Returns an iterator over command line arguments.
pub fn iter() -> Iter {
Iter {
platform_specific: crate::r#impl::iter(),
}
}
/// Iterator over command line arguments.
pub struct Iter {
platform_specific: crate::r#impl::Iter,
}
impl Iterator for Iter {
type Item = &'static OsStr;
fn next(&mut self) -> Option<Self::Item> {
self.platform_specific.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.platform_specific.size_hint()
}
}
impl ExactSizeIterator for Iter {
fn len(&self) -> usize {
self.platform_specific.len()
}
}
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
mod r#impl {
use std::ffi::{CStr, OsStr};
use std::mem;
use std::os::raw::{c_char, c_int};
use std::os::unix::ffi::OsStrExt;
use std::ptr;
static mut ARGC: c_int = 0;
static mut ARGV: *const *const c_char = ptr::null();
#[cfg(target_os = "linux")]
#[link_section = ".init_array"]
#[used]
static CAPTURE: unsafe extern "C" fn(c_int, *const *const c_char) = capture;
// Disabled for now until we investigate https://github.com/dtolnay/argv/issues/1
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[allow(dead_code)]
unsafe extern "C" fn capture(argc: c_int, argv: *const *const c_char) {
unsafe {
ARGC = argc;
ARGV = argv;
}
}
pub(crate) fn iter() -> Iter {
// These are only mutated before main so they are safe to read once main
// has begun.
let argc = unsafe { ARGC };
let argv = unsafe { ARGV };
// We count on the OS to provide argv for which argv + argc does not
// overflow.
let end = unsafe { argv.offset(argc as isize) };
Iter { next: argv, end }
}
pub(crate) struct Iter {
next: *const *const c_char,
end: *const *const c_char,
}
impl Iterator for Iter {
type Item = &'static OsStr;
fn next(&mut self) -> Option<Self::Item> {
if self.next == self.end {
None
} else {
let ptr = unsafe { *self.next };
let c_str = unsafe { CStr::from_ptr(ptr) };
self.next = unsafe { self.next.offset(1) };
Some(OsStr::from_bytes(c_str.to_bytes()))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl ExactSizeIterator for Iter {
fn len(&self) -> usize {
(self.end as usize - self.next as usize) / mem::size_of::<*const c_char>()
}
}
// Thread safe despite the raw pointers.
unsafe impl Send for Iter {}
unsafe impl Sync for Iter {}
}
#[cfg(any(not(target_os = "linux"), target_env = "musl"))]
mod r#impl {
use std::ffi::OsStr;
use std::sync::Once;
use std::{env, iter, ptr, slice};
static ONCE: Once = Once::new();
static mut ARGV: Vec<&'static OsStr> = Vec::new();
pub(crate) fn iter() -> Iter {
ONCE.call_once(|| {
let argv = env::args_os()
.map(|arg| -> &OsStr { Box::leak(arg.into_boxed_os_str()) })
.collect();
unsafe { ARGV = argv }
});
let argv = unsafe { &*ptr::addr_of!(ARGV) };
argv.iter().copied()
}
pub(crate) type Iter = iter::Copied<slice::Iter<'static, &'static OsStr>>;
}
const _AUTO_TRAITS: () = {
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
let _ = assert_send::<Iter>;
let _ = assert_sync::<Iter>;
};