From e81703c6e1555586d20cfafbf972f61c890635f1 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:04:53 -0700 Subject: [PATCH 01/14] sterror(3): added library for C-like error messages; Makefile: some efficiency changes --- Makefile | 43 ++++++++++++++++++++++++------------------- src/strerror.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 src/strerror.rs diff --git a/Makefile b/Makefile index f181e20..cb6bca6 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,11 @@ PREFIX=/usr/local -CC=cc -RUSTC=rustc +CC != command -v "$$CC" || printf 'cc\n' +RUSTC != command -v "$$RUSTC" || printf 'rustc\n' +RUSTLIBS=--extern getopt=build/o/libgetopt.rlib \ + --extern sysexits=build/o/libsysexits.rlib \ + --extern strerror=build/o/libstrerror.rlib .PHONY: all all: dj false fop hru intcmp rpn scrut str strcmp true @@ -43,6 +46,18 @@ test: build tests/posix-compat.sh $(RUSTC) --test src/getopt-rs/lib.rs -o build/test/getopt +.PHONY: rustlibs +rustlibs: build/o/libsysexits.rlib build/o/libgetopt.rlib \ + build/o/libstrerror.rlib + +build/o/libgetopt.rlib: build src/getopt-rs/lib.rs + $(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \ + -o $@ src/getopt-rs/lib.rs + +build/o/libstrerror.rlib: build src/strerror.rs + $(RUSTC) $(RUSTFLAGS) --crate-type=lib -o $@ \ + src/strerror.rs + build/o/libsysexits.rlib: build # bandage solution until bindgen(1) gets stdin support printf '#define EXIT_FAILURE 1\n' | cat - include/sysexits.h \ @@ -51,11 +66,7 @@ build/o/libsysexits.rlib: build "$$(printf '#include \n' \ | cpp -M -idirafter "build/include" - \ | sed 's/ /\n/g' | grep sysexits.h)" \ - | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o build/o/libsysexits.rlib - - -build/o/libgetopt.rlib: src/getopt-rs/lib.rs - $(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \ - -o build/o/libgetopt.rlib src/getopt-rs/lib.rs + | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ - .PHONY: dj dj: build/bin/dj @@ -69,17 +80,13 @@ build/bin/false: src/false.c build .PHONY: fop fop: build/bin/fop -build/bin/fop: src/fop.rs build build/o/libgetopt.rlib build/o/libsysexits.rlib - $(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \ - --extern sysexits=build/o/libsysexits.rlib \ - -o $@ src/fop.rs +build/bin/fop: src/fop.rs build rustlibs + $(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/fop.rs .PHONY: hru hru: build/bin/hru -build/bin/hru: src/hru.rs build build/o/libgetopt.rlib build/o/libsysexits.rlib - $(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \ - --extern sysexits=build/o/libsysexits.rlib \ - -o $@ src/hru.rs +build/bin/hru: src/hru.rs build rustlibs + $(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/hru.rs .PHONY: intcmp intcmp: build/bin/intcmp @@ -88,10 +95,8 @@ build/bin/intcmp: src/intcmp.c build .PHONY: rpn rpn: build/bin/rpn -build/bin/rpn: src/rpn.rs build build/o/libsysexits.rlib - $(RUSTC) $(RUSTFLAGS) \ - --extern sysexits=build/o/libsysexits.rlib \ - -o $@ src/rpn.rs +build/bin/rpn: src/rpn.rs build rustlibs + $(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/rpn.rs .PHONY: scrut scrut: build/bin/scrut diff --git a/src/strerror.rs b/src/strerror.rs new file mode 100644 index 0000000..0939df9 --- /dev/null +++ b/src/strerror.rs @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 Emma Tebibyte + * SPDX-License-Identifier: FSFAP + * + * Copying and distribution of this file, with or without modification, are + * permitted in any medium without royalty provided the copyright notice and + * this notice are preserved. This file is offered as-is, without any warranty. + */ + +use std::ffi::{ c_int, c_char, CStr }; + +/* binding to strerror(3p) */ +extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } + +/* wrapper function for use in Rust */ +pub fn c_error(err: std::io::Error) -> String { + /* Get the raw OS error. If it’s None, what the hell is going on‽ */ + let error = err.raw_os_error().unwrap_or_else(|| { panic!() }) as c_int; + + /* Get a CStr from the error message so that it’s referenced and then + * convert it to an owned value. If the string is not valid UTF-8, return + * that error instead. */ + match unsafe { CStr::from_ptr(strerror(error)) }.to_str() { + Ok(s) => s.to_owned(), // yay!! :D + Err(e) => e.to_string(), // awww :( + } +} From 8f956d775c4006b688e3a194ecdff3d629c1c8e0 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:10:28 -0700 Subject: [PATCH 02/14] fop(1): changed to use strerror(3) --- src/fop.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index c9a767e..0e0b059 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -22,10 +22,12 @@ use std::{ process::{ Command, exit, Stdio }, }; -extern crate sysexits; extern crate getopt; +extern crate strerror; +extern crate sysexits; use getopt::{ Opt, Parser }; +use strerror::c_error; use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; fn main() { @@ -55,7 +57,7 @@ fn main() { }); let index = argv[index_arg].parse::().unwrap_or_else(|e| { - eprintln!("{}: {}: {}.", argv[0], argv[1], e); + eprintln!("{}: {}: {}", argv[0], argv[1], e); exit(EX_DATAERR); }); @@ -75,13 +77,13 @@ fn main() { .stdout(Stdio::piped()) .spawn() .unwrap_or_else( |e| { - eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e)); exit(EX_UNAVAILABLE); }); let field = fields.get(index).unwrap_or_else(|| { eprintln!( - "{}: {}: No such index in input.", + "{}: {}: No such index in input", argv[0], index.to_string(), ); @@ -94,7 +96,7 @@ fn main() { } let output = spawned.wait_with_output().unwrap_or_else(|e| { - eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e)); exit(EX_IOERR); }); @@ -103,7 +105,7 @@ fn main() { if replace.pop() != Some(b'\n') { replace = output.stdout; } let new_field = String::from_utf8(replace).unwrap_or_else(|e| { - eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], e); exit(EX_IOERR); }); @@ -111,8 +113,8 @@ fn main() { stdout().write_all( fields.join(&d.to_string()).as_bytes() - ).unwrap_or_else(|e|{ - eprintln!("{}: {}.", argv[0], e); + ).unwrap_or_else(|e| { + eprintln!("{}: {}", argv[0], c_error(e)); exit(EX_IOERR); }); } From cf1d16f8609c531e69cce229b4d4719c75854275 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:12:44 -0700 Subject: [PATCH 03/14] hru(1): changed to use strerror(3) --- src/hru.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index 0e0b25d..ad73070 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -23,8 +23,10 @@ use std::{ process::{ ExitCode, exit }, }; +extern crate strerror; extern crate sysexits; +use strerror::c_error; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; const LIST: [(u32, &str); 10] = [ @@ -49,7 +51,7 @@ fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { let c = match 10_u128.checked_pow(n) { Some(c) => c, None => { - return Err(format!("10^{}: Integer overflow.", n.to_string())); + return Err(format!("10^{}: Integer overflow", n.to_string())); }, }; @@ -79,7 +81,7 @@ fn main() -> ExitCode { f }, Err(err) => { - eprintln!("{}: {}.", argv[0], err); + eprintln!("{}: {}", argv[0], err); return ExitCode::from(EX_DATAERR as u8); }, }; @@ -87,7 +89,7 @@ fn main() -> ExitCode { let (number, prefix) = match convert(n) { Ok(x) => x, Err(err) => { - eprintln!("{}: {}.", argv[0], err); + eprintln!("{}: {}", argv[0], err); return ExitCode::from(EX_SOFTWARE as u8); }, }; @@ -98,7 +100,7 @@ fn main() -> ExitCode { stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) .unwrap_or_else(|e| { - eprintln!("{}: {}.", argv[0], e); + eprintln!("{}: {}", argv[0], c_error(e)); exit(EX_IOERR); }); } From 3e6dc5cc46114ba19077e6a0486b635ffdb49805 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:14:18 -0700 Subject: [PATCH 04/14] rpn(1): make error messages consistent with other tools --- src/rpn.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rpn.rs b/src/rpn.rs index 0cb23b8..8ccf5d6 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -52,8 +52,10 @@ use std::{ use CalcType::*; +extern crate strerror; extern crate sysexits; +use strerror::c_error; use sysexits::EX_DATAERR; #[derive(Clone, PartialEq, PartialOrd, Debug)] @@ -172,7 +174,7 @@ fn eval( }; } else { return Err(EvaluationError { - message: format!("{}: Unexpected operation.", op), + message: format!("{}: Unexpected operation", op), code: EX_DATAERR, }) } From ca01ca407496f68f71d7d8dd34310b5da1c3e3a3 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:14:58 -0700 Subject: [PATCH 05/14] rpn(1): remove erroneous strerror(3) dependency --- src/rpn.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rpn.rs b/src/rpn.rs index 8ccf5d6..2bfbbf5 100644 --- a/src/rpn.rs +++ b/src/rpn.rs @@ -52,10 +52,8 @@ use std::{ use CalcType::*; -extern crate strerror; extern crate sysexits; -use strerror::c_error; use sysexits::EX_DATAERR; #[derive(Clone, PartialEq, PartialOrd, Debug)] From 898044cd43915600da69be7d7c9f48fa6bcd36da Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 1 Mar 2024 23:51:36 -0700 Subject: [PATCH 06/14] Makefile: better macro assignment --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cb6bca6..8c22903 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ PREFIX=/usr/local -CC != command -v "$$CC" || printf 'cc\n' -RUSTC != command -v "$$RUSTC" || printf 'rustc\n' +CC?=cc +RUSTC?=rustc RUSTLIBS=--extern getopt=build/o/libgetopt.rlib \ --extern sysexits=build/o/libsysexits.rlib \ --extern strerror=build/o/libstrerror.rlib From c392dbc680d56d9e6f7338fe5cc0e47fd910782b Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 2 Mar 2024 10:16:32 -0700 Subject: [PATCH 07/14] Makefile: better deps; fop(1), hru(1), strerror(3): changed strerror wrapper function name --- Makefile | 2 +- src/fop.rs | 8 ++++---- src/hru.rs | 4 ++-- src/strerror.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8c22903..d5203f5 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ build/o/libstrerror.rlib: build src/strerror.rs $(RUSTC) $(RUSTFLAGS) --crate-type=lib -o $@ \ src/strerror.rs -build/o/libsysexits.rlib: build +build/o/libsysexits.rlib: build include/sysexits.h # bandage solution until bindgen(1) gets stdin support printf '#define EXIT_FAILURE 1\n' | cat - include/sysexits.h \ > build/include/sysexits.h diff --git a/src/fop.rs b/src/fop.rs index 0e0b059..65ee738 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -27,7 +27,7 @@ extern crate strerror; extern crate sysexits; use getopt::{ Opt, Parser }; -use strerror::c_error; +use strerror::raw_message; use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; fn main() { @@ -77,7 +77,7 @@ fn main() { .stdout(Stdio::piped()) .spawn() .unwrap_or_else( |e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e)); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], raw_message(e)); exit(EX_UNAVAILABLE); }); @@ -96,7 +96,7 @@ fn main() { } let output = spawned.wait_with_output().unwrap_or_else(|e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e)); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], raw_message(e)); exit(EX_IOERR); }); @@ -114,7 +114,7 @@ fn main() { stdout().write_all( fields.join(&d.to_string()).as_bytes() ).unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], c_error(e)); + eprintln!("{}: {}", argv[0], raw_message(e)); exit(EX_IOERR); }); } diff --git a/src/hru.rs b/src/hru.rs index ad73070..29e1e49 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -26,7 +26,7 @@ use std::{ extern crate strerror; extern crate sysexits; -use strerror::c_error; +use strerror::raw_message; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; const LIST: [(u32, &str); 10] = [ @@ -100,7 +100,7 @@ fn main() -> ExitCode { stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) .unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], c_error(e)); + eprintln!("{}: {}", argv[0], raw_message(e)); exit(EX_IOERR); }); } diff --git a/src/strerror.rs b/src/strerror.rs index 0939df9..117ed0b 100644 --- a/src/strerror.rs +++ b/src/strerror.rs @@ -13,7 +13,7 @@ use std::ffi::{ c_int, c_char, CStr }; extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } /* wrapper function for use in Rust */ -pub fn c_error(err: std::io::Error) -> String { +pub fn raw_message(err: std::io::Error) -> String { /* Get the raw OS error. If it’s None, what the hell is going on‽ */ let error = err.raw_os_error().unwrap_or_else(|| { panic!() }) as c_int; From 8193e471f01f176590bd66ce1269d5a974a40f67 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 3 Mar 2024 17:26:40 -0700 Subject: [PATCH 08/14] changes redundant .unwrap_or_else(panic!()) to .unwrap() --- src/strerror.rs | 4 ++-- src/test.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/test.rs diff --git a/src/strerror.rs b/src/strerror.rs index 117ed0b..01d98e2 100644 --- a/src/strerror.rs +++ b/src/strerror.rs @@ -15,12 +15,12 @@ extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } /* wrapper function for use in Rust */ pub fn raw_message(err: std::io::Error) -> String { /* Get the raw OS error. If it’s None, what the hell is going on‽ */ - let error = err.raw_os_error().unwrap_or_else(|| { panic!() }) as c_int; + let errno = err.raw_os_error().unwrap() as c_int; /* Get a CStr from the error message so that it’s referenced and then * convert it to an owned value. If the string is not valid UTF-8, return * that error instead. */ - match unsafe { CStr::from_ptr(strerror(error)) }.to_str() { + match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() { Ok(s) => s.to_owned(), // yay!! :D Err(e) => e.to_string(), // awww :( } diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..4a602b1 --- /dev/null +++ b/src/test.rs @@ -0,0 +1,10 @@ +extern crate strerror; + +use strerror::raw_message; + +fn main() { + stdout.write_all(b"meow\n").unwrap_or_else(|e| { + eprintln!("{}", raw_message(e)); + std::process::exit(1); + }); +} From 2ad7140e1ea8fcc3151c7716805af62719519756 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 9 Mar 2024 11:53:03 -0700 Subject: [PATCH 09/14] Makefile: DESTDIR --- Makefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index d5203f5..66572fc 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,12 @@ # notice are preserved. This file is offered as-is, without any warranty. .POSIX: + +# if using BSD make(1), remove these pragmas because they break it .PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1) .PRAGMA: command_comment # breaks without this? +DESTDIR=./dist PREFIX=/usr/local CC?=cc @@ -32,14 +35,13 @@ clean: rm -rf build/ dist/ dist: all - mkdir -p dist/bin dist/share/man/man1 - cp build/bin/* dist/bin/ - cp docs/*.1 dist/share/man/man1/ + mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/man/man1 + cp build/bin/* $(DESTDIR)$(PREFIX)/bin/ + cp docs/*.1 $(DESTDIR)$(PREFIX)/share/man/man1/ .PHONY: install install: dist - mkdir -p $(PREFIX) - cp -r dist/* $(PREFIX)/ + cp -r $(DESTDIR)/* / .PHONY: test test: build From b2d56bbc9abd4f8fc8a991bcd230b713a9575d9f Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 9 Mar 2024 22:02:19 -0700 Subject: [PATCH 10/14] Makefile: even more changes --- Makefile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 66572fc..dd77985 100644 --- a/Makefile +++ b/Makefile @@ -16,11 +16,15 @@ DESTDIR=./dist PREFIX=/usr/local +SYSEXITS!=printf '\043include \n' | cpp -M - | sed 's/ /\n/g' \ + | sed -n 's/sysexits\.h//p' || printf 'include/\n' + CC?=cc RUSTC?=rustc RUSTLIBS=--extern getopt=build/o/libgetopt.rlib \ --extern sysexits=build/o/libsysexits.rlib \ --extern strerror=build/o/libstrerror.rlib +CFLAGS+=-I$(SYSEXITS) .PHONY: all all: dj false fop hru intcmp rpn scrut str strcmp true @@ -60,15 +64,12 @@ build/o/libstrerror.rlib: build src/strerror.rs $(RUSTC) $(RUSTFLAGS) --crate-type=lib -o $@ \ src/strerror.rs -build/o/libsysexits.rlib: build include/sysexits.h +build/o/libsysexits.rlib: build $(SYSEXITS)sysexits.h # bandage solution until bindgen(1) gets stdin support - printf '#define EXIT_FAILURE 1\n' | cat - include/sysexits.h \ + printf '#define EXIT_FAILURE 1\n' | cat - $(SYSEXITS)sysexits.h \ > build/include/sysexits.h bindgen --default-macro-constant-type signed --use-core --formatter=none \ - "$$(printf '#include \n' \ - | cpp -M -idirafter "build/include" - \ - | sed 's/ /\n/g' | grep sysexits.h)" \ - | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ - + build/include/sysexits.h | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ - .PHONY: dj dj: build/bin/dj From b356ac522fe9168337669f3c2a9e0b30163a5803 Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 18 Mar 2024 20:45:53 -0600 Subject: [PATCH 11/14] strerror(3): changed panic to 0 --- src/strerror.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strerror.rs b/src/strerror.rs index 01d98e2..e5a2a95 100644 --- a/src/strerror.rs +++ b/src/strerror.rs @@ -15,7 +15,7 @@ extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } /* wrapper function for use in Rust */ pub fn raw_message(err: std::io::Error) -> String { /* Get the raw OS error. If it’s None, what the hell is going on‽ */ - let errno = err.raw_os_error().unwrap() as c_int; + let errno = err.raw_os_error().unwrap_or(0) as c_int; /* Get a CStr from the error message so that it’s referenced and then * convert it to an owned value. If the string is not valid UTF-8, return From 1891c3e1aa83c92c8bd644541d13441f30b55c0c Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 18 Mar 2024 21:30:43 -0600 Subject: [PATCH 12/14] strerror(3): created strerror() method --- src/strerror.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/strerror.rs b/src/strerror.rs index e5a2a95..e306e7a 100644 --- a/src/strerror.rs +++ b/src/strerror.rs @@ -9,19 +9,23 @@ use std::ffi::{ c_int, c_char, CStr }; -/* binding to strerror(3p) */ -extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } +pub trait StrError { fn strerror(&self) -> String; } -/* wrapper function for use in Rust */ -pub fn raw_message(err: std::io::Error) -> String { - /* Get the raw OS error. If it’s None, what the hell is going on‽ */ - let errno = err.raw_os_error().unwrap_or(0) as c_int; +impl StrError for std::io::Error { + /* wrapper function for use in Rust */ + fn strerror(&self) -> String { + /* Get the raw OS error. If it’s None, what the hell is going on‽ */ + let errno = self.raw_os_error().unwrap_or(0) as c_int; - /* Get a CStr from the error message so that it’s referenced and then - * convert it to an owned value. If the string is not valid UTF-8, return - * that error instead. */ - match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() { - Ok(s) => s.to_owned(), // yay!! :D - Err(e) => e.to_string(), // awww :( + /* Get a CStr from the error message so that it’s referenced and then + * convert it to an owned value. If the string is not valid UTF-8, + * return that error instead. */ + match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() { + Ok(s) => s.to_owned(), // yay!! :D + Err(e) => e.to_string(), // awww :( + } } } + +/* binding to strerror(3p) */ +extern "C" { fn strerror(errnum: c_int) -> *mut c_char; } From d87b5d095870cb0874629ebcda13f069e1c4014f Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 18 Mar 2024 21:31:25 -0600 Subject: [PATCH 13/14] hru(1), fop(1): changed to reflect strerror changes --- src/fop.rs | 8 ++++---- src/hru.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 65ee738..b829602 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -27,7 +27,7 @@ extern crate strerror; extern crate sysexits; use getopt::{ Opt, Parser }; -use strerror::raw_message; +use strerror::StrError; use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; fn main() { @@ -77,7 +77,7 @@ fn main() { .stdout(Stdio::piped()) .spawn() .unwrap_or_else( |e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], raw_message(e)); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror()); exit(EX_UNAVAILABLE); }); @@ -96,7 +96,7 @@ fn main() { } let output = spawned.wait_with_output().unwrap_or_else(|e| { - eprintln!("{}: {}: {}", argv[0], argv[command_arg], raw_message(e)); + eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror()); exit(EX_IOERR); }); @@ -114,7 +114,7 @@ fn main() { stdout().write_all( fields.join(&d.to_string()).as_bytes() ).unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], raw_message(e)); + eprintln!("{}: {}", argv[0], e.strerror()); exit(EX_IOERR); }); } diff --git a/src/hru.rs b/src/hru.rs index 29e1e49..b7937f7 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -26,7 +26,7 @@ use std::{ extern crate strerror; extern crate sysexits; -use strerror::raw_message; +use strerror::StrError; use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE }; const LIST: [(u32, &str); 10] = [ @@ -100,7 +100,7 @@ fn main() -> ExitCode { stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) .unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], raw_message(e)); + eprintln!("{}: {}", argv[0], e.strerror()); exit(EX_IOERR); }); } From b503d976256e5100809ade0fff6deff1535e5461 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 24 Mar 2024 13:28:42 -0600 Subject: [PATCH 14/14] Makefile: directory specification changes --- Makefile | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index dd77985..3a9c844 100644 --- a/Makefile +++ b/Makefile @@ -13,18 +13,18 @@ .PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1) .PRAGMA: command_comment # breaks without this? -DESTDIR=./dist -PREFIX=/usr/local +DESTDIR ?= dist +PREFIX ?= /usr/local -SYSEXITS!=printf '\043include \n' | cpp -M - | sed 's/ /\n/g' \ - | sed -n 's/sysexits\.h//p' || printf 'include/\n' +SYSEXITS != printf '\043include \n' | cpp -M - | sed 's/ /\n/g' \ + | sed -n 's/sysexits\.h//p' || printf 'include\n' -CC?=cc -RUSTC?=rustc -RUSTLIBS=--extern getopt=build/o/libgetopt.rlib \ +CC ?= cc +RUSTC ?= rustc +RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \ --extern sysexits=build/o/libsysexits.rlib \ --extern strerror=build/o/libstrerror.rlib -CFLAGS+=-I$(SYSEXITS) +CFLAGS += -I$(SYSEXITS) .PHONY: all all: dj false fop hru intcmp rpn scrut str strcmp true @@ -36,12 +36,12 @@ build: .PHONY: clean clean: - rm -rf build/ dist/ + rm -rf build dist dist: all - mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/man/man1 - cp build/bin/* $(DESTDIR)$(PREFIX)/bin/ - cp docs/*.1 $(DESTDIR)$(PREFIX)/share/man/man1/ + mkdir -p $(DESTDIR)/$(PREFIX)/bin $(DESTDIR)/$(PREFIX)/share/man/man1 + cp build/bin/* $(DESTDIR)/$(PREFIX)/bin + cp docs/*.1 $(DESTDIR)/$(PREFIX)/share/man/man1 .PHONY: install install: dist