Partially fixed build with rust master

String::from_raw_buf removal
Deprecated closure syntax
cmp::Ord -> cmp::Ordering
Vec::from_{elem,fn} removal
rand::TaskRng -> rand::ThreadRng
PtrExt::is_not_null removal
to_c_str removal
This commit is contained in:
Michael Gehring 2015-01-08 21:59:07 +01:00
parent 49ace1933d
commit 0a757b957f
15 changed files with 63 additions and 38 deletions

View file

@ -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()
}
}

View file

@ -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<String> = 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());
}

View file

@ -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<String>) -> Option<c_passwd> {
let id = username.parse::<u32>().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<String>) -> Option<c_passwd> {
// 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<c_group> {
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<Vec<gid_t>, uint> {
return Err(os::errno());
}
let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t);
let mut groups : Vec<gid_t>= 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<c_passwd>, 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);
}

View file

@ -71,7 +71,7 @@ fn detect_algo(program: &str, matches: &getopts::Matches) -> (&'static str, Box<
"sha512sum" => ("SHA512", box Sha512::new() as Box<Digest>),
_ => {
{
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);

View file

@ -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<u8> = repeat(0).take(namelen).collect();
let err = unsafe {
gethostname (name.as_mut_ptr() as *mut libc::c_char,
namelen as libc::size_t)

View file

@ -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<String> {
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())
}
}
}

View file

@ -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<String>, 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<bool> = repeat(false).take(files.len()).collect();
loop {
let mut output = "".to_string();
let mut eof_count = 0;

View file

@ -151,7 +151,7 @@ fn shuf(input: Vec<String>, mode: Mode, repeat: bool, zero: bool, count: uint, o
enum WrappedRng {
RngFile(rand::reader::ReaderRng<io::File>),
RngDefault(rand::TaskRng),
RngDefault(rand::ThreadRng),
}
impl WrappedRng {
@ -170,7 +170,7 @@ fn shuf_lines(mut lines: Vec<String>, 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) };

View file

@ -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::<uint>() {
match strategy_param.as_slice().iter().map(|c| *c).collect::<String>().as_slice().parse::<uint>() {
Some(a) => a,
_ => crash!(1, "invalid number of bytes")
}

View file

@ -108,7 +108,7 @@ pub fn uumain(args: Vec<String>) -> 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
};

View file

@ -102,7 +102,7 @@ mod platform {
unsafe fn find_all_volumes() -> Vec<String> {
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,

View file

@ -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<T>(path: &Path, cb: || -> IoResult<T>) -> IoResult<T> {
fn with_path<F, T>(path: &Path, cb: F) -> IoResult<T> where F: Fn() -> IoResult<T> {
match cb() {
Err(f) => { warn(format!("{}: {}", path.display(), f.to_string()).as_slice()); Err(f) }
okay => okay

View file

@ -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<String>) -> 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()) {

View file

@ -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)
}
}

View file

@ -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);
}
}