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

Merge pull request #549 from kwantam/master

remove `#![feature()]` where possible in working utils ; fix `yes` and `uniq`
This commit is contained in:
Heather 2015-04-27 23:07:10 +03:00
commit 9d31fe83e6
10 changed files with 63 additions and 69 deletions

View file

@ -1,5 +1,5 @@
#![crate_name = "base64"] #![crate_name = "base64"]
#![feature(box_syntax, collections, rustc_private)] #![feature(box_syntax, rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -48,7 +48,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
optflag("h", "help", "display this help text and exit"), optflag("h", "help", "display this help text and exit"),
optflag("V", "version", "output version information and exit") optflag("V", "version", "output version information and exit")
]; ];
let matches = match getopts(args.tail(), &opts) { let matches = match getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
crash!(1, "error: {}", e); crash!(1, "error: {}", e);

View file

@ -1,5 +1,5 @@
#![crate_name = "basename"] #![crate_name = "basename"]
#![feature(collections, rustc_private)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -34,7 +34,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("V", "version", "output version information and exit"), getopts::optflag("V", "version", "output version information and exit"),
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!(1, "Invalid options\n{}", f) Err(f) => crash!(1, "Invalid options\n{}", f)
}; };

View file

@ -1,7 +1,5 @@
#![crate_name = "cat"] #![crate_name = "cat"]
#![feature(collections, rustc_private)] #![feature(rustc_private, box_syntax, unsafe_destructor)]
#![feature(box_syntax, unsafe_destructor)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -42,7 +40,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"), getopts::optflag("V", "version", "output version information and exit"),
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => panic!("Invalid options\n{}", f) Err(f) => panic!("Invalid options\n{}", f)
}; };

View file

@ -1,5 +1,5 @@
#![crate_name = "chroot"] #![crate_name = "chroot"]
#![feature(collections, rustc_private, path_ext)] #![feature(rustc_private, path_ext)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -57,7 +57,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
optflag("V", "version", "Show program's version") optflag("V", "version", "Show program's version")
]; ];
let opts = match getopts(args.tail(), &options) { let opts = match getopts(&args[1..], &options) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
show_error!("{}", f); show_error!("{}", f);
@ -195,7 +195,7 @@ fn set_groups_from_str(groups: &str) {
fn set_user(user: &str) { fn set_user(user: &str) {
if !user.is_empty() { if !user.is_empty() {
let user_id = get_pw_from_args(&vec!(String::from_str(user))).unwrap().pw_uid; let user_id = get_pw_from_args(&vec!(user.to_string())).unwrap().pw_uid;
let err = unsafe { setuid(user_id as libc::uid_t) }; let err = unsafe { setuid(user_id as libc::uid_t) };
if err != 0 { if err != 0 {
crash!(1, "cannot set user to {}: {}", user, Error::last_os_error()) crash!(1, "cannot set user to {}: {}", user, Error::last_os_error())

View file

@ -1,5 +1,5 @@
#![crate_name = "echo"] #![crate_name = "echo"]
#![feature(rustc_private, str_char)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -94,7 +94,7 @@ fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<Str
"-e" => options.escape = true, "-e" => options.escape = true,
"-E" => options.escape = false, "-E" => options.escape = false,
_ => { _ => {
if arg.len() > 1 && arg.char_at(0) == '-' { if arg.len() > 1 && arg.chars().next().unwrap_or('_') == '-' {
let mut newopts = options.clone(); let mut newopts = options.clone();
for ch in arg.chars().skip(1) { for ch in arg.chars().skip(1) {
match ch { match ch {

View file

@ -1,4 +1,5 @@
#![crate_name = "fmt"] #![crate_name = "fmt"]
#![feature(box_syntax,rustc_private,str_char,unicode,core)]
/* /*
* This file is part of `fmt` from the uutils coreutils package. * This file is part of `fmt` from the uutils coreutils package.
@ -9,9 +10,6 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
#![feature(box_syntax,core,rustc_private,collections,str_char,unicode)]
extern crate core;
extern crate getopts; extern crate getopts;
extern crate rustc_unicode; extern crate rustc_unicode;
extern crate unicode_width; extern crate unicode_width;
@ -87,7 +85,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("h", "help", "Display this help message and exit.") getopts::optflag("h", "help", "Display this help message and exit.")
]; ];
let matches = match getopts::getopts(args.tail(), &opts[..]) { let matches = match getopts::getopts(&args[1..], &opts[..]) {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!(1, "{}\nTry `{} --help' for more information.", f, args[0]) Err(f) => crash!(1, "{}\nTry `{} --help' for more information.", f, args[0])
}; };

View file

@ -7,7 +7,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use core::iter::Peekable; use std::iter::Peekable;
use std::io::{BufRead, Lines}; use std::io::{BufRead, Lines};
use std::slice::Iter; use std::slice::Iter;
use std::str::CharRange; use std::str::CharRange;

View file

@ -1,5 +1,5 @@
#![crate_name = "sync"] #![crate_name = "sync"]
#![feature(collections, rustc_private)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -148,7 +148,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
optflag("V", "version", "output version information and exit") optflag("V", "version", "output version information and exit")
]; ];
let matches = match getopts(args.tail(), &options) { let matches = match getopts(&args[1..], &options) {
Ok(m) => { m } Ok(m) => { m }
_ => { help(program, &options); return 1 } _ => { help(program, &options); return 1 }
}; };

View file

@ -1,5 +1,5 @@
#![crate_name = "uniq"] #![crate_name = "uniq"]
#![feature(collections, core, old_io, old_path, 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.
@ -13,10 +13,11 @@
extern crate getopts; extern crate getopts;
use std::ascii::OwnedAsciiExt;
use std::cmp::min; use std::cmp::min;
use std::str::FromStr; use std::str::FromStr;
use std::old_io as io; use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::path::Path;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
#[macro_use] #[macro_use]
@ -37,10 +38,10 @@ struct Uniq {
} }
impl Uniq { impl Uniq {
pub fn print_uniq<R: Reader, W: Writer>(&self, reader: &mut io::BufferedReader<R>, writer: &mut io::BufferedWriter<W>) { pub fn print_uniq<R: Read, W: Write>(&self, reader: &mut BufReader<R>, writer: &mut BufWriter<W>) {
let mut lines: Vec<String> = vec!(); let mut lines: Vec<String> = vec!();
let mut first_line_printed = false; let mut first_line_printed = false;
let delimiters = self.delimiters.as_slice(); let delimiters = &self.delimiters[..];
for io_line in reader.lines() { for io_line in reader.lines() {
let line = crash_if_err!(1, io_line); let line = crash_if_err!(1, io_line);
@ -68,18 +69,18 @@ impl Uniq {
Some(i) => min(slice_start + i, len), Some(i) => min(slice_start + i, len),
None => len None => len
}; };
let sliced = line.as_slice()[slice_start..slice_stop].to_string();
if self.ignore_case { line[slice_start..slice_stop].chars()
sliced.into_ascii_uppercase() .map(|c| match c {
} else { 'a' ... 'z' if self.ignore_case => ((c as u8) - 32) as char,
sliced _ => c,
} }).collect()
} else { } else {
line.clone() line.clone()
} }
} }
fn print_lines<W: Writer>(&self, writer: &mut io::BufferedWriter<W>, lines: &Vec<String>, print_delimiter: bool) -> bool { fn print_lines<W: Write>(&self, writer: &mut BufWriter<W>, lines: &Vec<String>, print_delimiter: bool) -> bool {
let mut first_line_printed = false; let mut first_line_printed = false;
let mut count = if self.all_repeated { 1 } else { lines.len() }; let mut count = if self.all_repeated { 1 } else { lines.len() };
if lines.len() == 1 && !self.repeats_only if lines.len() == 1 && !self.repeats_only
@ -89,7 +90,7 @@ impl Uniq {
count += 1; count += 1;
} }
if self.all_repeated { if self.all_repeated {
for line in lines.tail().iter() { for line in lines[1..].iter() {
self.print_line(writer, line, count, print_delimiter && !first_line_printed); self.print_line(writer, line, count, print_delimiter && !first_line_printed);
first_line_printed = true; first_line_printed = true;
count += 1; count += 1;
@ -98,16 +99,17 @@ impl Uniq {
first_line_printed first_line_printed
} }
fn print_line<W: Writer>(&self, writer: &mut io::BufferedWriter<W>, line: &String, count: usize, print_delimiter: bool) { fn print_line<W: Write>(&self, writer: &mut BufWriter<W>, line: &String, count: usize, print_delimiter: bool) {
let output_line = if self.show_counts {
format!("{:7} {}", count, line)
} else {
line.clone()
};
if print_delimiter { if print_delimiter {
crash_if_err!(1, writer.write_line("")); crash_if_err!(1, writer.write_all(&['\n' as u8]));
} }
crash_if_err!(1, writer.write_str(output_line.as_slice()));
crash_if_err!(1, if self.show_counts {
writer.write_all(format!("{:7} {}", count, line).as_bytes())
} else {
writer.write_all(line.as_bytes())
});
crash_if_err!(1, writer.write_all("\n".as_bytes()));
} }
} }
@ -120,9 +122,6 @@ fn opt_parsed<T: FromStr>(opt_name: &str, matches: &getopts::Matches) -> Option<
} }
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let program_path = Path::new(args[0].clone());
let program = program_path.filename_str().unwrap_or(NAME);
let opts = [ let opts = [
getopts::optflag("c", "count", "prefix lines by the number of occurrences"), getopts::optflag("c", "count", "prefix lines by the number of occurrences"),
getopts::optflag("d", "repeated", "only print duplicate lines"), getopts::optflag("d", "repeated", "only print duplicate lines"),
@ -139,7 +138,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit") getopts::optflag("V", "version", "output version information and exit")
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!(1, "{}", f) Err(f) => crash!(1, "{}", f)
}; };
@ -148,13 +147,13 @@ pub fn uumain(args: Vec<String>) -> i32 {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
println!(""); println!("");
println!("Usage:"); println!("Usage:");
println!(" {0} [OPTION]... [FILE]...", program); println!(" {0} [OPTION]... [FILE]...", args[0]);
println!(""); println!("");
print!("{}", getopts::usage("Filter adjacent matching lines from INPUT (or standard input),\n\ print!("{}", getopts::usage("Filter adjacent matching lines from INPUT (or standard input),\n\
writing to OUTPUT (or standard output).", &opts)); writing to OUTPUT (or standard output).", &opts));
println!(""); println!("");
println!("Note: '{0}' does not detect repeated lines unless they are adjacent.\n\ println!("Note: '{0}' does not detect repeated lines unless they are adjacent.\n\
You may want to sort the input first, or use 'sort -u' without '{0}'.\n", program); You may want to sort the input first, or use 'sort -u' without '{0}'.\n", args[0]);
} else if matches.opt_present("version") { } else if matches.opt_present("version") {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
} else { } else {
@ -171,7 +170,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
uniques_only: matches.opt_present("unique"), uniques_only: matches.opt_present("unique"),
all_repeated: matches.opt_present("all-repeated"), all_repeated: matches.opt_present("all-repeated"),
delimiters: match matches.opt_default("all-repeated", "none") { delimiters: match matches.opt_default("all-repeated", "none") {
Some(ref opt_arg) if opt_arg.as_slice() != "none" => { Some(ref opt_arg) if opt_arg != "none" => {
let rep_args = ["prepend".to_string(), "separate".to_string()]; let rep_args = ["prepend".to_string(), "separate".to_string()];
if !rep_args.contains(opt_arg) { if !rep_args.contains(opt_arg) {
crash!(1, "Incorrect argument for all-repeated: {}", opt_arg.clone()); crash!(1, "Incorrect argument for all-repeated: {}", opt_arg.clone());
@ -191,26 +190,26 @@ pub fn uumain(args: Vec<String>) -> i32 {
0 0
} }
fn open_input_file(in_file_name: String) -> io::BufferedReader<Box<Reader+'static>> { fn open_input_file(in_file_name: String) -> BufReader<Box<Read+'static>> {
let in_file = if in_file_name.as_slice() == "-" { let in_file = if in_file_name == "-" {
Box::new(io::stdio::stdin_raw()) as Box<Reader> Box::new(stdin()) as Box<Read>
} else { } else {
let path = Path::new(in_file_name); let path = Path::new(&in_file_name[..]);
let in_file = io::File::open(&path); let in_file = File::open(&path);
let r = crash_if_err!(1, in_file); let r = crash_if_err!(1, in_file);
Box::new(r) as Box<Reader> Box::new(r) as Box<Read>
}; };
io::BufferedReader::new(in_file) BufReader::new(in_file)
} }
fn open_output_file(out_file_name: String) -> io::BufferedWriter<Box<Writer+'static>> { fn open_output_file(out_file_name: String) -> BufWriter<Box<Write+'static>> {
let out_file = if out_file_name.as_slice() == "-" { let out_file = if out_file_name == "-" {
Box::new(io::stdio::stdout_raw()) as Box<Writer> Box::new(stdout()) as Box<Write>
} else { } else {
let path = Path::new(out_file_name); let path = Path::new(&out_file_name[..]);
let in_file = io::File::create(&path); let in_file = File::create(&path);
let w = crash_if_err!(1, in_file); let w = crash_if_err!(1, in_file);
Box::new(w) as Box<Writer> Box::new(w) as Box<Write>
}; };
io::BufferedWriter::new(out_file) BufWriter::new(out_file)
} }

View file

@ -1,5 +1,5 @@
#![crate_name = "yes"] #![crate_name = "yes"]
#![feature(collections, old_io, rustc_private)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -15,8 +15,7 @@
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
use std::old_io::print; use std::io::Write;
use std::borrow::IntoCow;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
#[macro_use] #[macro_use]
@ -30,7 +29,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"), getopts::optflag("V", "version", "output version information and exit"),
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
crash!(1, "invalid options\n{}", f) crash!(1, "invalid options\n{}", f)
@ -42,7 +41,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
println!("Usage:"); println!("Usage:");
println!(" {0} [STRING]... [OPTION]...", program); println!(" {0} [STRING]... [OPTION]...", program);
println!(""); println!("");
print(&getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", &opts)[..]); print!("{}", getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", &opts));
return 0; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
@ -50,9 +49,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
return 0; return 0;
} }
let string = if matches.free.is_empty() { let string = if matches.free.is_empty() {
"y".into_cow() "y".to_string()
} else { } else {
matches.free.connect(" ").into_cow() matches.free.connect(" ")
}; };
exec(&string[..]); exec(&string[..]);