mirror of
https://github.com/uutils/coreutils
synced 2024-11-16 01:38:04 +00:00
commit
1f2b68251f
2 changed files with 22 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "link"]
|
#![crate_name = "link"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -12,8 +12,9 @@
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
|
||||||
use std::old_io::fs::link;
|
use std::io::Write;
|
||||||
use std::old_path::Path;
|
use std::fs::hard_link;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[path="../common/util.rs"]
|
#[path="../common/util.rs"]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -28,7 +29,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(err) => panic!("{}", err),
|
Err(err) => panic!("{}", err),
|
||||||
};
|
};
|
||||||
|
@ -44,17 +45,17 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {} [OPTIONS] FILE1 FILE2", NAME);
|
println!(" {} [OPTIONS] FILE1 FILE2", NAME);
|
||||||
println!("");
|
println!("");
|
||||||
print!("{}", getopts::usage("Create a link named FILE2 to FILE1.", opts.as_slice()).as_slice());
|
print!("{}", getopts::usage("Create a link named FILE2 to FILE1.", &opts));
|
||||||
if matches.free.len() != 2 {
|
if matches.free.len() != 2 {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let old = Path::new(matches.free[0].as_slice());
|
let old = Path::new(&matches.free[0]);
|
||||||
let new = Path::new(matches.free[1].as_slice());
|
let new = Path::new(&matches.free[1]);
|
||||||
|
|
||||||
match link(&old, &new) {
|
match hard_link(old, new) {
|
||||||
Ok(_) => 0,
|
Ok(_) => 0,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
show_error!("{}", err);
|
show_error!("{}", err);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "sum"]
|
#![crate_name = "sum"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -13,8 +13,9 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::old_io::{File, IoResult, print};
|
use std::fs::File;
|
||||||
use std::old_io::stdio::{stdin_raw};
|
use std::io::{Read, Result, stdin, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[path="../common/util.rs"]
|
#[path="../common/util.rs"]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -23,7 +24,7 @@ mod util;
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
static NAME: &'static str = "sum";
|
static NAME: &'static str = "sum";
|
||||||
|
|
||||||
fn bsd_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
fn bsd_sum(mut reader: Box<Read>) -> (usize, u16) {
|
||||||
let mut buf = [0; 1024];
|
let mut buf = [0; 1024];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut checksum: u16 = 0;
|
let mut checksum: u16 = 0;
|
||||||
|
@ -43,7 +44,7 @@ fn bsd_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
||||||
(blocks_read, checksum)
|
(blocks_read, checksum)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sysv_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
fn sysv_sum(mut reader: Box<Read>) -> (usize, u16) {
|
||||||
let mut buf = [0; 512];
|
let mut buf = [0; 512];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut ret = 0;
|
let mut ret = 0;
|
||||||
|
@ -66,18 +67,18 @@ fn sysv_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
||||||
(blocks_read, ret as u16)
|
(blocks_read, ret as u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(name: &str) -> IoResult<Box<Reader>> {
|
fn open(name: &str) -> Result<Box<Read>> {
|
||||||
match name {
|
match name {
|
||||||
"-" => Ok(Box::new(stdin_raw()) as Box<Reader>),
|
"-" => Ok(Box::new(stdin()) as Box<Read>),
|
||||||
_ => {
|
_ => {
|
||||||
let f = try!(File::open(&Path::new(name)));
|
let f = try!(File::open(&Path::new(name)));
|
||||||
Ok(Box::new(f) as Box<Reader>)
|
Ok(Box::new(f) as Box<Read>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let program = args[0].as_slice();
|
let program = &args[0];
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
|
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
|
||||||
getopts::optflag("s", "sysv", "use System V compatible algorithm"),
|
getopts::optflag("s", "sysv", "use System V compatible algorithm"),
|
||||||
|
@ -85,7 +86,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
getopts::optflag("v", "version", "print the version and exit"),
|
getopts::optflag("v", "version", "print the version 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)
|
||||||
};
|
};
|
||||||
|
@ -96,7 +97,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {0} [OPTION]... [FILE]...", program);
|
println!(" {0} [OPTION]... [FILE]...", program);
|
||||||
println!("");
|
println!("");
|
||||||
print(getopts::usage("checksum and count the blocks in a file", &opts).as_slice());
|
print!("{}", getopts::usage("checksum and count the blocks in a file", &opts));
|
||||||
println!("");
|
println!("");
|
||||||
println!("With no FILE, or when FILE is -, read standard input.");
|
println!("With no FILE, or when FILE is -, read standard input.");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -117,7 +118,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let print_names = sysv || files.len() > 1;
|
let print_names = sysv || files.len() > 1;
|
||||||
|
|
||||||
for file in files.iter() {
|
for file in files.iter() {
|
||||||
let reader = match open(file.as_slice()) {
|
let reader = match open(file) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
_ => crash!(1, "unable to open file")
|
_ => crash!(1, "unable to open file")
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue