From 3b7625459921aebd9b7c37b411fa523fe0bce1b2 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 14 Jul 2024 00:31:23 -0600 Subject: [PATCH] mm(1): fixes creating files --- src/mm.rs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index e991b5a..9c1151c 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -36,12 +36,12 @@ fn main() -> ExitCode { let argv = args().collect::>(); 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::>(); + 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::>(); @@ -127,7 +144,8 @@ fn main() -> ExitCode { eprintln!("{}: {}", argv[0], e.strerror()); 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()); return ExitCode::from(EX_IOERR as u8); }