1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-03 06:27:45 +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; extern crate libc;
@ -24,6 +24,7 @@ use std::ptr::{null_mut, read};
#[cfg(any(target_os = "macos", target_os = "freebsd"))] #[cfg(any(target_os = "macos", target_os = "freebsd"))]
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)]
pub struct c_passwd { pub struct c_passwd {
pub pw_name: *const c_char, /* user name */ pub pw_name: *const c_char, /* user name */
pub pw_passwd: *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")] #[cfg(target_os = "linux")]
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)]
pub struct c_passwd { pub struct c_passwd {
pub pw_name: *const c_char, /* user name */ pub pw_name: *const c_char, /* user name */
pub pw_passwd: *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, pub pw_shell: *const c_char,
} }
//impl Copy for c_passwd {}
#[cfg(any(target_os = "macos", target_os = "freebsd"))] #[cfg(any(target_os = "macos", target_os = "freebsd"))]
#[repr(C)] #[repr(C)]
pub struct utsname { pub struct utsname {
@ -72,8 +72,6 @@ pub struct utsname {
pub domainame: [c_char; 65] pub domainame: [c_char; 65]
} }
//impl Copy for utsname {}
#[repr(C)] #[repr(C)]
pub struct c_group { pub struct c_group {
pub gr_name: *const c_char, // group name 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 pub gr_mem: *const *const c_char, // member list
} }
//impl Copy for c_group {}
#[repr(C)] #[repr(C)]
pub struct c_tm { pub struct c_tm {
pub tm_sec: c_int, /* seconds */ pub tm_sec: c_int, /* seconds */
@ -97,8 +93,6 @@ pub struct c_tm {
pub tm_isdst: c_int /* daylight saving time */ pub tm_isdst: c_int /* daylight saving time */
} }
//impl Copy for c_tm {}
extern { extern {
pub fn getpwuid(uid: uid_t) -> *const c_passwd; pub fn getpwuid(uid: uid_t) -> *const c_passwd;
pub fn getpwnam(login: *const c_char) -> *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() { for signal in ALL_SIGNALS.iter() {
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().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); return Some(signal.value);
} }
} }

View file

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

View file

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