2024-03-01 23:04:53 -07:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
|
|
|
|
* 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 */
|
2024-03-02 10:16:32 -07:00
|
|
|
|
pub fn raw_message(err: std::io::Error) -> String {
|
2024-03-01 23:04:53 -07:00
|
|
|
|
/* Get the raw OS error. If it’s None, what the hell is going on‽ */
|
2024-03-03 17:26:40 -07:00
|
|
|
|
let errno = err.raw_os_error().unwrap() as c_int;
|
2024-03-01 23:04:53 -07:00
|
|
|
|
|
|
|
|
|
/* 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. */
|
2024-03-03 17:26:40 -07:00
|
|
|
|
match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() {
|
2024-03-01 23:04:53 -07:00
|
|
|
|
Ok(s) => s.to_owned(), // yay!! :D
|
|
|
|
|
Err(e) => e.to_string(), // awww :(
|
|
|
|
|
}
|
|
|
|
|
}
|