mm(1): fixes creating files
This commit is contained in:
parent
e972ff468a
commit
3b76254599
42
src/mm.rs
42
src/mm.rs
@ -36,12 +36,12 @@ fn main() -> ExitCode {
|
|||||||
let argv = args().collect::<Vec<_>>();
|
let argv = args().collect::<Vec<_>>();
|
||||||
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
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 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 u = false; /* unbuffer i/o */
|
||||||
let mut ins = Vec::new(); /* initial inputs vector */
|
let mut ins = Vec::new(); /* initial input file path vector */
|
||||||
let mut outs = Vec::new(); /* initial outputs vector */
|
let mut outs = Vec::new(); /* initial output file path vector */
|
||||||
|
|
||||||
while let Some(opt) = argv.getopt("aei:o:tu") {
|
while let Some(opt) = argv.getopt("aei:o:tu") {
|
||||||
match opt.opt() {
|
match opt.opt() {
|
||||||
@ -74,29 +74,43 @@ fn main() -> ExitCode {
|
|||||||
let inputs = ins.iter().map(|file| {
|
let inputs = ins.iter().map(|file| {
|
||||||
/* if a file is “-”, it is stdin */
|
/* if a file is “-”, it is stdin */
|
||||||
if *file == "-" {
|
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,
|
Ok(f) => f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
||||||
exit(EX_IOERR);
|
exit(EX_IOERR);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
println!("{:?}", outs);
|
||||||
|
|
||||||
/* map all path strings to files */
|
/* map all path strings to files */
|
||||||
let mut outputs = outs.iter().map(|file| {
|
let mut outputs = outs.iter().map(|file| {
|
||||||
/* of a file is “-”, it is stdout */
|
/* of a file is “-”, it is stdout */
|
||||||
if *file == "-" {
|
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,
|
Ok(f) => return f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
||||||
exit(EX_IOERR);
|
exit(EX_IOERR);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -104,13 +118,16 @@ fn main() -> ExitCode {
|
|||||||
|
|
||||||
/* if -e is specified, use stderr */
|
/* if -e is specified, use stderr */
|
||||||
if e {
|
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| {
|
let mut outputs = outputs.iter().map(|o| {
|
||||||
if u {
|
if u {
|
||||||
|
/* unbuffered writing through a buffer of capacity 0 */
|
||||||
BufWriter::with_capacity(0, o)
|
BufWriter::with_capacity(0, o)
|
||||||
} else {
|
} else {
|
||||||
|
/* theoretically buffered writing */
|
||||||
BufWriter::new(o)
|
BufWriter::new(o)
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
@ -127,7 +144,8 @@ fn main() -> ExitCode {
|
|||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
return ExitCode::from(EX_IOERR as u8);
|
return ExitCode::from(EX_IOERR as u8);
|
||||||
}
|
}
|
||||||
if let Err(e) = out.flush() {
|
/* immediately flush the output for -u */
|
||||||
|
if let Err(e) = out.flush() {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
return ExitCode::from(EX_IOERR as u8);
|
return ExitCode::from(EX_IOERR as u8);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user