Added strerror(3)
as a Rust library
#76
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "strerror"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This makes Rust error messages consistent with C error messages.
See notes.
@ -15,3 +15,2 @@
CC=cc
RUSTC=rustc
CC?=cc
I really like these Makefile changes.
@ -0,0 +13,4 @@
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 {
Don't name things after implementation details because implementations change.
errormessage
oremsg
or something would be better.emessage
? I don't know.@ -0,0 +15,4 @@
/* 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;
For consistency with C conventions
error
should beerrno
.Instead of panicking, set errno to 0. POSIX requires strerror(3p) to have useful function when its argument is 0.
To save work with inevitable localization stuff it may be wise to use strerror_l(3p) instead with the local locale - if we want to support locales.
This is not C and
err
is not a number.This is not C, and error is the internal value of an
Err
variant of aResult
, which is anio::Error
. Passing this function anio::Error
that has no correspondingRawOsError
is not the equivalent of passing it0
, it’s more like passing the function a null terminator.From strerror(3p):
I misunderstood. I will change the
error
variable toerrno
if it’ll be more understandable to you.I'm glad you showed me the strerrror(3p) locale thing.
In person we discussed
errno
being fine and thepanic!
being technically unreachable - though I still believe 0 should be passed to strerror(3) instead of a panic.I have decided to make
0
work but as soon as we can put manual compiler errors in the library we will do that.@ -0,0 +20,4 @@
/* 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() {
You should note somewhere that this can return an empty string in case of an error.
It cannot do this. It either returns a
String
derived from theRawOsError
or, in the case that the locale-specified error string is not valid UTF-8, returns aString
derived from aUtf8Error
.I"ve suggested some changes but I really like the intent.
@ -11,3 +13,4 @@
.PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1)
.PRAGMA: command_comment # breaks without this?
DESTDIR=./dist
For readability, spaces between the assignment operators and their names and values would be nice. e.g.
DESTDIR = ./dist
(though I would forego the./
as Makefiles are conventionally run relative to$PWD
anyway).@ -32,3 +42,1 @@
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
Consecutive directory delimiters are ignored when evaluating UNIX paths;
///
is an unnecessarily wordy equivalent to/
when used as a pathname.$(DESTDIR)/$(PREFIX)/bin
and so on is preferable, though, to ensure that hasty modifications won't write to adistbuildbin
or something. Mistakes happen!The names
PREFIX
andDESTDIR
sound equivalent to me. On face value I would expect the prefix to come first.Historically,
$DESTDIR
is a directory inside the tree (in this case,dist/
) to which programs are installed in a phony root filesystem. Then, whenmake install
is called, the$PREFIX
is already baked into$DESTDIR
and you can copy it straight into the root filesystem.@ -37,3 +47,2 @@
install: dist
mkdir -p $(PREFIX)
cp -r dist/* $(PREFIX)/
cp -r $(DESTDIR)/* /
Installation to a given
PREFIX
should be possible.That is what this does
@trinity if you have no more questions, may you approve this for merging?
@ -0,0 +1,10 @@
extern crate strerror;
What's the purpose of this file?
Mistake.
@trinity: in the future, do not make commits without PGP signing them, and do not merge main into branches that have pull requests open to main.
I think I misclicked, I don't remember doing that and I wouldn't commit to a branch I didn't create. Sorry about that.
@ -1,76 +0,0 @@
.\" Copyright (c) 2024 DTB <trinity@trinity.moe>
Would merging this branch nuke mm(1)?
I think so, I can fix it though. Just approve it whenever you think it’s ready.
Merged.
I gave the okay verbally as I was too tired to approve it digitally.
Pull request closed