mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-05 15:37:47 +00:00
Format everything using rustfmt
This commit is contained in:
parent
1c0b1ab375
commit
f74a1b6333
12 changed files with 418 additions and 176 deletions
|
@ -6,42 +6,70 @@ pub struct HelpText<'a> {
|
||||||
pub syntax: &'a str,
|
pub syntax: &'a str,
|
||||||
pub summary: &'a str,
|
pub summary: &'a str,
|
||||||
pub long_help: &'a str,
|
pub long_help: &'a str,
|
||||||
pub display_usage : bool
|
pub display_usage: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CoreOptions<'a> {
|
pub struct CoreOptions<'a> {
|
||||||
options: getopts::Options,
|
options: getopts::Options,
|
||||||
help_text : HelpText<'a>
|
help_text: HelpText<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CoreOptions<'a> {
|
impl<'a> CoreOptions<'a> {
|
||||||
pub fn new(help_text: HelpText<'a>) -> Self {
|
pub fn new(help_text: HelpText<'a>) -> Self {
|
||||||
let mut ret = CoreOptions {
|
let mut ret = CoreOptions {
|
||||||
options: getopts::Options::new(),
|
options: getopts::Options::new(),
|
||||||
help_text : help_text
|
help_text: help_text,
|
||||||
};
|
};
|
||||||
ret.options
|
ret.options
|
||||||
.optflag("", "help", "print usage information")
|
.optflag("", "help", "print usage information")
|
||||||
.optflag("", "version", "print name and version number");
|
.optflag("", "version", "print name and version number");
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
pub fn optflagopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions<'a> {
|
pub fn optflagopt(
|
||||||
|
&mut self,
|
||||||
|
short_name: &str,
|
||||||
|
long_name: &str,
|
||||||
|
desc: &str,
|
||||||
|
hint: &str,
|
||||||
|
) -> &mut CoreOptions<'a> {
|
||||||
self.options.optflagopt(short_name, long_name, desc, hint);
|
self.options.optflagopt(short_name, long_name, desc, hint);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions<'a> {
|
pub fn optflag(
|
||||||
|
&mut self,
|
||||||
|
short_name: &str,
|
||||||
|
long_name: &str,
|
||||||
|
desc: &str,
|
||||||
|
) -> &mut CoreOptions<'a> {
|
||||||
self.options.optflag(short_name, long_name, desc);
|
self.options.optflag(short_name, long_name, desc);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn optflagmulti(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions<'a> {
|
pub fn optflagmulti(
|
||||||
|
&mut self,
|
||||||
|
short_name: &str,
|
||||||
|
long_name: &str,
|
||||||
|
desc: &str,
|
||||||
|
) -> &mut CoreOptions<'a> {
|
||||||
self.options.optflagmulti(short_name, long_name, desc);
|
self.options.optflagmulti(short_name, long_name, desc);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn optopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions<'a> {
|
pub fn optopt(
|
||||||
|
&mut self,
|
||||||
|
short_name: &str,
|
||||||
|
long_name: &str,
|
||||||
|
desc: &str,
|
||||||
|
hint: &str,
|
||||||
|
) -> &mut CoreOptions<'a> {
|
||||||
self.options.optopt(short_name, long_name, desc, hint);
|
self.options.optopt(short_name, long_name, desc, hint);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn optmulti(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions<'a> {
|
pub fn optmulti(
|
||||||
|
&mut self,
|
||||||
|
short_name: &str,
|
||||||
|
long_name: &str,
|
||||||
|
desc: &str,
|
||||||
|
hint: &str,
|
||||||
|
) -> &mut CoreOptions<'a> {
|
||||||
self.options.optmulti(short_name, long_name, desc, hint);
|
self.options.optmulti(short_name, long_name, desc, hint);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -50,7 +78,7 @@ impl<'a> CoreOptions<'a> {
|
||||||
}
|
}
|
||||||
pub fn parse(&mut self, args: Vec<String>) -> getopts::Matches {
|
pub fn parse(&mut self, args: Vec<String>) -> getopts::Matches {
|
||||||
let matches = match self.options.parse(&args[1..]) {
|
let matches = match self.options.parse(&args[1..]) {
|
||||||
Ok(m) => { Some(m) },
|
Ok(m) => Some(m),
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
eprint!("{}: error: ", self.help_text.name);
|
eprint!("{}: error: ", self.help_text.name);
|
||||||
eprintln!("{}", f);
|
eprintln!("{}", f);
|
||||||
|
@ -59,16 +87,26 @@ impl<'a> CoreOptions<'a> {
|
||||||
}.unwrap();
|
}.unwrap();
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
let usage_str = if self.help_text.display_usage {
|
let usage_str = if self.help_text.display_usage {
|
||||||
format!("\n {}\n\n Reference\n",
|
format!(
|
||||||
|
"\n {}\n\n Reference\n",
|
||||||
self.options.usage(self.help_text.summary)
|
self.options.usage(self.help_text.summary)
|
||||||
).replace("Options:", " Options:")
|
).replace("Options:", " Options:")
|
||||||
} else { String::new() };
|
} else {
|
||||||
print!("
|
String::new()
|
||||||
|
};
|
||||||
|
print!(
|
||||||
|
"
|
||||||
{0} {1}
|
{0} {1}
|
||||||
|
|
||||||
{0} {2}
|
{0} {2}
|
||||||
{3}{4}
|
{3}{4}
|
||||||
", self.help_text.name, self.help_text.version, self.help_text.syntax, usage_str, self.help_text.long_help);
|
",
|
||||||
|
self.help_text.name,
|
||||||
|
self.help_text.version,
|
||||||
|
self.help_text.syntax,
|
||||||
|
usage_str,
|
||||||
|
self.help_text.long_help
|
||||||
|
);
|
||||||
exit!(0);
|
exit!(0);
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("{} {}", self.help_text.name, self.help_text.version);
|
println!("{} {}", self.help_text.name, self.help_text.version);
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
extern crate data_encoding;
|
extern crate data_encoding;
|
||||||
use self::data_encoding::{base64, base32, decode};
|
use self::data_encoding::{decode, base32, base64};
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub type DecodeResult = Result<Vec<u8>, decode::Error>;
|
pub type DecodeResult = Result<Vec<u8>, decode::Error>;
|
||||||
|
|
|
@ -27,24 +27,29 @@
|
||||||
|
|
||||||
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
|
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
|
||||||
use libc::time_t;
|
use libc::time_t;
|
||||||
use libc::{uid_t, gid_t, c_char, c_int};
|
use libc::{c_char, c_int, gid_t, uid_t};
|
||||||
use libc::{passwd, group, getpwnam, getpwuid, getgrnam, getgrgid, getgroups};
|
use libc::{getgrgid, getgrnam, getgroups, getpwnam, getpwuid, group, passwd};
|
||||||
|
|
||||||
use ::std::ptr;
|
use std::ptr;
|
||||||
use ::std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use ::std::io::Error as IOError;
|
use std::io::Error as IOError;
|
||||||
use ::std::io::Result as IOResult;
|
use std::io::Result as IOResult;
|
||||||
use ::std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use ::std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn getgrouplist(name: *const c_char, gid: gid_t, groups: *mut gid_t, ngroups: *mut c_int) -> c_int;
|
fn getgrouplist(
|
||||||
|
name: *const c_char,
|
||||||
|
gid: gid_t,
|
||||||
|
groups: *mut gid_t,
|
||||||
|
ngroups: *mut c_int,
|
||||||
|
) -> c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
||||||
let ngroups = unsafe { getgroups(0, ptr::null_mut()) };
|
let ngroups = unsafe { getgroups(0, ptr::null_mut()) };
|
||||||
if ngroups == -1 {
|
if ngroups == -1 {
|
||||||
return Err(IOError::last_os_error())
|
return Err(IOError::last_os_error());
|
||||||
}
|
}
|
||||||
let mut groups = Vec::with_capacity(ngroups as usize);
|
let mut groups = Vec::with_capacity(ngroups as usize);
|
||||||
let ngroups = unsafe { getgroups(ngroups, groups.as_mut_ptr()) };
|
let ngroups = unsafe { getgroups(ngroups, groups.as_mut_ptr()) };
|
||||||
|
@ -173,7 +178,9 @@ impl Group {
|
||||||
|
|
||||||
/// Fetch desired entry.
|
/// Fetch desired entry.
|
||||||
pub trait Locate<K> {
|
pub trait Locate<K> {
|
||||||
fn locate(key: K) -> IOResult<Self> where Self: ::std::marker::Sized;
|
fn locate(key: K) -> IOResult<Self>
|
||||||
|
where
|
||||||
|
Self: ::std::marker::Sized;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! f {
|
macro_rules! f {
|
||||||
|
|
|
@ -31,9 +31,9 @@ pub fn resolve_relative_path<'a>(path: &'a Path) -> Cow<'a, Path> {
|
||||||
result.pop();
|
result.pop();
|
||||||
}
|
}
|
||||||
Component::CurDir => (),
|
Component::CurDir => (),
|
||||||
Component::RootDir |
|
Component::RootDir | Component::Normal(_) | Component::Prefix(_) => {
|
||||||
Component::Normal(_) |
|
result.push(comp.as_os_str())
|
||||||
Component::Prefix(_) => result.push(comp.as_os_str()),
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.into()
|
result.into()
|
||||||
|
@ -53,7 +53,10 @@ fn resolve<P: AsRef<Path>>(original: P) -> IOResult<PathBuf> {
|
||||||
let mut result = original.as_ref().to_path_buf();
|
let mut result = original.as_ref().to_path_buf();
|
||||||
loop {
|
loop {
|
||||||
if followed == MAX_LINKS_FOLLOWED {
|
if followed == MAX_LINKS_FOLLOWED {
|
||||||
return Err(Error::new(ErrorKind::InvalidInput, "maximum links followed"));
|
return Err(Error::new(
|
||||||
|
ErrorKind::InvalidInput,
|
||||||
|
"maximum links followed",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match fs::symlink_metadata(&result) {
|
match fs::symlink_metadata(&result) {
|
||||||
|
@ -93,8 +96,7 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
|
||||||
// vector for canonicalization.
|
// vector for canonicalization.
|
||||||
for part in original.components() {
|
for part in original.components() {
|
||||||
match part {
|
match part {
|
||||||
Component::Prefix(_) |
|
Component::Prefix(_) | Component::RootDir => {
|
||||||
Component::RootDir => {
|
|
||||||
result.push(part.as_os_str());
|
result.push(part.as_os_str());
|
||||||
}
|
}
|
||||||
Component::CurDir => (),
|
Component::CurDir => (),
|
||||||
|
@ -117,12 +119,10 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
|
||||||
}
|
}
|
||||||
|
|
||||||
match resolve(&result) {
|
match resolve(&result) {
|
||||||
Err(e) => {
|
Err(e) => match can_mode {
|
||||||
match can_mode {
|
|
||||||
CanonicalizeMode::Missing => continue,
|
CanonicalizeMode::Missing => continue,
|
||||||
_ => return Err(e),
|
_ => return Err(e),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
Ok(path) => {
|
Ok(path) => {
|
||||||
result.pop();
|
result.pop();
|
||||||
result.push(path);
|
result.push(path);
|
||||||
|
|
|
@ -15,20 +15,22 @@ pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result<u32, String> {
|
||||||
Err(format!("mode is too large ({} > 7777)", mode))
|
Err(format!("mode is too large ({} > 7777)", mode))
|
||||||
} else {
|
} else {
|
||||||
match u32::from_str_radix(mode, 8) {
|
match u32::from_str_radix(mode, 8) {
|
||||||
Ok(change) => {
|
Ok(change) => Ok(match op {
|
||||||
Ok(match op {
|
|
||||||
'+' => fperm | change,
|
'+' => fperm | change,
|
||||||
'-' => fperm & !change,
|
'-' => fperm & !change,
|
||||||
'=' => change,
|
'=' => change,
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
})
|
}),
|
||||||
}
|
Err(err) => Err(err.description().to_owned()),
|
||||||
Err(err) => Err(err.description().to_owned())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_symbolic(mut fperm: u32, mut mode: &str, considering_dir: bool) -> Result<u32, String> {
|
pub fn parse_symbolic(
|
||||||
|
mut fperm: u32,
|
||||||
|
mut mode: &str,
|
||||||
|
considering_dir: bool,
|
||||||
|
) -> Result<u32, String> {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use libc::umask;
|
use libc::umask;
|
||||||
|
|
||||||
|
@ -43,9 +45,7 @@ pub fn parse_symbolic(mut fperm: u32, mut mode: &str, considering_dir: bool) ->
|
||||||
return Err(format!("invalid mode ({})", mode));
|
return Err(format!("invalid mode ({})", mode));
|
||||||
}
|
}
|
||||||
let respect_umask = pos == 0;
|
let respect_umask = pos == 0;
|
||||||
let last_umask = unsafe {
|
let last_umask = unsafe { umask(0) };
|
||||||
umask(0)
|
|
||||||
};
|
|
||||||
mode = &mode[pos..];
|
mode = &mode[pos..];
|
||||||
while mode.len() > 0 {
|
while mode.len() > 0 {
|
||||||
let (op, pos) = parse_op(mode, None)?;
|
let (op, pos) = parse_op(mode, None)?;
|
||||||
|
@ -59,7 +59,7 @@ pub fn parse_symbolic(mut fperm: u32, mut mode: &str, considering_dir: bool) ->
|
||||||
'+' => fperm |= srwx & mask,
|
'+' => fperm |= srwx & mask,
|
||||||
'-' => fperm &= !(srwx & mask),
|
'-' => fperm &= !(srwx & mask),
|
||||||
'=' => fperm = (fperm & !mask) | (srwx & mask),
|
'=' => fperm = (fperm & !mask) | (srwx & mask),
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -77,7 +77,7 @@ fn parse_levels(mode: &str) -> (u32, usize) {
|
||||||
'g' => 0o7070,
|
'g' => 0o7070,
|
||||||
'o' => 0o7007,
|
'o' => 0o7007,
|
||||||
'a' => 0o7777,
|
'a' => 0o7777,
|
||||||
_ => break
|
_ => break,
|
||||||
};
|
};
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
@ -93,10 +93,13 @@ fn parse_op(mode: &str, default: Option<char>) -> Result<(char, usize), String>
|
||||||
'+' | '-' | '=' => Ok((ch, 1)),
|
'+' | '-' | '=' => Ok((ch, 1)),
|
||||||
_ => match default {
|
_ => match default {
|
||||||
Some(ch) => Ok((ch, 0)),
|
Some(ch) => Ok((ch, 0)),
|
||||||
None => Err(format!("invalid operator (expected +, -, or =, but found {})", ch))
|
None => Err(format!(
|
||||||
}
|
"invalid operator (expected +, -, or =, but found {})",
|
||||||
|
ch
|
||||||
|
)),
|
||||||
},
|
},
|
||||||
None => Err("unexpected end of mode".to_owned())
|
},
|
||||||
|
None => Err("unexpected end of mode".to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +121,7 @@ fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) {
|
||||||
'u' => srwx = (fperm & 0o700) | ((fperm >> 3) & 0o070) | ((fperm >> 6) & 0o007),
|
'u' => srwx = (fperm & 0o700) | ((fperm >> 3) & 0o070) | ((fperm >> 6) & 0o007),
|
||||||
'g' => srwx = ((fperm << 3) & 0o700) | (fperm & 0o070) | ((fperm >> 3) & 0o007),
|
'g' => srwx = ((fperm << 3) & 0o700) | (fperm & 0o070) | ((fperm >> 3) & 0o007),
|
||||||
'o' => srwx = ((fperm << 6) & 0o700) | ((fperm << 3) & 0o070) | (fperm & 0o007),
|
'o' => srwx = ((fperm << 6) & 0o700) | ((fperm << 3) & 0o070) | (fperm & 0o007),
|
||||||
_ => break
|
_ => break,
|
||||||
};
|
};
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::time::Duration;
|
||||||
pub fn from_str(string: &str) -> Result<Duration, String> {
|
pub fn from_str(string: &str) -> Result<Duration, String> {
|
||||||
let len = string.len();
|
let len = string.len();
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
return Err("empty string".to_owned())
|
return Err("empty string".to_owned());
|
||||||
}
|
}
|
||||||
let slice = &string[..len - 1];
|
let slice = &string[..len - 1];
|
||||||
let (numstr, times) = match string.chars().next_back().unwrap() {
|
let (numstr, times) = match string.chars().next_back().unwrap() {
|
||||||
|
@ -26,13 +26,13 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
|
||||||
} else if string == "inf" || string == "infinity" {
|
} else if string == "inf" || string == "infinity" {
|
||||||
("inf", 1)
|
("inf", 1)
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("invalid time interval '{}'", string))
|
return Err(format!("invalid time interval '{}'", string));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let num = match numstr.parse::<f64>() {
|
let num = match numstr.parse::<f64>() {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => return Err(format!("invalid time interval '{}': {}", string, e))
|
Err(e) => return Err(format!("invalid time interval '{}': {}", string, e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
const NANOS_PER_SEC: u32 = 1_000_000_000;
|
const NANOS_PER_SEC: u32 = 1_000_000_000;
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
use super::libc;
|
use super::libc;
|
||||||
use libc::{c_int, pid_t, uid_t, gid_t};
|
use libc::{c_int, gid_t, pid_t, uid_t};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::process::Child;
|
use std::process::Child;
|
||||||
|
@ -17,27 +17,19 @@ use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
pub fn geteuid() -> uid_t {
|
pub fn geteuid() -> uid_t {
|
||||||
unsafe {
|
unsafe { libc::geteuid() }
|
||||||
libc::geteuid()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getegid() -> gid_t {
|
pub fn getegid() -> gid_t {
|
||||||
unsafe {
|
unsafe { libc::getegid() }
|
||||||
libc::getegid()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getgid() -> gid_t {
|
pub fn getgid() -> gid_t {
|
||||||
unsafe {
|
unsafe { libc::getgid() }
|
||||||
libc::getgid()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getuid() -> uid_t {
|
pub fn getuid() -> uid_t {
|
||||||
unsafe {
|
unsafe { libc::getuid() }
|
||||||
libc::getuid()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is basically sys::unix::process::ExitStatus
|
// This is basically sys::unix::process::ExitStatus
|
||||||
|
@ -109,7 +101,10 @@ impl ChildExt for Child {
|
||||||
fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result<Option<ExitStatus>> {
|
fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result<Option<ExitStatus>> {
|
||||||
// The result will be written to that Option, protected by a Mutex
|
// The result will be written to that Option, protected by a Mutex
|
||||||
// Then the Condvar will be signaled
|
// Then the Condvar will be signaled
|
||||||
let state = Arc::new((Mutex::new(Option::None::<io::Result<ExitStatus>>), Condvar::new()));
|
let state = Arc::new((
|
||||||
|
Mutex::new(Option::None::<io::Result<ExitStatus>>),
|
||||||
|
Condvar::new(),
|
||||||
|
));
|
||||||
|
|
||||||
// Start the waiting thread
|
// Start the waiting thread
|
||||||
let state_th = state.clone();
|
let state_th = state.clone();
|
||||||
|
|
|
@ -9,8 +9,10 @@
|
||||||
|
|
||||||
pub static DEFAULT_SIGNAL: usize = 15;
|
pub static DEFAULT_SIGNAL: usize = 15;
|
||||||
|
|
||||||
|
pub struct Signal<'a> {
|
||||||
pub struct Signal<'a> { pub name:&'a str, pub value: usize}
|
pub name: &'a str,
|
||||||
|
pub value: usize,
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -27,40 +29,132 @@ Linux Programmer's Manual
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
pub static ALL_SIGNALS: [Signal<'static>; 31] = [
|
pub static ALL_SIGNALS: [Signal<'static>; 31] = [
|
||||||
Signal{ name: "HUP", value:1 },
|
Signal {
|
||||||
Signal{ name: "INT", value:2 },
|
name: "HUP",
|
||||||
Signal{ name: "QUIT", value:3 },
|
value: 1,
|
||||||
Signal{ name: "ILL", value:4 },
|
},
|
||||||
Signal{ name: "TRAP", value:5 },
|
Signal {
|
||||||
Signal{ name: "ABRT", value:6 },
|
name: "INT",
|
||||||
Signal{ name: "BUS", value:7 },
|
value: 2,
|
||||||
Signal{ name: "FPE", value:8 },
|
},
|
||||||
Signal{ name: "KILL", value:9 },
|
Signal {
|
||||||
Signal{ name: "USR1", value:10 },
|
name: "QUIT",
|
||||||
Signal{ name: "SEGV", value:11 },
|
value: 3,
|
||||||
Signal{ name: "USR2", value:12 },
|
},
|
||||||
Signal{ name: "PIPE", value:13 },
|
Signal {
|
||||||
Signal{ name: "ALRM", value:14 },
|
name: "ILL",
|
||||||
Signal{ name: "TERM", value:15 },
|
value: 4,
|
||||||
Signal{ name: "STKFLT", value:16 },
|
},
|
||||||
Signal{ name: "CHLD", value:17 },
|
Signal {
|
||||||
Signal{ name: "CONT", value:18 },
|
name: "TRAP",
|
||||||
Signal{ name: "STOP", value:19 },
|
value: 5,
|
||||||
Signal{ name: "TSTP", value:20 },
|
},
|
||||||
Signal{ name: "TTIN", value:21 },
|
Signal {
|
||||||
Signal{ name: "TTOU", value:22 },
|
name: "ABRT",
|
||||||
Signal{ name: "URG", value:23 },
|
value: 6,
|
||||||
Signal{ name: "XCPU", value:24 },
|
},
|
||||||
Signal{ name: "XFSZ", value:25 },
|
Signal {
|
||||||
Signal{ name: "VTALRM", value:26 },
|
name: "BUS",
|
||||||
Signal{ name: "PROF", value:27 },
|
value: 7,
|
||||||
Signal{ name: "WINCH", value:28 },
|
},
|
||||||
Signal{ name: "POLL", value:29 },
|
Signal {
|
||||||
Signal{ name: "PWR", value:30 },
|
name: "FPE",
|
||||||
Signal{ name: "SYS", value:31 },
|
value: 8,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "KILL",
|
||||||
|
value: 9,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "USR1",
|
||||||
|
value: 10,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "SEGV",
|
||||||
|
value: 11,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "USR2",
|
||||||
|
value: 12,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "PIPE",
|
||||||
|
value: 13,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "ALRM",
|
||||||
|
value: 14,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TERM",
|
||||||
|
value: 15,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "STKFLT",
|
||||||
|
value: 16,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "CHLD",
|
||||||
|
value: 17,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "CONT",
|
||||||
|
value: 18,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "STOP",
|
||||||
|
value: 19,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TSTP",
|
||||||
|
value: 20,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TTIN",
|
||||||
|
value: 21,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TTOU",
|
||||||
|
value: 22,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "URG",
|
||||||
|
value: 23,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "XCPU",
|
||||||
|
value: 24,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "XFSZ",
|
||||||
|
value: 25,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "VTALRM",
|
||||||
|
value: 26,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "PROF",
|
||||||
|
value: 27,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "WINCH",
|
||||||
|
value: 28,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "POLL",
|
||||||
|
value: 29,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "PWR",
|
||||||
|
value: 30,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "SYS",
|
||||||
|
value: 31,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,37 +198,130 @@ No Name Default Action Description
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
||||||
pub static ALL_SIGNALS: [Signal<'static>; 31] = [
|
pub static ALL_SIGNALS: [Signal<'static>; 31] = [
|
||||||
Signal{ name: "HUP", value:1 },
|
Signal {
|
||||||
Signal{ name: "INT", value:2 },
|
name: "HUP",
|
||||||
Signal{ name: "QUIT", value:3 },
|
value: 1,
|
||||||
Signal{ name: "ILL", value:4 },
|
},
|
||||||
Signal{ name: "TRAP", value:5 },
|
Signal {
|
||||||
Signal{ name: "ABRT", value:6 },
|
name: "INT",
|
||||||
Signal{ name: "EMT", value:7 },
|
value: 2,
|
||||||
Signal{ name: "FPE", value:8 },
|
},
|
||||||
Signal{ name: "KILL", value:9 },
|
Signal {
|
||||||
Signal{ name: "BUS", value:10 },
|
name: "QUIT",
|
||||||
Signal{ name: "SEGV", value:11 },
|
value: 3,
|
||||||
Signal{ name: "SYS", value:12 },
|
},
|
||||||
Signal{ name: "PIPE", value:13 },
|
Signal {
|
||||||
Signal{ name: "ALRM", value:14 },
|
name: "ILL",
|
||||||
Signal{ name: "TERM", value:15 },
|
value: 4,
|
||||||
Signal{ name: "URG", value:16 },
|
},
|
||||||
Signal{ name: "STOP", value:17 },
|
Signal {
|
||||||
Signal{ name: "TSTP", value:18 },
|
name: "TRAP",
|
||||||
Signal{ name: "CONT", value:19 },
|
value: 5,
|
||||||
Signal{ name: "CHLD", value:20 },
|
},
|
||||||
Signal{ name: "TTIN", value:21 },
|
Signal {
|
||||||
Signal{ name: "TTOU", value:22 },
|
name: "ABRT",
|
||||||
Signal{ name: "IO", value:23 },
|
value: 6,
|
||||||
Signal{ name: "XCPU", value:24 },
|
},
|
||||||
Signal{ name: "XFSZ", value:25 },
|
Signal {
|
||||||
Signal{ name: "VTALRM", value:26 },
|
name: "EMT",
|
||||||
Signal{ name: "PROF", value:27 },
|
value: 7,
|
||||||
Signal{ name: "WINCH", value:28 },
|
},
|
||||||
Signal{ name: "INFO", value:29 },
|
Signal {
|
||||||
Signal{ name: "USR1", value:30 },
|
name: "FPE",
|
||||||
Signal{ name: "USR2", value:31 },
|
value: 8,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "KILL",
|
||||||
|
value: 9,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "BUS",
|
||||||
|
value: 10,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "SEGV",
|
||||||
|
value: 11,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "SYS",
|
||||||
|
value: 12,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "PIPE",
|
||||||
|
value: 13,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "ALRM",
|
||||||
|
value: 14,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TERM",
|
||||||
|
value: 15,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "URG",
|
||||||
|
value: 16,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "STOP",
|
||||||
|
value: 17,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TSTP",
|
||||||
|
value: 18,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "CONT",
|
||||||
|
value: 19,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "CHLD",
|
||||||
|
value: 20,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TTIN",
|
||||||
|
value: 21,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "TTOU",
|
||||||
|
value: 22,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "IO",
|
||||||
|
value: 23,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "XCPU",
|
||||||
|
value: 24,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "XFSZ",
|
||||||
|
value: 25,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "VTALRM",
|
||||||
|
value: 26,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "PROF",
|
||||||
|
value: 27,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "WINCH",
|
||||||
|
value: 28,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "INFO",
|
||||||
|
value: 29,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "USR1",
|
||||||
|
value: 30,
|
||||||
|
},
|
||||||
|
Signal {
|
||||||
|
name: "USR2",
|
||||||
|
value: 31,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option<usize> {
|
pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option<usize> {
|
||||||
|
@ -143,7 +330,9 @@ pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option<usize> {
|
||||||
}
|
}
|
||||||
for signal in &ALL_SIGNALS {
|
for signal in &ALL_SIGNALS {
|
||||||
let long_name = format!("SIG{}", signal.name);
|
let long_name = format!("SIG{}", signal.name);
|
||||||
if signal.name == signal_name_or_value || (signal_name_or_value == signal.value.to_string()) || (long_name == signal_name_or_value) {
|
if signal.name == signal_name_or_value || (signal_name_or_value == signal.value.to_string())
|
||||||
|
|| (long_name == signal_name_or_value)
|
||||||
|
{
|
||||||
return Some(signal.value);
|
return Some(signal.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,4 +25,3 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [
|
||||||
pub fn utf8_char_width(b: u8) -> usize {
|
pub fn utf8_char_width(b: u8) -> usize {
|
||||||
return UTF8_CHAR_WIDTH[b as usize] as usize;
|
return UTF8_CHAR_WIDTH[b as usize] as usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,12 +33,12 @@
|
||||||
|
|
||||||
use super::libc;
|
use super::libc;
|
||||||
pub extern crate time;
|
pub extern crate time;
|
||||||
use self::time::{Tm, Timespec};
|
use self::time::{Timespec, Tm};
|
||||||
|
|
||||||
use ::std::io::Result as IOResult;
|
use std::io::Result as IOResult;
|
||||||
use ::std::io::Error as IOError;
|
use std::io::Error as IOError;
|
||||||
use ::std::ptr;
|
use std::ptr;
|
||||||
use ::std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
pub use self::ut::*;
|
pub use self::ut::*;
|
||||||
use libc::utmpx;
|
use libc::utmpx;
|
||||||
|
@ -159,8 +159,10 @@ impl Utmpx {
|
||||||
}
|
}
|
||||||
/// A.K.A. ut.ut_tv
|
/// A.K.A. ut.ut_tv
|
||||||
pub fn login_time(&self) -> Tm {
|
pub fn login_time(&self) -> Tm {
|
||||||
time::at(Timespec::new(self.inner.ut_tv.tv_sec as i64,
|
time::at(Timespec::new(
|
||||||
self.inner.ut_tv.tv_usec as i32))
|
self.inner.ut_tv.tv_sec as i64,
|
||||||
|
self.inner.ut_tv.tv_usec as i32,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
/// A.K.A. ut.ut_exit
|
/// A.K.A. ut.ut_exit
|
||||||
///
|
///
|
||||||
|
@ -202,10 +204,12 @@ impl Utmpx {
|
||||||
let c_host = CString::new(host).unwrap();
|
let c_host = CString::new(host).unwrap();
|
||||||
let mut res = ptr::null_mut();
|
let mut res = ptr::null_mut();
|
||||||
let status = unsafe {
|
let status = unsafe {
|
||||||
libc::getaddrinfo(c_host.as_ptr(),
|
libc::getaddrinfo(
|
||||||
|
c_host.as_ptr(),
|
||||||
ptr::null(),
|
ptr::null(),
|
||||||
&hints as *const _,
|
&hints as *const _,
|
||||||
&mut res as *mut _)
|
&mut res as *mut _,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
if status == 0 {
|
if status == 0 {
|
||||||
let info: libc::addrinfo = unsafe { ptr::read(res as *const _) };
|
let info: libc::addrinfo = unsafe { ptr::read(res as *const _) };
|
||||||
|
@ -255,7 +259,9 @@ impl Iterator for UtmpxIter {
|
||||||
unsafe {
|
unsafe {
|
||||||
let res = getutxent();
|
let res = getutxent();
|
||||||
if !res.is_null() {
|
if !res.is_null() {
|
||||||
Some(Utmpx { inner: ptr::read(res as *const _) })
|
Some(Utmpx {
|
||||||
|
inner: ptr::read(res as *const _),
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
endutxent();
|
endutxent();
|
||||||
None
|
None
|
||||||
|
|
|
@ -12,7 +12,10 @@ pub trait ToWide {
|
||||||
fn to_wide(&self) -> Vec<u16>;
|
fn to_wide(&self) -> Vec<u16>;
|
||||||
fn to_wide_null(&self) -> Vec<u16>;
|
fn to_wide_null(&self) -> Vec<u16>;
|
||||||
}
|
}
|
||||||
impl<T> ToWide for T where T: AsRef<OsStr> {
|
impl<T> ToWide for T
|
||||||
|
where
|
||||||
|
T: AsRef<OsStr>,
|
||||||
|
{
|
||||||
fn to_wide(&self) -> Vec<u16> {
|
fn to_wide(&self) -> Vec<u16> {
|
||||||
self.as_ref().encode_wide().collect()
|
self.as_ref().encode_wide().collect()
|
||||||
}
|
}
|
||||||
|
@ -30,6 +33,8 @@ impl FromWide for String {
|
||||||
}
|
}
|
||||||
fn from_wide_null(wide: &[u16]) -> String {
|
fn from_wide_null(wide: &[u16]) -> String {
|
||||||
let len = wide.iter().take_while(|&&c| c != 0).count();
|
let len = wide.iter().take_while(|&&c| c != 0).count();
|
||||||
OsString::from_wide(&wide[..len]).to_string_lossy().into_owned()
|
OsString::from_wide(&wide[..len])
|
||||||
|
.to_string_lossy()
|
||||||
|
.into_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue