From c93440170da8935a35fbc1e6f0c319bf022fbc59 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sun, 17 May 2015 14:59:04 -0400 Subject: [PATCH 1/2] Fix id. --- src/common/c_types.rs | 12 +++--------- src/id/id.rs | 5 +++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/common/c_types.rs b/src/common/c_types.rs index 8ff1cc8c9..d41871257 100644 --- a/src/common/c_types.rs +++ b/src/common/c_types.rs @@ -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; diff --git a/src/id/id.rs b/src/id/id.rs index 0918b9471..2b8b6fb5a 100644 --- a/src/id/id.rs +++ b/src/id/id.rs @@ -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) -> i32 { - let args_t = args.tail(); + let args_t = &args[1..]; let options = [ optflag("h", "", "Show help"), From 7d71dfbbf41dbcb4c3fd8a9eb936e540924dd022 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sun, 17 May 2015 15:26:52 -0400 Subject: [PATCH 2/2] Fix kill. --- src/common/signals.rs | 2 +- src/kill/kill.rs | 57 +++++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/common/signals.rs b/src/common/signals.rs index 37b567d5f..f48fefd2f 100644 --- a/src/common/signals.rs +++ b/src/common/signals.rs @@ -145,7 +145,7 @@ pub fn signal_by_name_or_value(signal_name_or_value: &str) -> Option { } 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); } } diff --git a/src/kill/kill.rs b/src/kill/kill.rs index 1790820a5..f0a044a5b 100644 --- a/src/kill/kill.rs +++ b/src/kill/kill.rs @@ -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) -> i32 { let opts = [ optflag("h", "help", "display this help and exit"), @@ -66,10 +59,10 @@ pub fn uumain(args: Vec) -> 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) -> 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) -> (Vec, Option) { 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) { 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) -> i32 { None => crash!(EXIT_ERR, "unknown signal name {}", signalname) }; for pid in pids.iter() { - match pid.as_slice().parse() { + match pid.parse::() { 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) };