From dd39aeff02bfdc71293906f2464181413f461247 Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 22 Aug 2024 00:22:05 -0600 Subject: [PATCH 01/44] mm(1): adds ioerr(), usage(), and oser() functions --- src/mm.rs | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index 10c6ef2..025b0f6 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -20,9 +20,9 @@ use std::{ env::args, fs::File, - io::{ stdin, stdout, stderr, BufWriter, Read, Write }, + io::{ Error, BufWriter, Read, Write, stderr, stdin, stdout }, os::fd::{ AsRawFd, FromRawFd }, - process::{ exit, ExitCode }, + process::{ ExitCode, exit}, }; extern crate getopt; @@ -47,15 +47,29 @@ use ArgMode::*; enum ArgMode { In, Out } +fn ioerr(argv0: &String, e: Error) -> ExitCode { + eprintln!("{}: {}", argv0, e.strerror()); + ExitCode::from(EX_IOERR as u8) +} + +#[cfg(target_os="openbsd")] +fn oserr(argv0: &String, e: Error) -> ExitCode { + eprintln!("{}: {}", argv0, e.strerror()); + ExitCode::from(EX_OSERR as u8) +} + +fn usage(argv0: &String) -> ExitCode { + eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0); + ExitCode::from(EX_USAGE as u8) +} + fn main() -> ExitCode { let argv = args().collect::>(); - let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]); #[cfg(target_os="openbsd")] { let promises = Promises::new("cpath rpath stdio unveil wpath"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } @@ -85,8 +99,7 @@ fn main() -> ExitCode { mode = Some(Out); /* latest argument == -o */ }, Err(_) | Ok(_) => { - eprintln!("{}", usage); - return ExitCode::from(EX_USAGE as u8); + return usage(&argv[0]); }, }; @@ -111,8 +124,7 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['r']); if let Err(e) = unveil(Some(&input), Some(perms)) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } @@ -120,20 +132,17 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['c', 'w']); if let Err(e) = unveil(Some(&output), Some(perms)) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } if let Err(e) = unveil(None, None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } if ins.is_empty() && outs.is_empty() && argv.len() > optind { - eprintln!("Usage: {}", usage); - return ExitCode::from(EX_USAGE as u8); + return usage(&argv[0]); } /* use stdin if no inputs are specified */ @@ -153,7 +162,7 @@ fn main() -> ExitCode { match File::open(file) { Ok(f) => f, Err(e) => { - eprintln!("{}: {}: {}", argv[0], file, e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }, } @@ -180,7 +189,7 @@ fn main() -> ExitCode { match options { Ok(f) => return f, Err(e) => { - eprintln!("{}: {}: {}", argv[0], file, e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }, }; @@ -205,21 +214,19 @@ fn main() -> ExitCode { for file in inputs { for byte in file.bytes().map(|b| { b.unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }) }) { for out in &mut outputs { if let Err(e) = out.write(&[byte]) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_IOERR as u8); + return ioerr(&argv[0], e); } if u { /* immediately flush the output for -u */ if let Err(e) = out.flush() { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_IOERR as u8); + return ioerr(&argv[0], e); } } } -- 2.46.1 From a14b8fb9d705c6fccbcd469404db8b41a48f4265 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Aug 2024 13:35:30 -0600 Subject: [PATCH 02/44] fop(1): adds functions for error handling --- src/fop.rs | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 7c8424e..16977e8 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -18,8 +18,8 @@ use std::{ env::args, - io::{ Read, stdin, stdout, Write }, - process::{ Command, exit, Stdio }, + io::{ Error, Read, Write, stdin, stdout }, + process::{ Command, ExitCode, Stdio, exit }, }; extern crate getopt; @@ -34,7 +34,16 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; -fn main() { +fn err(argv0: &String, e: Error) { + eprintln!("{}: {}", argv0, e.strerror()); +} + +fn usage(argv0: &String) -> i32 { + eprintln!("Usage: {} [-d delimiter] index command [args...]", argv0); + EX_USAGE +} + +fn main() -> ExitCode { let argv = args().collect::>(); let mut d = '\u{1E}'.to_string(); /* ASCII record separator */ let mut optind = 1; @@ -42,16 +51,11 @@ fn main() { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio proc exec"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); + err(&argv[0], e); exit(EX_OSERR); } } - let usage = format!( - "Usage: {} [-d delimiter] index command [args...]", - argv[0], - ); - while let Some(opt) = argv.getopt("d:") { match opt.opt() { Ok("d") => { @@ -60,8 +64,7 @@ fn main() { optind = opt.ind(); }, _ => { - eprintln!("{}", usage); - exit(EX_USAGE); + return ExitCode::from(usage(&argv[0]) as u8); } }; } @@ -77,14 +80,13 @@ fn main() { /* argv[0] of the operator command */ let operator = argv.get(command_arg).unwrap_or_else(|| { - eprintln!("{}", usage); - exit(EX_USAGE); + exit(usage(&argv[0])); }); /* read entire standard input into memory */ let mut buf = String::new(); if let Err(e) = stdin().read_to_string(&mut buf) { - eprintln!("{}: {}", argv[0], e.strerror()); + err(&argv[0], e); exit(EX_IOERR); }; @@ -105,17 +107,13 @@ fn main() { .stdout(Stdio::piped()) /* piped stdout to handle output ourselves */ .spawn() .unwrap_or_else( |e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror()); + err(&argv[0], e); exit(EX_UNAVAILABLE); }); /* get field we want to pipe into spawned program */ let field = fields.get(index).unwrap_or_else(|| { - eprintln!( - "{}: {}: No such index in input", - argv[0], - index.to_string(), - ); + eprintln!("{}: {}: no such index in input", argv[0], index); exit(EX_DATAERR); }); @@ -126,7 +124,7 @@ fn main() { } let output = spawned.wait_with_output().unwrap_or_else(|e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror()); + err(&argv[0], e); exit(EX_IOERR); }); @@ -143,7 +141,7 @@ fn main() { /* convert the output of the program to UTF-8 */ let new_field = String::from_utf8(replace).unwrap_or_else(|e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], e); + eprintln!("{}: {}", argv[0], e); exit(EX_IOERR); }); @@ -154,7 +152,9 @@ fn main() { stdout().write_all( fields.join(&d.to_string()).as_bytes() ).unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], e.strerror()); + err(&argv[0], e); exit(EX_IOERR); }); + + ExitCode::SUCCESS } -- 2.46.1 From 1fffd3df529bca6da41dd5387cff62577bdea8b3 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Aug 2024 14:21:43 -0600 Subject: [PATCH 03/44] Makefile: sets bindgen to output exit codes as u8 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5de9618..5b7d4fa 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ build/o/libstrerror.rlib: build src/libstrerror.rs src/libstrerror.rs build/o/libsysexits.rlib: build/include/sysexits.h - bindgen --default-macro-constant-type signed --use-core --formatter=none \ + bindgen --fit-macro-constant-types --default-macro-constant-type unsigned --use-core --formatter=none \ build/include/sysexits.h | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ - # bandage solution until bindgen(1) gets stdin support -- 2.46.1 From 928dce0234af0068769a4d7dee42d42ecf6427ca Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Aug 2024 14:22:11 -0600 Subject: [PATCH 04/44] fop(1): uses new sysexits --- src/fop.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 16977e8..6bb778c 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -38,7 +38,7 @@ fn err(argv0: &String, e: Error) { eprintln!("{}: {}", argv0, e.strerror()); } -fn usage(argv0: &String) -> i32 { +fn usage(argv0: &String) -> u8 { eprintln!("Usage: {} [-d delimiter] index command [args...]", argv0); EX_USAGE } @@ -52,7 +52,7 @@ fn main() -> ExitCode { let promises = Promises::new("stdio proc exec"); if let Err(e) = pledge(Some(promises), None) { err(&argv[0], e); - exit(EX_OSERR); + return ExitCode::from(EX_OSERR); } } @@ -64,7 +64,7 @@ fn main() -> ExitCode { optind = opt.ind(); }, _ => { - return ExitCode::from(usage(&argv[0]) as u8); + return ExitCode::from(usage(&argv[0])); } }; } @@ -72,7 +72,7 @@ fn main() -> ExitCode { /* parse the specified index as a number we can use */ let index = argv[optind].parse::().unwrap_or_else(|e| { eprintln!("{}: {}: {}", argv[0], argv[1], e); - exit(EX_DATAERR); + exit(EX_DATAERR as i32); }); /* index of the argv[0] for the operator command */ @@ -80,14 +80,14 @@ fn main() -> ExitCode { /* argv[0] of the operator command */ let operator = argv.get(command_arg).unwrap_or_else(|| { - exit(usage(&argv[0])); + exit(usage(&argv[0]) as i32); }); /* read entire standard input into memory */ let mut buf = String::new(); if let Err(e) = stdin().read_to_string(&mut buf) { err(&argv[0], e); - exit(EX_IOERR); + exit(EX_IOERR as i32); }; /* split the buffer by the delimiter (by default, '\u{1E}') */ @@ -108,13 +108,13 @@ fn main() -> ExitCode { .spawn() .unwrap_or_else( |e| { err(&argv[0], e); - exit(EX_UNAVAILABLE); + exit(EX_UNAVAILABLE as i32); }); /* get field we want to pipe into spawned program */ let field = fields.get(index).unwrap_or_else(|| { eprintln!("{}: {}: no such index in input", argv[0], index); - exit(EX_DATAERR); + exit(EX_DATAERR as i32); }); /* get the stdin of the newly spawned program and feed it the field val */ @@ -125,7 +125,7 @@ fn main() -> ExitCode { let output = spawned.wait_with_output().unwrap_or_else(|e| { err(&argv[0], e); - exit(EX_IOERR); + exit(EX_IOERR as i32); }); /* get the output with which the original field will be replaced */ @@ -142,7 +142,7 @@ fn main() -> ExitCode { /* convert the output of the program to UTF-8 */ let new_field = String::from_utf8(replace).unwrap_or_else(|e| { eprintln!("{}: {}", argv[0], e); - exit(EX_IOERR); + exit(EX_IOERR as i32); }); /* store the new field in the old fields vector */ @@ -153,7 +153,7 @@ fn main() -> ExitCode { fields.join(&d.to_string()).as_bytes() ).unwrap_or_else(|e| { err(&argv[0], e); - exit(EX_IOERR); + exit(EX_IOERR as i32); }); ExitCode::SUCCESS -- 2.46.1 From 4db4160019cc81e97b5f4e6dc36d31efd92af914 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Aug 2024 14:23:11 -0600 Subject: [PATCH 05/44] swab(1): uses new sysexits --- src/swab.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/swab.rs b/src/swab.rs index 4dc86de..6afed28 100644 --- a/src/swab.rs +++ b/src/swab.rs @@ -38,17 +38,17 @@ use strerror::StrError; fn oserr(argv0: &str, e: Error) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_OSERR as u8) + ExitCode::from(EX_OSERR) } fn ioerr(argv0: &str, e: Error) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_IOERR as u8) + ExitCode::from(EX_IOERR) } fn usage(s: &str) -> ExitCode { eprintln!("Usage: {} [-w word_size]", s); - ExitCode::from(EX_USAGE as u8) + ExitCode::from(EX_USAGE) } fn main() -> ExitCode { @@ -88,7 +88,7 @@ fn main() -> ExitCode { loop { match input.read(&mut buf) { - Ok(0) => break ExitCode::from(EX_OK as u8), // read nothing; bye + Ok(0) => break ExitCode::from(EX_OK), // read nothing; bye Ok(v) if v == wordsize => { // read full block; swab let (left, right) = buf.split_at(v/2); -- 2.46.1 From 722679247ab0f7b1a4ed6f3e40b6eea084a411f3 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Aug 2024 14:31:02 -0600 Subject: [PATCH 06/44] hru(1): uses new sysexits --- src/hru.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index e07ec39..70981a3 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -90,7 +90,7 @@ fn main() -> ExitCode { let promises = Promises::new("stdio"); if let Err(e) = pledge(Some(promises), None) { eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return ExitCode::from(EX_OSERR); } } @@ -104,30 +104,26 @@ fn main() -> ExitCode { buf.clear(); f }, - Err(err) => { - eprintln!("{}: {}", argv[0], err); - return ExitCode::from(EX_DATAERR as u8); + Err(e) => { + eprintln!("{}: {}", argv[0], e); + return ExitCode::from(EX_DATAERR); }, }; - let (number, prefix) = match convert(n) { - Ok(x) => x, - Err(err) => { - eprintln!("{}: {}", argv[0], err); - return ExitCode::from(EX_SOFTWARE as u8); - }, - }; + let (number, prefix) = convert(n).unwrap_or_else(|e| { + eprintln!("{}: {}", argv[0], e); + exit(EX_SOFTWARE.into()); + }); let si_prefix = format!("{}B", prefix.1); /* round output number to one decimal place */ let out = ((number * 10.0).round() / 10.0).to_string(); - stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) - .unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], e.strerror()); - exit(EX_IOERR); - }); + if let Err(e) = stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) { + eprintln!("{}: {}", argv[0], e.strerror()); + return ExitCode::from(EX_IOERR); + } } ExitCode::SUCCESS -- 2.46.1 From f104598164591d0d462d8c3dc1e5d07408837b63 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 15:53:58 -0600 Subject: [PATCH 07/44] mm(1): updates error-handling functions and uncovers issue with error reporting --- src/mm.rs | 36 +++++++++++++++--------------------- tests/bonsai/mm.mk | 8 +++++++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index 025b0f6..80a9cd7 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -47,20 +47,14 @@ use ArgMode::*; enum ArgMode { In, Out } -fn ioerr(argv0: &String, e: Error) -> ExitCode { +fn err(argv0: &String, e: Error, code: Option) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_IOERR as u8) -} - -#[cfg(target_os="openbsd")] -fn oserr(argv0: &String, e: Error) -> ExitCode { - eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_OSERR as u8) + ExitCode::from(code.unwrap_or(1 /* unknown error */)) } fn usage(argv0: &String) -> ExitCode { eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0); - ExitCode::from(EX_USAGE as u8) + ExitCode::from(EX_USAGE) } fn main() -> ExitCode { @@ -69,7 +63,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("cpath rpath stdio unveil wpath"); if let Err(e) = pledge(Some(promises), None) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -124,7 +118,7 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['r']); if let Err(e) = unveil(Some(&input), Some(perms)) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -132,12 +126,12 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['c', 'w']); if let Err(e) = unveil(Some(&output), Some(perms)) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } if let Err(e) = unveil(None, None) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -162,8 +156,8 @@ fn main() -> ExitCode { match File::open(file) { Ok(f) => f, Err(e) => { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&(argv[0].clone() + ": " + file), e, None); + exit(EX_IOERR.into()); }, } }).collect::>(); @@ -189,8 +183,8 @@ fn main() -> ExitCode { match options { Ok(f) => return f, Err(e) => { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&(argv[0].clone() + ": " + file), e, None); + exit(EX_IOERR.into()); }, }; }).collect::>(); @@ -214,19 +208,19 @@ fn main() -> ExitCode { for file in inputs { for byte in file.bytes().map(|b| { b.unwrap_or_else(|e| { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&argv[0], e, None); + exit(EX_IOERR.into()); }) }) { for out in &mut outputs { if let Err(e) = out.write(&[byte]) { - return ioerr(&argv[0], e); + return err(&argv[0], e, Some(EX_IOERR)); } if u { /* immediately flush the output for -u */ if let Err(e) = out.flush() { - return ioerr(&argv[0], e); + return err(&argv[0], e, Some(EX_IOERR)); } } } diff --git a/tests/bonsai/mm.mk b/tests/bonsai/mm.mk index 124d284..891dae3 100755 --- a/tests/bonsai/mm.mk +++ b/tests/bonsai/mm.mk @@ -6,7 +6,7 @@ # notice are preserved. This file is offered as-is, without any warranty. .PHONY: mm_tests -mm_tests: mm_args mm_help mm_stderr mm_remaining +mm_tests: mm_args mm_help mm_stderr mm_remaining mm_remaining_options .PHONY: mm_none mm_none: $(BIN)/mm @@ -32,3 +32,9 @@ mm_remaining: $(BIN)/mm test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)" $(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1 diff /tmp/mm_test0 /tmp/mm_test1 + +.PHONY: mm_remaining_options +# check to make sure mm -i with trailing arguments interprets -o as one +mm_remaining_options: + ! $(BIN)/mm -i README COPYING -o - 2>&1 | cut -d: -f2 \ + | xargs test " -o" = -- 2.46.1 From 150fa22f35a55f72cbb342c530efe498e73e0d56 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 15:57:55 -0600 Subject: [PATCH 08/44] fop(1): changes casts to calls to .into() --- src/fop.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 6bb778c..a58f759 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -72,7 +72,7 @@ fn main() -> ExitCode { /* parse the specified index as a number we can use */ let index = argv[optind].parse::().unwrap_or_else(|e| { eprintln!("{}: {}: {}", argv[0], argv[1], e); - exit(EX_DATAERR as i32); + exit(EX_DATAERR.into()); }); /* index of the argv[0] for the operator command */ @@ -80,14 +80,14 @@ fn main() -> ExitCode { /* argv[0] of the operator command */ let operator = argv.get(command_arg).unwrap_or_else(|| { - exit(usage(&argv[0]) as i32); + exit(usage(&argv[0]).into()); }); /* read entire standard input into memory */ let mut buf = String::new(); if let Err(e) = stdin().read_to_string(&mut buf) { err(&argv[0], e); - exit(EX_IOERR as i32); + exit(EX_IOERR.into()); }; /* split the buffer by the delimiter (by default, '\u{1E}') */ @@ -108,13 +108,13 @@ fn main() -> ExitCode { .spawn() .unwrap_or_else( |e| { err(&argv[0], e); - exit(EX_UNAVAILABLE as i32); + exit(EX_UNAVAILABLE.into()); }); /* get field we want to pipe into spawned program */ let field = fields.get(index).unwrap_or_else(|| { eprintln!("{}: {}: no such index in input", argv[0], index); - exit(EX_DATAERR as i32); + exit(EX_DATAERR.into()); }); /* get the stdin of the newly spawned program and feed it the field val */ @@ -125,7 +125,7 @@ fn main() -> ExitCode { let output = spawned.wait_with_output().unwrap_or_else(|e| { err(&argv[0], e); - exit(EX_IOERR as i32); + exit(EX_IOERR.into()); }); /* get the output with which the original field will be replaced */ @@ -142,7 +142,7 @@ fn main() -> ExitCode { /* convert the output of the program to UTF-8 */ let new_field = String::from_utf8(replace).unwrap_or_else(|e| { eprintln!("{}: {}", argv[0], e); - exit(EX_IOERR as i32); + exit(EX_IOERR.into()); }); /* store the new field in the old fields vector */ @@ -153,7 +153,7 @@ fn main() -> ExitCode { fields.join(&d.to_string()).as_bytes() ).unwrap_or_else(|e| { err(&argv[0], e); - exit(EX_IOERR as i32); + exit(EX_IOERR.into()); }); ExitCode::SUCCESS -- 2.46.1 From a0138be79ebf7d174acbdca99afa4949babf6b47 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 17:22:02 -0600 Subject: [PATCH 09/44] rpn(1): refactor to make code more efficient, readable, and maintainable --- src/rpn.rs | 103 +++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 7669efb..ff0b1bd 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -54,7 +54,7 @@ use CalcType::*; extern crate sysexits; -use sysexits::EX_DATAERR; +use sysexits::{ EX_DATAERR, EX_IOERR }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate strerror; @@ -120,13 +120,18 @@ impl Display for CalcType { #[derive(Debug, Clone)] struct EvaluationError { message: String, - code: i32, + code: ExitCode, } /* I’m no math nerd but I want the highest possible approximation of 0.9 * repeating and it seems this can give it to me */ const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0; +fn err(argv0: &String, e: std::io::Error, code: Option) -> ExitCode { + eprintln!("{}: {}", argv0, e.strerror()); + ExitCode::from(code.unwrap_or(1 /* unknown error */)) +} + fn eval( input: &str, initial_stack: VecDeque, @@ -153,12 +158,12 @@ fn eval( Invalid(i) => { return Err(EvaluationError { message: format!("{}: Invalid token", i), - code: EX_DATAERR, + code: ExitCode::from(EX_DATAERR), }) }, op => { ops.push_back(op.clone()); - oper = true; + oper = true; /* this is an operation */ let vals = ( stack.pop_back(), @@ -179,7 +184,7 @@ fn eval( } else { return Err(EvaluationError { message: format!("{}: Unexpected operation", op), - code: EX_DATAERR, + code: ExitCode::from(EX_DATAERR), }) } }, @@ -190,51 +195,41 @@ fn eval( } /* Round a float to the given precision level */ -fn round_precise(value: &f64, precision: usize) -> f64 { +fn round_precise(value: &f64) -> f64 { + /* Set floating-point precision for correcting rounding errors based on + * machine epsilon */ + let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as i32; + let multiplier = 10_f64.powi(precision as i32); (value * multiplier).round() / multiplier } +fn unstack(stack: VecDeque, op: bool) -> bool { + if let Some(val) = stack.iter().last() { + if !op { return true; } + + println!("{}", round_precise(val).to_string()); + } else { + return false; + } + + return true; +} + fn main() -> ExitCode { let argv = args().collect::>(); #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return err(&argv[0], e, Some(EX_OSERR)); } } let mut stack = VecDeque::new(); let mut buf = String::new(); - /* Set floating-point precision for correcting rounding errors based on - * machine epsilon */ - let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as usize; - - if argv.get(1).is_none() { /* read from stdin */ - while let Ok(_) = stdin().read_line(&mut buf) { - match eval(&buf.trim(), stack) { - Ok(s) => { - buf.clear(); - stack = s.0.clone(); - let val = match stack.iter().last() { - Some(v) => v, - None => break, - }; - - if s.1 == false { continue; } - - println!("{}", round_precise(val, precision).to_string()); - }, - Err(err) => { - eprintln!("{}: {}", argv[0], err.message); - return ExitCode::from(err.code as u8); - }, - }; - } - } else { /* read from argv */ + if argv.get(1).is_some() { /* read expressions from argv */ /* join argv into an owned String joined by spaces minus argv[0] */ let input = argv .iter() @@ -245,20 +240,36 @@ fn main() -> ExitCode { match eval(&input, stack) { Ok(s) => { - stack = s.0.clone(); - - let val = match stack.iter().last() { - Some(v) => v, - None => return ExitCode::SUCCESS, - }; - - println!("{}", round_precise(val, precision).to_string()) - }, - Err(err) => { - eprintln!("{}: {}", argv[0], err.message); - return ExitCode::from(err.code as u8); + let _ = unstack(s.0.clone(), s.1.clone()); + return ExitCode::SUCCESS; + } + Err(e) => { + eprintln!("{}: {}", argv[0], e.message); + return e.code; }, }; } + + /* else, read from stdin */ + loop { /* take input until EOF */ + if let Err(e) = stdin().read_line(&mut buf) { + return err(&argv[0], e, Some(EX_IOERR)); + } + + match eval(&buf.trim(), stack) { + Ok(s) => { + buf.clear(); + stack = s.0.clone(); + + if unstack(s.0, s.1) { continue; } + else { break; } + }, + Err(e) => { + eprintln!("{}: {}", argv[0], e.message); + return e.code; + }, + }; + } + ExitCode::SUCCESS } -- 2.46.1 From 5eb71e90a3fe0f453ab35b3f326902e78045a369 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 17:22:57 -0600 Subject: [PATCH 10/44] tests/bonsai: rpn.mk: tests the standard input --- tests/bonsai/rpn.mk | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/bonsai/rpn.mk b/tests/bonsai/rpn.mk index 6fecab9..297295c 100755 --- a/tests/bonsai/rpn.mk +++ b/tests/bonsai/rpn.mk @@ -6,7 +6,7 @@ # notice are preserved. This file is offered as-is, without any warranty. .PHONY: rpn_tests -rpn_tests: rpn_help rpn_add rpn_sub rpn_mul rpn_div rpn_mod rpn_flr +rpn_tests: rpn_help rpn_add rpn_sub rpn_mul rpn_div rpn_mod rpn_flr rpn_stdin .PHONY: rpn_help rpn_help: $(BIN)/rpn @@ -41,3 +41,8 @@ rpn_mod: $(BIN)/rpn rpn_flr: $(BIN)/rpn test "$$($(BIN)/rpn 12 5 //)" -eq 2 test "$$($(BIN)/rpn 9 4 //)" -eq 2 + +# done last because all operations have been tested +.PHONY: rpn_stdin +rpn_stdin: $(BIN)/rpn + test "$$(printf '1\n2\n+\n3\n-\n' | $(BIN)/rpn | tail -n1)" -eq 0 -- 2.46.1 From ff4ff825bdd20d4d5554e6084cacf8b79463cc21 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 17:26:26 -0600 Subject: [PATCH 11/44] swab(1): changes disparate error handling functions to one function --- src/swab.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/swab.rs b/src/swab.rs index 6afed28..2cd7800 100644 --- a/src/swab.rs +++ b/src/swab.rs @@ -35,15 +35,9 @@ use strerror::StrError; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; - -fn oserr(argv0: &str, e: Error) -> ExitCode { +fn err(argv0: &str, e: Error, code: u8) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_OSERR) -} - -fn ioerr(argv0: &str, e: Error) -> ExitCode { - eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_IOERR) + ExitCode::from(code) } fn usage(s: &str) -> ExitCode { @@ -57,7 +51,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio"); if let Err(e) = pledge(Some(promises), None) { - return oserr(&argv[0], e); + return err(&argv[0], e, EX_OSERR); } } @@ -94,16 +88,16 @@ fn main() -> ExitCode { if let Err(e) = output.write(&right) .and_then(|_| output.write(&left)) { - break ioerr(&argv[0], e); + break err(&argv[0], e, EX_IOERR); } }, Ok(v) => { // partial read; partially write if let Err(e) = output.write(&buf[..v]) { - break ioerr(&argv[0], e); + break err(&argv[0], e, EX_IOERR); } }, - Err(e) => break oserr(&argv[0], e) + Err(e) => break err(&argv[0], e, EX_OSERR) } } } -- 2.46.1 From 232629f4d38c7459d2717336ef607a71e6b340d2 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 17:55:00 -0600 Subject: [PATCH 12/44] hru(1): makes more efficient --- src/hru.rs | 65 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index 70981a3..236a2aa 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -19,7 +19,7 @@ use std::{ cmp::Ordering, env::args, - io::{ stdin, stdout, Write }, + io::{ Write, stdin, stdout }, process::{ ExitCode, exit }, }; @@ -47,6 +47,16 @@ const LIST: [(u32, &str); 10] = [ (30, "Q"), /* quetta */ ]; +fn err(argv0: &String, message: String, code: Option) -> ExitCode { + eprintln!("{}: {}", argv0, message); + ExitCode::from(code.unwrap_or(1 /* unknown error */)) +} + +fn usage(argv0: &String) -> ExitCode { + eprintln!("Usage: {}", argv0); + ExitCode::from(EX_USAGE) +} + fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { /* preserve decimal places in output by casting to a float */ let mut out = (input as f64, (0_u32, "")); @@ -81,49 +91,44 @@ fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { fn main() -> ExitCode { let argv = args().collect::>(); - if let Some(_) = argv.get(1) { - eprintln!("Usage: {}", argv[0]); - return ExitCode::from(EX_USAGE as u8); - } + if let Some(_) = argv.get(1) { return usage(&argv[0]); } #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR); + return err(&argv[0], e.strerror(), Some(EX_OSERR)); } } let mut buf = String::new(); - while let Ok(_) = stdin().read_line(&mut buf) { - if buf.is_empty() { return ExitCode::SUCCESS; } + if let Err(e) = stdin().read_line(&mut buf) { + return err(&argv[0], e.strerror(), Some(EX_IOERR)); + } - let n: u128 = match buf.trim().parse() { - Ok(f) => { - buf.clear(); - f - }, - Err(e) => { - eprintln!("{}: {}", argv[0], e); - return ExitCode::from(EX_DATAERR); - }, - }; + if buf.is_empty() { return ExitCode::SUCCESS; } - let (number, prefix) = convert(n).unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], e); - exit(EX_SOFTWARE.into()); - }); + let n: u128 = match buf.trim().parse() { + Ok(f) => { + buf.clear(); + f + }, + Err(e) => return err(&argv[0], e.to_string(), Some(EX_DATAERR)), + }; - let si_prefix = format!("{}B", prefix.1); + let (number, prefix) = convert(n).unwrap_or_else(|e| { + let _ = err(&argv[0], e.to_string(), None); + exit(EX_SOFTWARE.into()); + }); - /* round output number to one decimal place */ - let out = ((number * 10.0).round() / 10.0).to_string(); + let si_prefix = prefix.1.to_owned() + "B"; - if let Err(e) = stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_IOERR); - } + /* round output number to one decimal place */ + let rounded = (number * 10.0).round() / 10.0; + let out = rounded.to_string() + " " + &si_prefix + &'\n'.to_string(); + + if let Err(e) = stdout().write_all(out.as_bytes()) { + return err(&argv[0], e.strerror(), Some(EX_IOERR)); } ExitCode::SUCCESS -- 2.46.1 From cc53dab035c5d5fe5037981bd1fedee87a3962a4 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 18:18:27 -0600 Subject: [PATCH 13/44] rpn(1): handles errors when writing to stdout --- src/rpn.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index ff0b1bd..d4056b6 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -46,7 +46,7 @@ use std::{ collections::VecDeque, env::args, fmt::{ self, Display, Formatter }, - io::stdin, + io::{ Error, Write, stdin, stdout }, process::ExitCode, }; @@ -125,7 +125,7 @@ struct EvaluationError { /* I’m no math nerd but I want the highest possible approximation of 0.9 * repeating and it seems this can give it to me */ -const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0; +const PRECISION_MOD: f64 = 0.9 + f64::EPSILON; fn err(argv0: &String, e: std::io::Error, code: Option) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); @@ -204,16 +204,16 @@ fn round_precise(value: &f64) -> f64 { (value * multiplier).round() / multiplier } -fn unstack(stack: VecDeque, op: bool) -> bool { +fn unstack(stack: VecDeque, op: bool) -> Result { if let Some(val) = stack.iter().last() { - if !op { return true; } + if !op { return Ok(true); } - println!("{}", round_precise(val).to_string()); + let out = round_precise(val).to_string() + &'\n'.to_string(); + + return stdout().write_all(out.as_bytes()).map(|_| true); } else { - return false; + return Ok(false); } - - return true; } fn main() -> ExitCode { @@ -240,7 +240,9 @@ fn main() -> ExitCode { match eval(&input, stack) { Ok(s) => { - let _ = unstack(s.0.clone(), s.1.clone()); + if let Err(e) = unstack(s.0.clone(), s.1.clone()) { + return err(&argv[0], e, Some(EX_IOERR)); + } return ExitCode::SUCCESS; } Err(e) => { @@ -261,8 +263,11 @@ fn main() -> ExitCode { buf.clear(); stack = s.0.clone(); - if unstack(s.0, s.1) { continue; } - else { break; } + match unstack(s.0, s.1) { + Ok(b) if b => continue, + Ok(_) => break, + Err(e) => return err(&argv[0], e, Some(EX_IOERR)), + }; }, Err(e) => { eprintln!("{}: {}", argv[0], e.message); -- 2.46.1 From b875aa1058339a17640817136037fb40451ca263 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 19:00:01 -0600 Subject: [PATCH 14/44] rpn(1): more improvements --- src/rpn.rs | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index d4056b6..42b30a1 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -120,14 +120,20 @@ impl Display for CalcType { #[derive(Debug, Clone)] struct EvaluationError { message: String, - code: ExitCode, + code: u8, +} + +impl StrError for EvaluationError { + fn strerror(&self) -> String { + self.message.clone() + } } /* I’m no math nerd but I want the highest possible approximation of 0.9 * repeating and it seems this can give it to me */ const PRECISION_MOD: f64 = 0.9 + f64::EPSILON; -fn err(argv0: &String, e: std::io::Error, code: Option) -> ExitCode { +fn err(argv0: &String, e: &T, code: Option) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); ExitCode::from(code.unwrap_or(1 /* unknown error */)) } @@ -150,6 +156,7 @@ fn eval( .rev() .map(|t| CalcType::from(t)) .collect(); + let mut ops: VecDeque = VecDeque::new(); while let Some(n) = toks.pop_back() { @@ -157,18 +164,15 @@ fn eval( Val(v) => stack.push_back(v), Invalid(i) => { return Err(EvaluationError { - message: format!("{}: Invalid token", i), - code: ExitCode::from(EX_DATAERR), + message: format!("{}: invalid token", i), + code: EX_DATAERR, }) }, op => { ops.push_back(op.clone()); oper = true; /* this is an operation */ - let vals = ( - stack.pop_back(), - stack.pop_back(), - ); + let vals = (stack.pop_back(), stack.pop_back()); if let (Some(x), Some(y)) = vals { match op { @@ -183,8 +187,8 @@ fn eval( }; } else { return Err(EvaluationError { - message: format!("{}: Unexpected operation", op), - code: ExitCode::from(EX_DATAERR), + message: format!("{}: unexpected operation", op), + code: EX_DATAERR, }) } }, @@ -204,6 +208,7 @@ fn round_precise(value: &f64) -> f64 { (value * multiplier).round() / multiplier } +/* print the stack and let the caller know if evaluation should continue */ fn unstack(stack: VecDeque, op: bool) -> Result { if let Some(val) = stack.iter().last() { if !op { return Ok(true); } @@ -222,7 +227,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio"); if let Err(e) = pledge(Some(promises), None) { - return err(&argv[0], e, Some(EX_OSERR)); + return err(&argv[0], &e, Some(EX_OSERR)); } } @@ -240,14 +245,16 @@ fn main() -> ExitCode { match eval(&input, stack) { Ok(s) => { + /* we can ignore the return value of unstack() because we are + * not continually evaluating from stdin */ if let Err(e) = unstack(s.0.clone(), s.1.clone()) { - return err(&argv[0], e, Some(EX_IOERR)); + return err(&argv[0], &e, Some(EX_IOERR)); } + return ExitCode::SUCCESS; - } + }, Err(e) => { - eprintln!("{}: {}", argv[0], e.message); - return e.code; + return err(&argv[0], &e, Some(e.code)); }, }; } @@ -255,23 +262,24 @@ fn main() -> ExitCode { /* else, read from stdin */ loop { /* take input until EOF */ if let Err(e) = stdin().read_line(&mut buf) { - return err(&argv[0], e, Some(EX_IOERR)); + return err(&argv[0], &e, Some(EX_IOERR)); } match eval(&buf.trim(), stack) { Ok(s) => { buf.clear(); - stack = s.0.clone(); + stack = s.0.clone(); /* out with the old, in with the new */ match unstack(s.0, s.1) { Ok(b) if b => continue, Ok(_) => break, - Err(e) => return err(&argv[0], e, Some(EX_IOERR)), + Err(e) => { + return err(&argv[0], &e, Some(EX_IOERR)) + }, }; }, Err(e) => { - eprintln!("{}: {}", argv[0], e.message); - return e.code; + return err(&argv[0], &e, Some(e.code)); }, }; } -- 2.46.1 From 5b666d6858f69bfe20f6043aa778e41d850ea067 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 27 Aug 2024 23:54:39 -0600 Subject: [PATCH 15/44] fop(1): adds null unveil --- src/fop.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index a58f759..974c5bb 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -32,7 +32,8 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate openbsd; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] +use openbsd::{ Promises, pledge, unveil }; fn err(argv0: &String, e: Error) { eprintln!("{}: {}", argv0, e.strerror()); @@ -49,11 +50,16 @@ fn main() -> ExitCode { let mut optind = 1; #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio proc exec"); + let promises = Promises::new("exec proc stdio unveil"); if let Err(e) = pledge(Some(promises), None) { err(&argv[0], e); return ExitCode::from(EX_OSERR); } + + if let Err(e) = unveil(None, None) { + err(&argv[0], e); + return ExitCode::from(EX_OSERR); + } } while let Some(opt) = argv.getopt("d:") { -- 2.46.1 From 9aeadd8f8fd73781663dc8c9e301eeca9028fac6 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 27 Aug 2024 23:57:12 -0600 Subject: [PATCH 16/44] hru(1): adds null unveil --- src/hru.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index 236a2aa..978f261 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -31,7 +31,7 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE, EX_USAGE }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate openbsd; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; /* list of SI prefixes */ const LIST: [(u32, &str); 10] = [ @@ -94,10 +94,14 @@ fn main() -> ExitCode { if let Some(_) = argv.get(1) { return usage(&argv[0]); } #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio"); + let promises = Promises::new("stdio unveil"); if let Err(e) = pledge(Some(promises), None) { return err(&argv[0], e.strerror(), Some(EX_OSERR)); } + + if let Err(e) = unveil(None, None) { + return err(&argv[0], e.strerror(), Some(EX_OSERR)); + } } let mut buf = String::new(); -- 2.46.1 From 06384c72fb9ca9742e95a34f944e89f49f7776f4 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:09:40 -0600 Subject: [PATCH 17/44] intcmp(1): adds null unveil --- src/intcmp.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/intcmp.rs b/src/intcmp.rs index 456f268..9efc8be 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -26,27 +26,35 @@ extern crate getopt; extern crate sysexits; use getopt::GetOpt; -use sysexits::EX_USAGE; +use sysexits::{ EX_DATAERR, EX_USAGE }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] extern crate strerror; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; #[cfg(target_os="openbsd")] use strerror::StrError; -fn usage(s: &str) -> ExitCode { - eprintln!("Usage: {} [-egl] integer integer...", s); - ExitCode::from(EX_USAGE as u8) +fn err(argv0: &String, e: String, code: u8) -> ExitCode { + eprintln!("{}: {}", argv0, e); + ExitCode::from(code) +} + +fn usage(argv0: &str) -> ExitCode { + eprintln!("Usage: {} [-egl] integer integer...", argv0); + ExitCode::from(EX_USAGE) } fn main() -> ExitCode { let argv = args().collect::>(); #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio"); + let promises = Promises::new("stdio unveil"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return err(&argv[0], e.strerror(), EX_OSERR); + } + + if let Err(e) = unveil(None, None) { + return err(&argv[0], e.strerror(), EX_OSERR); } } @@ -78,8 +86,8 @@ fn main() -> ExitCode { match arg.parse::() { /* parse current operand */ Ok(n) => currn = n, Err(e) => { - eprintln!("{}: {}: {}", &argv[0], arg, e); - return ExitCode::from(EX_USAGE as u8); + let error = arg.to_owned() + ": " + &e.to_string(); + return err(&argv[0], error, EX_DATAERR); } } -- 2.46.1 From b946469da5295f266cc8d760488867b9a6a8f9ae Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:13:21 -0600 Subject: [PATCH 18/44] rpn(1): adds null unveil --- src/rpn.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 42b30a1..d1cd3c3 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -60,7 +60,7 @@ use sysexits::{ EX_DATAERR, EX_IOERR }; #[cfg(target_os="openbsd")] extern crate strerror; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] use strerror::StrError; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; #[derive(Clone, PartialEq, PartialOrd, Debug)] /* enum CalcType is a type containing operations used in the calculator */ @@ -225,10 +225,14 @@ fn main() -> ExitCode { let argv = args().collect::>(); #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio"); + let promises = Promises::new("stdio unveil"); if let Err(e) = pledge(Some(promises), None) { return err(&argv[0], &e, Some(EX_OSERR)); } + + if let Err(e) = unveil(None, None) { + return err(&argv[0], &e, Some(EX_OSERR)); + } } let mut stack = VecDeque::new(); -- 2.46.1 From 8fd5bdf5a6c8a18092c946e81c4289df953bdc16 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:15:15 -0600 Subject: [PATCH 19/44] swab(1): adds null unveil --- src/swab.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/swab.rs b/src/swab.rs index 2cd7800..2ea9fcc 100644 --- a/src/swab.rs +++ b/src/swab.rs @@ -33,7 +33,7 @@ use sysexits::{ EX_IOERR, EX_OK, EX_OSERR, EX_USAGE }; use strerror::StrError; #[cfg(target_os="openbsd")] extern crate openbsd; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; fn err(argv0: &str, e: Error, code: u8) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); @@ -49,10 +49,14 @@ fn main() -> ExitCode { let argv = args().collect::>(); #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio"); + let promises = Promises::new("stdio unveil"); if let Err(e) = pledge(Some(promises), None) { return err(&argv[0], e, EX_OSERR); } + + if let Err(e) = unveil(None, None) { + return err(&argv[0], e, EX_OSERR); + } } let mut buf: Vec = Vec::new(); // holds the sequence getting swabbed -- 2.46.1 From efedcd3ae4296b218bbc7e0eae1a3dd4d43030e7 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:24:35 -0600 Subject: [PATCH 20/44] true(1), false(1): adds null unveils --- src/false.c | 5 +++-- src/true.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/false.c b/src/false.c index 555a170..191cfce 100644 --- a/src/false.c +++ b/src/false.c @@ -8,12 +8,13 @@ */ #ifdef __OpenBSD__ -# include /* pledge(2) */ +# include /* NULL, pledge(2), unveil(2) */ #endif int main(void) { #ifdef __OpenBSD__ - pledge(NULL, NULL); + (void)pledge(NULL, NULL); + (void)unveil(NULL, NULL); #endif return 1; } diff --git a/src/true.c b/src/true.c index 0e2f91d..dbe9974 100644 --- a/src/true.c +++ b/src/true.c @@ -8,11 +8,12 @@ */ #ifdef __OpenBSD__ -# include /* pledge(2) */ +# include /* NULL, pledge(2), unveil(2) */ #endif int main(void) { #ifdef __OpenBSD__ - pledge(NULL, NULL); + (void)pledge(NULL, NULL); + (void)unveil(NULL, NULL); #endif } -- 2.46.1 From 6166a3ca365f3efefd222c93ff10ef301cea96f1 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:26:26 -0600 Subject: [PATCH 21/44] npc(1): adds null unveil --- src/npc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/npc.c b/src/npc.c index cd488e5..67d7f28 100644 --- a/src/npc.c +++ b/src/npc.c @@ -20,7 +20,7 @@ #include /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin, * stdout, EOF */ #include /* EX_IOERR, EX_OK, EX_OSERR, EX_USAGE */ -#include /* pledge(2), getopt(3) */ +#include /* NULL, getopt(3), pledge(2), unveil(2) */ char *program_name = "npc"; @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) { char showtab = 0; /* prints tab characters in caret notation */ #ifdef __OpenBSD__ - if (pledge("stdio", NULL) == -1) { + if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL)) { perror(argv[0] == NULL ? program_name : argv[0]); return EX_OSERR; } -- 2.46.1 From 41982c2f736b8f8ffb3df924566849b7514ba23c Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:30:45 -0600 Subject: [PATCH 22/44] str(1): adds null unveil --- src/str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/str.c b/src/str.c index 9ed27c2..834d607 100644 --- a/src/str.c +++ b/src/str.c @@ -25,7 +25,7 @@ #include /* EX_OSERR, EX_USAGE */ #ifdef __OpenBSD__ -# include /* pledge(2) */ +# include /* pledge(2), unveil(2) */ #endif char *program_name = "str"; @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) { program_name = argv[0] == NULL ? program_name : argv[0]; #ifdef __OpenBSD__ - if (pledge("stdio", NULL) == -1) { + if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) { perror(program_name); return EX_OSERR; } -- 2.46.1 From 9f520df82be412c90dfbe8d7b43982302ab9d991 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:31:02 -0600 Subject: [PATCH 23/44] strcmp(1): adds null unveil --- src/strcmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/strcmp.c b/src/strcmp.c index 6d930d5..6eda193 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -20,7 +20,7 @@ #include /* EX_OK, EX_OSERR, EX_USAGE */ #ifdef __OpenBSD__ -# include /* pledge(2) */ +# include /* pledge(2), unveil(2) */ #endif char *program_name = "strcmp"; @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) { unsigned int i; #ifdef __OpenBSD__ - if (pledge("stdio", NULL) == -1) { + if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) { perror(argv[0] == NULL ? program_name : argv[0]); return EX_OSERR; -- 2.46.1 From 8d00fa0afdcb4a0e768627ff3c4ec0584864750d Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:46:32 -0600 Subject: [PATCH 24/44] fop(1): minor formatting change --- src/fop.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 974c5bb..b6da510 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -32,8 +32,7 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate openbsd; -#[cfg(target_os="openbsd")] -use openbsd::{ Promises, pledge, unveil }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; fn err(argv0: &String, e: Error) { eprintln!("{}: {}", argv0, e.strerror()); -- 2.46.1 From f50bfeea92fee4b554d43a883f007300af7f475f Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 18:05:56 -0600 Subject: [PATCH 25/44] fop(1): fixes panic on no arguments --- src/fop.rs | 2 ++ tests/bonsai/fop.mk | 1 + 2 files changed, 3 insertions(+) diff --git a/src/fop.rs b/src/fop.rs index b6da510..261aba5 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -61,6 +61,8 @@ fn main() -> ExitCode { } } + if argv.len() == 1 { return ExitCode::from(usage(&argv[0])); } + while let Some(opt) = argv.getopt("d:") { match opt.opt() { Ok("d") => { diff --git a/tests/bonsai/fop.mk b/tests/bonsai/fop.mk index b538031..230e1c1 100755 --- a/tests/bonsai/fop.mk +++ b/tests/bonsai/fop.mk @@ -24,6 +24,7 @@ fop_fail: $(BIN)/fop ! printf 'test\n' | $(BIN)/fop 1 cat ! printf 'test\n' | $(BIN)/fop 'test' cat ! printf 'test\n' | $(BIN)/fop -d'test' cat + ! $(BIN)/fop .PHONY: fop_functionality fop_functionality: $(BIN)/fop -- 2.46.1 From 731e62ee7ea81e0aa5b9cdb34a233e83b69ae0e4 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 18:24:49 -0600 Subject: [PATCH 26/44] rpn(1): made evaluation function more readable --- src/rpn.rs | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index d1cd3c3..6907772 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -138,6 +138,33 @@ fn err(argv0: &String, e: &T, code: Option) -> ExitCode { ExitCode::from(code.unwrap_or(1 /* unknown error */)) } +fn operate( + mut stack: VecDeque, + op: CalcType, +) -> Result, EvaluationError> { + let vals = (stack.pop_back(), stack.pop_back()); + + if let (Some(x), Some(y)) = vals { + match op { + Add => stack.push_back(y + x), + Subtract => stack.push_back(y - x), + Multiply => stack.push_back(y * x), + Divide => stack.push_back(y / x), + Power => stack.push_back(y.powf(x)), + Floor => stack.push_back((y / x).floor()), + Modulo => stack.push_back(y % x), + _ => {}, + }; + } else { + return Err(EvaluationError { + message: format!("{}: unexpected operation", op), + code: EX_DATAERR, + }) + } + + Ok(stack) +} + fn eval( input: &str, initial_stack: VecDeque, @@ -170,27 +197,9 @@ fn eval( }, op => { ops.push_back(op.clone()); + oper = true; /* this is an operation */ - - let vals = (stack.pop_back(), stack.pop_back()); - - if let (Some(x), Some(y)) = vals { - match op { - Add => stack.push_back(y + x), - Subtract => stack.push_back(y - x), - Multiply => stack.push_back(y * x), - Divide => stack.push_back(y / x), - Power => stack.push_back(y.powf(x)), - Floor => stack.push_back((y / x).floor()), - Modulo => stack.push_back(y % x), - _ => {}, - }; - } else { - return Err(EvaluationError { - message: format!("{}: unexpected operation", op), - code: EX_DATAERR, - }) - } + return operate(stack, op).map(|s| (s, oper)); }, }; } -- 2.46.1 From 62ce28852429a5a0ef74c4cf270f2da0738cfd84 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 4 Sep 2024 22:12:44 -0600 Subject: [PATCH 27/44] rpn(1): makes rounding more efficient --- src/rpn.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 6907772..7383201 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -129,10 +129,6 @@ impl StrError for EvaluationError { } } -/* I’m no math nerd but I want the highest possible approximation of 0.9 - * repeating and it seems this can give it to me */ -const PRECISION_MOD: f64 = 0.9 + f64::EPSILON; - fn err(argv0: &String, e: &T, code: Option) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); ExitCode::from(code.unwrap_or(1 /* unknown error */)) @@ -211,9 +207,9 @@ fn eval( fn round_precise(value: &f64) -> f64 { /* Set floating-point precision for correcting rounding errors based on * machine epsilon */ - let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as i32; - - let multiplier = 10_f64.powi(precision as i32); + let precision = (-f64::EPSILON.log10()).floor() as i32; + let multiplier = 10_f64.powi(precision); + (value * multiplier).round() / multiplier } -- 2.46.1 From b8e5901d97e6ef788071630ed251aaf0d94342b3 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:09:46 -0600 Subject: [PATCH 28/44] libopenbsd.rs(3): adds default value for Promises --- src/libopenbsd.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libopenbsd.rs b/src/libopenbsd.rs index f3e60b9..613aa68 100644 --- a/src/libopenbsd.rs +++ b/src/libopenbsd.rs @@ -43,6 +43,10 @@ impl Promises { } } +impl Default for Promises { + fn default() -> Self { Promises::new("") } +} + pub fn pledge( promises: Option, execpromises: Option ) -> Result<(), Error> { -- 2.46.1 From 73a75a32df861223db4a31738f0cabc60be540d5 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:10:26 -0600 Subject: [PATCH 29/44] hru(1): uses default Promises value for child processes --- src/hru.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hru.rs b/src/hru.rs index 978f261..153032b 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -95,7 +95,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio unveil"); - if let Err(e) = pledge(Some(promises), None) { + if let Err(e) = pledge(Some(promises), Some(Promises::default())) { return err(&argv[0], e.strerror(), Some(EX_OSERR)); } -- 2.46.1 From e385d873eca340303a6574b3fd3de6497892dec6 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:11:22 -0600 Subject: [PATCH 30/44] intcmp(1): uses default value for Promises --- src/intcmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intcmp.rs b/src/intcmp.rs index 9efc8be..7bd4819 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -49,7 +49,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio unveil"); - if let Err(e) = pledge(Some(promises), None) { + if let Err(e) = pledge(Some(promises), Some(Promises::default())) { return err(&argv[0], e.strerror(), EX_OSERR); } -- 2.46.1 From df0a236f8138e12dcafda5b163caf12243d2252f Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:12:14 -0600 Subject: [PATCH 31/44] mm(1): uses default for Promises --- src/mm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mm.rs b/src/mm.rs index 80a9cd7..0565ecb 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -62,7 +62,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("cpath rpath stdio unveil wpath"); - if let Err(e) = pledge(Some(promises), None) { + if let Err(e) = pledge(Some(promises), Some(Promises::default())) { return err(&argv[0], e, Some(EX_OSERR)); } } -- 2.46.1 From 1ccdc65d307385ee10610ed4acfd29940d28f06a Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:13:20 -0600 Subject: [PATCH 32/44] rpn(1): uses default for promises --- src/rpn.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpn.rs b/src/rpn.rs index 7383201..b2c1bcc 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -231,7 +231,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio unveil"); - if let Err(e) = pledge(Some(promises), None) { + if let Err(e) = pledge(Some(promises), Some(Promises::default())) { return err(&argv[0], &e, Some(EX_OSERR)); } -- 2.46.1 From 800a097903d3eeedd1eccba1ae663a419206fbc5 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 10 Sep 2024 17:14:02 -0600 Subject: [PATCH 33/44] swab(1): uses default for promises --- src/swab.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swab.rs b/src/swab.rs index 2ea9fcc..4dd0021 100644 --- a/src/swab.rs +++ b/src/swab.rs @@ -50,7 +50,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("stdio unveil"); - if let Err(e) = pledge(Some(promises), None) { + if let Err(e) = pledge(Some(promises), Some(Promises::default())) { return err(&argv[0], e, EX_OSERR); } -- 2.46.1 From 0b3ed37c380fd9f44be686fbdad5cf02f3429964 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 02:32:03 -0600 Subject: [PATCH 34/44] libopenbsd.rs(3): makes using statically-allocated arrays possible --- src/libopenbsd.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/libopenbsd.rs b/src/libopenbsd.rs index 613aa68..2557dd8 100644 --- a/src/libopenbsd.rs +++ b/src/libopenbsd.rs @@ -69,14 +69,12 @@ pub fn pledge( pub struct UnveilPerms(CString); impl UnveilPerms { - pub fn new(permissions: Vec) -> Self { - if permissions.is_empty() { - return UnveilPerms(CString::new("").unwrap()); - } - - UnveilPerms( - CString::new(permissions.iter().collect::()).unwrap() - ) + pub fn new>(permissions: T) -> Self { + let perms = CString::new( + permissions.into_iter().collect::() + ).unwrap(); + + UnveilPerms(perms) } } @@ -87,9 +85,9 @@ pub fn unveil( let path_c = path.map(CString::new).map(Result::unwrap); let arg1 = path_c.map(|p| p.into_raw() as *const c_char).unwrap_or(null()); - let arg2 = permissions - .map(|p| p.0.into_raw() as *const c_char) - .unwrap_or(null()); + let arg2 = permissions.map(|p| { + p.0.into_raw() as *const c_char + }).unwrap_or(null()); unsafe { match openbsd::unveil(arg1, arg2) { -- 2.46.1 From 7498b283ce2dd9dcc7ae1d404884f49eb09aa91d Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 02:32:32 -0600 Subject: [PATCH 35/44] mm(1): updates to use new unveil api --- src/mm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index 0565ecb..700082e 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -115,7 +115,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { for input in &ins { - let perms = UnveilPerms::new(vec!['r']); + let perms = UnveilPerms::new(['r']); if let Err(e) = unveil(Some(&input), Some(perms)) { return err(&argv[0], e, Some(EX_OSERR)); @@ -123,7 +123,7 @@ fn main() -> ExitCode { } for output in &outs { - let perms = UnveilPerms::new(vec!['c', 'w']); + let perms = UnveilPerms::new(['c', 'w']); if let Err(e) = unveil(Some(&output), Some(perms)) { return err(&argv[0], e, Some(EX_OSERR)); -- 2.46.1 From e0b5467fb64f648201da75037c8c7af1d128806a Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:13:38 -0600 Subject: [PATCH 36/44] npc(1): fixes pledge invocation --- src/npc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/npc.c b/src/npc.c index 67d7f28..e00b36e 100644 --- a/src/npc.c +++ b/src/npc.c @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) { char showtab = 0; /* prints tab characters in caret notation */ #ifdef __OpenBSD__ - if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL)) { + if (pledge("stdio unveil", "") == -1 || unveil(NULL, NULL)) { perror(argv[0] == NULL ? program_name : argv[0]); return EX_OSERR; } -- 2.46.1 From f25a499ca02114f6c45bfa70234ffd6fa74d6802 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:14:36 -0600 Subject: [PATCH 37/44] scrut(1): fixes pledge(2) invocation --- src/scrut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scrut.c b/src/scrut.c index cfdd923..41504d2 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -46,7 +46,7 @@ int main(int argc, char *argv[]) { program_name = argv[0] == NULL ? program_name : argv[0]; #ifdef __OpenBSD__ - if (pledge("rpath stdio unveil", NULL) == -1) { + if (pledge("rpath stdio unveil", "") == -1) { perror(program_name); return EX_OSERR; } -- 2.46.1 From a43daf2cf2a37f8c454c256dbe39f82ee0ec8d1c Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:15:13 -0600 Subject: [PATCH 38/44] str(1): fixes pledge(2) invocation --- src/str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/str.c b/src/str.c index 834d607..6d80bff 100644 --- a/src/str.c +++ b/src/str.c @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) { program_name = argv[0] == NULL ? program_name : argv[0]; #ifdef __OpenBSD__ - if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) { + if (pledge("stdio unveil", "") == -1 || unveil(NULL, NULL) == -1) { perror(program_name); return EX_OSERR; } -- 2.46.1 From 8f1e570b501a0c77f60bc4121458a0d070528762 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:15:58 -0600 Subject: [PATCH 39/44] strcmp(1): fixes pledge(2) invocation --- src/strcmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strcmp.c b/src/strcmp.c index 6eda193..ff2e1ae 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) { unsigned int i; #ifdef __OpenBSD__ - if (pledge("stdio unveil", NULL) == -1 || unveil(NULL, NULL) == -1) { + if (pledge("stdio unveil", "") == -1 || unveil(NULL, NULL) == -1) { perror(argv[0] == NULL ? program_name : argv[0]); return EX_OSERR; -- 2.46.1 From 579bb9887442bbcdcc7b6a388360766832aa61f6 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:18:01 -0600 Subject: [PATCH 40/44] true(1): fixes pledge(2) invocation --- src/true.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/true.c b/src/true.c index dbe9974..43091d7 100644 --- a/src/true.c +++ b/src/true.c @@ -13,7 +13,6 @@ int main(void) { #ifdef __OpenBSD__ - (void)pledge(NULL, NULL); - (void)unveil(NULL, NULL); + (void)pledge("stdio", ""); #endif } -- 2.46.1 From ba73f505279e3b80ae13b0865a65301d8991daec Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:19:23 -0600 Subject: [PATCH 41/44] true(1): removes NULL from commented imports --- src/true.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/true.c b/src/true.c index 43091d7..d436aee 100644 --- a/src/true.c +++ b/src/true.c @@ -8,7 +8,7 @@ */ #ifdef __OpenBSD__ -# include /* NULL, pledge(2), unveil(2) */ +# include /* pledge(2) */ #endif int main(void) { -- 2.46.1 From 8bdb72ece81eccb5bb823118ebace19efa0a70c1 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:19:56 -0600 Subject: [PATCH 42/44] false(1): fixes pledge(2) invocation --- src/false.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/false.c b/src/false.c index 191cfce..3cea001 100644 --- a/src/false.c +++ b/src/false.c @@ -8,13 +8,12 @@ */ #ifdef __OpenBSD__ -# include /* NULL, pledge(2), unveil(2) */ +# include /* pledge(2) */ #endif int main(void) { #ifdef __OpenBSD__ - (void)pledge(NULL, NULL); - (void)unveil(NULL, NULL); + (void)pledge("stdio", ""); #endif return 1; } -- 2.46.1 From c4cd2563f9ae03f9e158a6f8f8fb86ccee6277d8 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 11 Sep 2024 03:26:00 -0600 Subject: [PATCH 43/44] scrut(1): fixes failing write test --- src/scrut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scrut.c b/src/scrut.c index 41504d2..2d3c2bf 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) { struct stat buf; #ifdef __OpenBSD__ - if (unveil(*argv, "r") == -1) { + if (unveil(*argv, "rw") == -1) { perror(program_name); return EX_OSERR; } -- 2.46.1 From 982c67df13d1fba8d296bb7d4f79d2d92279882a Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 1 Oct 2024 22:08:56 -0600 Subject: [PATCH 44/44] intcmp(1), swab(1): consistent type annotations --- src/intcmp.rs | 2 +- src/swab.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/intcmp.rs b/src/intcmp.rs index 7bd4819..5f8f64e 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -39,7 +39,7 @@ fn err(argv0: &String, e: String, code: u8) -> ExitCode { ExitCode::from(code) } -fn usage(argv0: &str) -> ExitCode { +fn usage(argv0: &String) -> ExitCode { eprintln!("Usage: {} [-egl] integer integer...", argv0); ExitCode::from(EX_USAGE) } diff --git a/src/swab.rs b/src/swab.rs index 4dd0021..e167230 100644 --- a/src/swab.rs +++ b/src/swab.rs @@ -35,12 +35,12 @@ use strerror::StrError; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; -fn err(argv0: &str, e: Error, code: u8) -> ExitCode { +fn err(argv0: &String, e: Error, code: u8) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); ExitCode::from(code) } -fn usage(s: &str) -> ExitCode { +fn usage(s: &String) -> ExitCode { eprintln!("Usage: {} [-w word_size]", s); ExitCode::from(EX_USAGE) } -- 2.46.1