Added strerror(3) as a Rust library
#76
Loading…
x
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=ccRUSTC=rustcCC?=ccI 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.
errormessageoremsgor 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
errorshould 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
erris not a number.This is not C, and error is the internal value of an
Errvariant of aResult, which is anio::Error. Passing this function anio::Errorthat has no correspondingRawOsErroris 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
errorvariable toerrnoif it’ll be more understandable to you.I'm glad you showed me the strerrror(3p) locale thing.
In person we discussed
errnobeing 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
0work 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
Stringderived from theRawOsErroror, in the case that the locale-specified error string is not valid UTF-8, returns aStringderived 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=./distFor 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$PWDanyway).@ -32,3 +42,1 @@mkdir -p dist/bin dist/share/man/man1cp build/bin/* dist/bin/cp docs/*.1 dist/share/man/man1/mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/man/man1Consecutive directory delimiters are ignored when evaluating UNIX paths;
///is an unnecessarily wordy equivalent to/when used as a pathname.$(DESTDIR)/$(PREFIX)/binand so on is preferable, though, to ensure that hasty modifications won't write to adistbuildbinor something. Mistakes happen!The names
PREFIXandDESTDIRsound equivalent to me. On face value I would expect the prefix to come first.Historically,
$DESTDIRis a directory inside the tree (in this case,dist/) to which programs are installed in a phony root filesystem. Then, whenmake installis called, the$PREFIXis already baked into$DESTDIRand you can copy it straight into the root filesystem.@ -37,3 +47,2 @@install: distmkdir -p $(PREFIX)cp -r dist/* $(PREFIX)/cp -r $(DESTDIR)/* /Installation to a given
PREFIXshould 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