diff --git a/src/chroot/chroot.rs b/src/chroot/chroot.rs index c718460c6..a4d74c04c 100644 --- a/src/chroot/chroot.rs +++ b/src/chroot/chroot.rs @@ -14,7 +14,9 @@ extern crate libc; use getopts::{optflag, optopt, getopts, usage}; use c_types::{get_pw_from_args, get_group}; use libc::funcs::posix88::unistd::{execvp, setuid, setgid}; +use std::ffi::c_str_to_bytes; use std::io::fs::PathExtensions; +use std::iter::FromIterator; #[path = "../common/util.rs"] #[macro_use] mod util; #[path = "../common/c_types.rs"] mod c_types; @@ -191,8 +193,9 @@ fn set_user(user: &str) { fn strerror(errno: i32) -> String { unsafe { - let err = libc::funcs::c95::string::strerror(errno); - String::from_raw_buf(err as *const u8) + let err = libc::funcs::c95::string::strerror(errno) as *const libc::c_char; + let bytes= c_str_to_bytes(&err); + String::from_utf8_lossy(bytes).to_string() } } diff --git a/src/comm/comm.rs b/src/comm/comm.rs index 670c56c1e..8f16cdc0f 100644 --- a/src/comm/comm.rs +++ b/src/comm/comm.rs @@ -11,7 +11,7 @@ extern crate getopts; -use std::cmp::Ord; +use std::cmp::Ordering; use std::io::{BufferedReader, IoResult, print}; use std::io::fs::File; use std::io::stdio::{stdin, StdinReader}; @@ -60,33 +60,33 @@ impl LineReader { fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { - let delim = Vec::from_fn(4, |col| mkdelim(col, opts)); + let delim : Vec = range(0, 4).map(|col| mkdelim(col, opts)).collect(); let mut ra = a.read_line(); let mut rb = b.read_line(); while ra.is_ok() || rb.is_ok() { let ord = match (ra.clone(), rb.clone()) { - (Err(_), Ok(_)) => Greater, - (Ok(_) , Err(_)) => Less, + (Err(_), Ok(_)) => Ordering::Greater, + (Ok(_) , Err(_)) => Ordering::Less, (Ok(s0), Ok(s1)) => s0.cmp(&s1), _ => unreachable!(), }; match ord { - Less => { + Ordering::Less => { if !opts.opt_present("1") { print!("{}{}", delim[1], ra.map(ensure_nl).unwrap()); } ra = a.read_line(); } - Greater => { + Ordering::Greater => { if !opts.opt_present("2") { print!("{}{}", delim[2], rb.map(ensure_nl).unwrap()); } rb = b.read_line(); } - Equal => { + Ordering::Equal => { if !opts.opt_present("3") { print!("{}{}", delim[3], ra.map(ensure_nl).unwrap()); } diff --git a/src/common/c_types.rs b/src/common/c_types.rs index e96cbd83f..0650a51d7 100644 --- a/src/common/c_types.rs +++ b/src/common/c_types.rs @@ -15,6 +15,8 @@ use self::libc::int32_t; use self::libc::funcs::posix88::unistd::getgroups; +use std::ffi::{c_str_to_bytes, CString}; +use std::iter::repeat; use std::vec::Vec; use std::os; @@ -122,7 +124,7 @@ pub fn get_pw_from_args(free: &Vec) -> Option { let id = username.parse::().unwrap(); let pw_pointer = unsafe { getpwuid(id as uid_t) }; - if pw_pointer.is_not_null() { + if !pw_pointer.is_null() { Some(unsafe { read(pw_pointer) }) } else { crash!(1, "{}: no such user", username); @@ -131,9 +133,10 @@ pub fn get_pw_from_args(free: &Vec) -> Option { // Passed the username as a string } else { let pw_pointer = unsafe { - getpwnam(username.as_slice().to_c_str().into_inner() as *const libc::c_char) + let cstr = CString::from_slice(username.as_bytes()); + getpwnam(cstr.as_slice_with_nul().as_ptr()) }; - if pw_pointer.is_not_null() { + if !pw_pointer.is_null() { Some(unsafe { read(pw_pointer) }) } else { crash!(1, "{}: no such user", username); @@ -148,10 +151,13 @@ pub fn get_group(groupname: &str) -> Option { let group = if groupname.chars().all(|c| c.is_digit(10)) { unsafe { getgrgid(groupname.parse().unwrap()) } } else { - unsafe { getgrnam(groupname.to_c_str().into_inner() as *const c_char) } + unsafe { + let cstr = CString::from_slice(groupname.as_bytes()); + getgrnam(cstr.as_slice_with_nul().as_ptr() as *const c_char) + } }; - if group.is_not_null() { + if !group.is_null() { Some(unsafe { read(group) }) } else { @@ -199,7 +205,7 @@ pub fn get_groups() -> Result, uint> { return Err(os::errno()); } - let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t); + let mut groups : Vec= repeat(0).take(ngroups as uint).collect(); let ngroups = unsafe { getgroups(ngroups, groups.as_mut_ptr()) }; if ngroups == -1 { Err(os::errno()) @@ -222,9 +228,11 @@ pub fn group(possible_pw: Option, nflag: bool) { for &g in groups.iter() { if nflag { let group = unsafe { getgrgid(g) }; - if group.is_not_null() { + if !group.is_null() { let name = unsafe { - String::from_raw_buf(read(group).gr_name as *const u8) + let gname = read(group).gr_name; + let bytes= c_str_to_bytes(&gname); + String::from_utf8_lossy(bytes).to_string() }; print!("{} ", name); } diff --git a/src/hashsum/hashsum.rs b/src/hashsum/hashsum.rs index 199ef94b3..f5ed9a056 100644 --- a/src/hashsum/hashsum.rs +++ b/src/hashsum/hashsum.rs @@ -71,7 +71,7 @@ fn detect_algo(program: &str, matches: &getopts::Matches) -> (&'static str, Box< "sha512sum" => ("SHA512", box Sha512::new() as Box), _ => { { - let set_or_crash = |n, val| -> () { + let mut set_or_crash = |&mut: n, val| -> () { if alg.is_some() { crash!(1, "You cannot combine multiple hash algorithms!") }; name = n; alg = Some(val); diff --git a/src/hostname/hostname.rs b/src/hostname/hostname.rs index d0f7d8994..5ad247404 100644 --- a/src/hostname/hostname.rs +++ b/src/hostname/hostname.rs @@ -17,6 +17,7 @@ extern crate libc; use std::collections::hash_set::HashSet; use std::io::net::addrinfo; +use std::iter::repeat; use std::str; use getopts::{optflag, getopts, usage}; @@ -130,8 +131,7 @@ fn help_menu(program: &str, options: &[getopts::OptGroup]) { fn xgethostname() -> String { let namelen = 256u; - let mut name = Vec::from_elem(namelen, 0u8); - + let mut name : Vec = repeat(0).take(namelen).collect(); let err = unsafe { gethostname (name.as_mut_ptr() as *mut libc::c_char, namelen as libc::size_t) diff --git a/src/logname/logname.rs b/src/logname/logname.rs index 0571640e1..f5a36ca6c 100644 --- a/src/logname/logname.rs +++ b/src/logname/logname.rs @@ -16,6 +16,7 @@ extern crate getopts; extern crate libc; +use std::ffi::c_str_to_bytes; use std::io::print; use libc::c_char; @@ -32,7 +33,7 @@ fn get_userlogin() -> Option { if login.is_null() { None } else { - Some(String::from_raw_buf(login as *const u8)) + Some(String::from_utf8_lossy(c_str_to_bytes(&login)).to_string()) } } } diff --git a/src/paste/paste.rs b/src/paste/paste.rs index 72af5d141..a4ecbe8e6 100644 --- a/src/paste/paste.rs +++ b/src/paste/paste.rs @@ -13,6 +13,7 @@ extern crate getopts; extern crate libc; use std::io; +use std::iter::repeat; #[path = "../common/util.rs"] #[macro_use] @@ -88,7 +89,7 @@ fn paste(filenames: Vec, serial: bool, delimiters: &str) { println!("{}", output.as_slice().slice_to(output.len() - 1)); } } else { - let mut eof = Vec::from_elem(files.len(), false); + let mut eof : Vec = repeat(false).take(files.len()).collect(); loop { let mut output = "".to_string(); let mut eof_count = 0; diff --git a/src/shuf/shuf.rs b/src/shuf/shuf.rs index 5ca8fbf6d..946967d27 100644 --- a/src/shuf/shuf.rs +++ b/src/shuf/shuf.rs @@ -151,7 +151,7 @@ fn shuf(input: Vec, mode: Mode, repeat: bool, zero: bool, count: uint, o enum WrappedRng { RngFile(rand::reader::ReaderRng), - RngDefault(rand::TaskRng), + RngDefault(rand::ThreadRng), } impl WrappedRng { @@ -170,7 +170,7 @@ fn shuf_lines(mut lines: Vec, repeat: bool, zero: bool, count: uint, out }; let mut rng = match random { Some(name) => WrappedRng::RngFile(rand::reader::ReaderRng::new(try!(io::File::open(&Path::new(name))))), - None => WrappedRng::RngDefault(rand::task_rng()), + None => WrappedRng::RngDefault(rand::thread_rng()), }; let mut len = lines.len(); let max = if repeat { count } else { cmp::min(count, len) }; diff --git a/src/split/split.rs b/src/split/split.rs index 6dd2e33f7..f5a26f4dd 100644 --- a/src/split/split.rs +++ b/src/split/split.rs @@ -177,7 +177,7 @@ impl Splitter for ByteSplitter { _ => crash!(1, "invalid number of bytes") }; let n = if suffix.is_alphabetic() { - match String::from_chars(strategy_param.as_slice()).as_slice().parse::() { + match strategy_param.as_slice().iter().map(|c| *c).collect::().as_slice().parse::() { Some(a) => a, _ => crash!(1, "invalid number of bytes") } diff --git a/src/sum/sum.rs b/src/sum/sum.rs index 4d46f421d..c92bc8a18 100644 --- a/src/sum/sum.rs +++ b/src/sum/sum.rs @@ -108,7 +108,7 @@ pub fn uumain(args: Vec) -> int { let sysv = matches.opt_present("sysv"); let files = if matches.free.is_empty() { - Vec::from_elem(1, "-".to_string()) + vec!["-".to_string()] } else { matches.free }; diff --git a/src/sync/sync.rs b/src/sync/sync.rs index 2b9903544..8f007dfce 100644 --- a/src/sync/sync.rs +++ b/src/sync/sync.rs @@ -102,7 +102,7 @@ mod platform { unsafe fn find_all_volumes() -> Vec { match find_first_volume() { (first_volume, next_volume_handle) => { - let mut volumes = Vec::from_elem(1, first_volume); + let mut volumes = vec![first_volume]; loop { let mut name: [libc::c_char; 260] = mem::uninitialized(); // MAX_PATH match FindNextVolumeA(next_volume_handle, diff --git a/src/tee/tee.rs b/src/tee/tee.rs index cd26e58e1..e2f290caa 100644 --- a/src/tee/tee.rs +++ b/src/tee/tee.rs @@ -1,7 +1,5 @@ #![crate_name = "tee"] #![feature(phase)] -#![feature(macro_rules)] - /* * This file is part of the uutils coreutils package. * @@ -12,7 +10,7 @@ */ extern crate getopts; -#[phase(plugin, link)] extern crate log; +#[macro_use] extern crate log; use std::io::{println, stdin, stdout, Append, File, Truncate, Write}; use std::io::{IoResult}; @@ -143,7 +141,7 @@ impl Reader for NamedReader { } } -fn with_path(path: &Path, cb: || -> IoResult) -> IoResult { +fn with_path(path: &Path, cb: F) -> IoResult where F: Fn() -> IoResult { match cb() { Err(f) => { warn(format!("{}: {}", path.display(), f.to_string()).as_slice()); Err(f) } okay => okay diff --git a/src/tty/tty.rs b/src/tty/tty.rs index 3c08489c5..d7d0aaf2d 100644 --- a/src/tty/tty.rs +++ b/src/tty/tty.rs @@ -17,6 +17,7 @@ extern crate getopts; extern crate libc; +use std::ffi::c_str_to_bytes; use std::io::println; use std::io::stdio::stderr; use getopts::{optflag,getopts}; @@ -48,7 +49,14 @@ pub fn uumain(args: Vec) -> int { } }; - let tty = unsafe { String::from_raw_buf(ttyname(libc::STDIN_FILENO) as *const u8) }; + let tty = unsafe { + let ptr = ttyname(libc::STDIN_FILENO); + if !ptr.is_null() { + String::from_utf8_lossy(c_str_to_bytes(&ptr)).to_string() + } else { + "".to_string() + } + }; if !silent { if !tty.as_slice().chars().all(|c| c.is_whitespace()) { diff --git a/src/uname/uname.rs b/src/uname/uname.rs index c7e8a1ff1..c944891b4 100644 --- a/src/uname/uname.rs +++ b/src/uname/uname.rs @@ -16,6 +16,7 @@ extern crate getopts; extern crate libc; +use std::ffi::c_str_to_bytes; use std::mem::uninitialized; use std::io::print; use c_types::utsname; @@ -35,15 +36,19 @@ extern { fn uname(uts: *mut utsname); } +unsafe fn string_from_c_str(ptr: *const i8) -> String { + String::from_utf8_lossy(c_str_to_bytes(&ptr)).to_string() +} + unsafe fn getuname() -> utsrust { let mut uts: utsname = uninitialized(); uname(&mut uts); utsrust { - sysname: String::from_raw_buf(uts.sysname.as_ptr() as *const u8), - nodename: String::from_raw_buf(uts.nodename.as_ptr() as *const u8), - release: String::from_raw_buf(uts.release.as_ptr() as *const u8), - version: String::from_raw_buf(uts.version.as_ptr() as *const u8), - machine: String::from_raw_buf(uts.machine.as_ptr() as *const u8) + sysname: string_from_c_str(uts.sysname.as_ptr() as *const i8), + nodename: string_from_c_str(uts.nodename.as_ptr() as *const i8), + release: string_from_c_str(uts.release.as_ptr() as *const i8), + version: string_from_c_str(uts.version.as_ptr() as *const i8), + machine: string_from_c_str(uts.machine.as_ptr() as *const i8) } } diff --git a/src/users/users.rs b/src/users/users.rs index 122de7022..39bc99220 100644 --- a/src/users/users.rs +++ b/src/users/users.rs @@ -17,6 +17,7 @@ extern crate getopts; extern crate libc; +use std::ffi::c_str_to_bytes; use std::io::print; use std::mem; use std::ptr; @@ -107,7 +108,7 @@ fn exec(filename: &str) { } if (*line).ut_type == USER_PROCESS { - let user = String::from_raw_buf(mem::transmute(&(*line).ut_user)); + let user = String::from_utf8_lossy(c_str_to_bytes(mem::transmute(&(*line).ut_user))).to_string(); users.push(user); } }