mm(1): fixes creating files
This commit is contained in:
parent
e972ff468a
commit
3b76254599
40
src/mm.rs
40
src/mm.rs
@ -36,12 +36,12 @@ fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<_>>();
|
||||
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
||||
|
||||
let mut a = false; /* append rather than update */
|
||||
let mut a = false; /* append to the file */
|
||||
let mut e = false; /* use stderr as an output */
|
||||
let mut t = true;
|
||||
let mut t = true; /* do not truncate the file before writing */
|
||||
let mut u = false; /* unbuffer i/o */
|
||||
let mut ins = Vec::new(); /* initial inputs vector */
|
||||
let mut outs = Vec::new(); /* initial outputs vector */
|
||||
let mut ins = Vec::new(); /* initial input file path vector */
|
||||
let mut outs = Vec::new(); /* initial output file path vector */
|
||||
|
||||
while let Some(opt) = argv.getopt("aei:o:tu") {
|
||||
match opt.opt() {
|
||||
@ -74,29 +74,43 @@ fn main() -> ExitCode {
|
||||
let inputs = ins.iter().map(|file| {
|
||||
/* if a file is “-”, it is stdin */
|
||||
if *file == "-" {
|
||||
return unsafe { File::from_raw_fd(stdin().as_raw_fd()) }; /* fd0 = stdin */
|
||||
/* portable way to access stdin as a file */
|
||||
return unsafe { File::from_raw_fd(stdin().as_raw_fd()) };
|
||||
}
|
||||
|
||||
match File::options().open(file) {
|
||||
match File::open(file) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
||||
exit(EX_IOERR);
|
||||
},
|
||||
}
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
println!("{:?}", outs);
|
||||
|
||||
/* map all path strings to files */
|
||||
let mut outputs = outs.iter().map(|file| {
|
||||
/* of a file is “-”, it is stdout */
|
||||
if *file == "-" {
|
||||
return unsafe { File::from_raw_fd(stdout().as_raw_fd()) }; /* fd1 = stdout */
|
||||
/* portable way to access stdout as a file */
|
||||
return unsafe { File::from_raw_fd(stdout().as_raw_fd()) };
|
||||
}
|
||||
|
||||
match File::options().truncate(t).append(a).open(file) {
|
||||
let options = File::options()
|
||||
/* truncate if -t is specified and append if -a is specified */
|
||||
.truncate(t)
|
||||
.append(a)
|
||||
/* enable the ability to create and write to files */
|
||||
.create(true)
|
||||
.write(true)
|
||||
/* finally, open the file! */
|
||||
.open(file);
|
||||
|
||||
match options {
|
||||
Ok(f) => return f,
|
||||
Err(e) => {
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
||||
exit(EX_IOERR);
|
||||
},
|
||||
};
|
||||
@ -104,13 +118,16 @@ fn main() -> ExitCode {
|
||||
|
||||
/* if -e is specified, use stderr */
|
||||
if e {
|
||||
outputs.push(unsafe { File::from_raw_fd(stderr().as_raw_fd()) }); /* fd2 = stderr */
|
||||
/* portable way to access stderr as a file */
|
||||
outputs.push(unsafe { File::from_raw_fd(stderr().as_raw_fd()) });
|
||||
}
|
||||
|
||||
let mut outputs = outputs.iter().map(|o| {
|
||||
if u {
|
||||
/* unbuffered writing through a buffer of capacity 0 */
|
||||
BufWriter::with_capacity(0, o)
|
||||
} else {
|
||||
/* theoretically buffered writing */
|
||||
BufWriter::new(o)
|
||||
}
|
||||
}).collect::<Vec<_>>();
|
||||
@ -127,6 +144,7 @@ fn main() -> ExitCode {
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
return ExitCode::from(EX_IOERR as u8);
|
||||
}
|
||||
/* immediately flush the output for -u */
|
||||
if let Err(e) = out.flush() {
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
return ExitCode::from(EX_IOERR as u8);
|
||||
|
Loading…
Reference in New Issue
Block a user