1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 14:07:46 +00:00

Merge pull request #604 from jbcrail/fix-id-kill

Fix id and kill.
This commit is contained in:
Heather 2015-05-17 22:47:30 +03:00
commit fda91c492a
4 changed files with 30 additions and 46 deletions

View file

@ -1,4 +1,4 @@
#![allow(dead_code, non_camel_case_types)]
#![allow(dead_code, non_camel_case_types, raw_pointer_derive)]
extern crate libc;
@ -24,6 +24,7 @@ use std::ptr::{null_mut, read};
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct c_passwd {
pub pw_name: *const c_char, /* user name */
pub pw_passwd: *const c_char, /* user name */
@ -39,6 +40,7 @@ pub struct c_passwd {
#[cfg(target_os = "linux")]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct c_passwd {
pub pw_name: *const c_char, /* user name */
pub pw_passwd: *const c_char, /* user name */
@ -49,8 +51,6 @@ pub struct c_passwd {
pub pw_shell: *const c_char,
}
//impl Copy for c_passwd {}
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
#[repr(C)]
pub struct utsname {
@ -72,8 +72,6 @@ pub struct utsname {
pub domainame: [c_char; 65]
}
//impl Copy for utsname {}
#[repr(C)]
pub struct c_group {
pub gr_name: *const c_char, // group name
@ -82,8 +80,6 @@ pub struct c_group {
pub gr_mem: *const *const c_char, // member list
}
//impl Copy for c_group {}
#[repr(C)]
pub struct c_tm {
pub tm_sec: c_int, /* seconds */
@ -97,8 +93,6 @@ pub struct c_tm {
pub tm_isdst: c_int /* daylight saving time */
}
//impl Copy for c_tm {}
extern {
pub fn getpwuid(uid: uid_t) -> *const c_passwd;
pub fn getpwnam(login: *const c_char) -> *const c_passwd;

View file

@ -145,7 +145,7 @@ pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option<usize> {
}
for signal in ALL_SIGNALS.iter() {
let long_name = format!("SIG{}", signal.name);
if signal.name == signal_name_or_value || (signal_name_or_value == signal.value.to_string().as_slice()) || (long_name.as_slice() == 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);
}
}

View file

@ -1,5 +1,5 @@
#![crate_name = "id"]
#![feature(collections, core, rustc_private, std_misc)]
#![feature(rustc_private)]
/*
* This file is part of the uutils coreutils package.
@ -18,6 +18,7 @@
extern crate getopts;
extern crate libc;
use std::io::Write;
use std::ffi::CStr;
use std::ptr::read;
use libc::{
@ -86,7 +87,7 @@ extern {
static NAME: &'static str = "id";
pub fn uumain(args: Vec<String>) -> i32 {
let args_t = args.tail();
let args_t = &args[1..];
let options = [
optflag("h", "", "Show help"),

View file

@ -1,5 +1,5 @@
#![crate_name = "kill"]
#![feature(collections, core, old_io, rustc_private, unicode)]
#![feature(rustc_private)]
/*
* This file is part of the uutils coreutils package.
@ -10,39 +10,34 @@
* that was distributed with this source code.
*/
extern crate getopts;
extern crate libc;
extern crate collections;
extern crate serialize;
#[macro_use] extern crate log;
use std::old_io::process::Process;
use getopts::{
getopts,
optopt,
optflag,
optflagopt,
usage,
};
use getopts::{getopts, optflag, optflagopt, optopt, usage};
use libc::{c_int, pid_t};
use signals::ALL_SIGNALS;
use std::io::{Error, Write};
#[path = "../common/util.rs"]
#[macro_use]
mod util;
#[path = "../common/c_types.rs"]
mod c_types;
#[path = "../common/signals.rs"]
mod signals;
static NAME: &'static str = "kill";
static VERSION: &'static str = "0.0.1";
static VERSION: &'static str = "0.0.1";
static EXIT_OK: i32 = 0;
static EXIT_ERR: i32 = 1;
#[derive(Clone, Copy)]
pub enum Mode {
Kill,
Table,
@ -51,8 +46,6 @@ pub enum Mode {
Version,
}
impl Copy for Mode {}
pub fn uumain(args: Vec<String>) -> i32 {
let opts = [
optflag("h", "help", "display this help and exit"),
@ -66,10 +59,10 @@ pub fn uumain(args: Vec<String>) -> i32 {
let (args, obs_signal) = handle_obsolete(args);
let matches = match getopts(args.tail(), &opts) {
let matches = match getopts(&args[1..], &opts) {
Ok(m) => m,
Err(e) => {
show_error!("{}\n{}", e, get_help_text(NAME, usage.as_slice()));
show_error!("{}\n{}", e, get_help_text(NAME, &usage));
return EXIT_ERR;
},
};
@ -87,10 +80,10 @@ pub fn uumain(args: Vec<String>) -> i32 {
};
match mode {
Mode::Kill => return kill(matches.opt_str("signal").unwrap_or(obs_signal.unwrap_or("9".to_string())).as_slice(), matches.free),
Mode::Kill => return kill(&matches.opt_str("signal").unwrap_or(obs_signal.unwrap_or("9".to_string())), matches.free),
Mode::Table => table(),
Mode::List => list(matches.opt_str("list")),
Mode::Help => help(NAME, usage.as_slice()),
Mode::Help => help(NAME, &usage),
Mode::Version => version(),
}
@ -105,8 +98,8 @@ fn handle_obsolete(mut args: Vec<String>) -> (Vec<String>, Option<String>) {
let mut i = 0;
while i < args.len() {
// this is safe because slice is valid when it is referenced
let slice: &str = unsafe { std::mem::transmute(args[i].as_slice()) };
if slice.char_at(0) == '-' && slice.len() > 1 && slice.char_at(1).is_digit(10) {
let slice = &args[i].clone();//unsafe { std::mem::transmute(args[i]) };
if slice.chars().next().unwrap() == '-' && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) {
let val = &slice[1..];
match val.parse() {
Ok(num) => {
@ -144,10 +137,10 @@ fn table() {
fn print_signal(signal_name_or_value: &str) {
for signal in ALL_SIGNALS.iter() {
if signal.name == signal_name_or_value || (format!("SIG{}", signal.name).as_slice()) == signal_name_or_value {
if signal.name == signal_name_or_value || (format!("SIG{}", signal.name)) == signal_name_or_value {
println!("{}", signal.value);
exit!(EXIT_OK as i32)
} else if signal_name_or_value == signal.value.to_string().as_slice() {
} else if signal_name_or_value == signal.value.to_string() {
println!("{}", signal.name);
exit!(EXIT_OK as i32)
}
@ -172,7 +165,7 @@ fn print_signals() {
fn list(arg: Option<String>) {
match arg {
Some(x) => print_signal(x.as_slice()),
Some(ref x) => print_signal(x),
None => print_signals(),
};
}
@ -193,16 +186,12 @@ fn kill(signalname: &str, pids: std::vec::Vec<String>) -> i32 {
None => crash!(EXIT_ERR, "unknown signal name {}", signalname)
};
for pid in pids.iter() {
match pid.as_slice().parse() {
match pid.parse::<usize>() {
Ok(x) => {
let result = Process::kill(x, signal_value as isize);
match result {
Ok(_) => (),
Err(f) => {
show_error!("{}", f);
status = 1;
}
};
if unsafe { libc::funcs::posix88::signal::kill(x as pid_t, signal_value as c_int) } != 0 {
show_error!("{}", Error::last_os_error());
status = 1;
}
},
Err(e) => crash!(EXIT_ERR, "failed to parse argument {}: {}", pid, e)
};