diff --git a/melib/src/datetime.rs b/melib/src/datetime.rs index 124d64866..eaea9e9a0 100644 --- a/melib/src/datetime.rs +++ b/melib/src/datetime.rs @@ -73,22 +73,34 @@ extern "C" { fn gettimeofday(tv: *mut libc::timeval, tz: *mut libc::timezone) -> i32; } +#[cfg(not(target_os = "netbsd"))] struct Locale { new_locale: libc::locale_t, old_locale: libc::locale_t, } +#[cfg(target_os = "netbsd")] +struct Locale { + mask: std::os::raw::c_int, + old_locale: *const std::os::raw::c_char, +} impl Drop for Locale { fn drop(&mut self) { + #[cfg(not(target_os = "netbsd"))] unsafe { let _ = libc::uselocale(self.old_locale); libc::freelocale(self.new_locale); } + #[cfg(target_os = "netbsd")] + unsafe { + let _ = libc::setlocale(self.mask, self.old_locale); + } } } // How to unit test this? Test machine is not guaranteed to have non-english locales. impl Locale { + #[cfg(not(target_os = "netbsd"))] fn new( mask: std::os::raw::c_int, locale: *const std::os::raw::c_char, @@ -108,6 +120,25 @@ impl Locale { old_locale, }) } + #[cfg(target_os = "netbsd")] + fn new( + mask: std::os::raw::c_int, + locale: *const std::os::raw::c_char, + _base: libc::locale_t, + ) -> Result { + let old_locale = unsafe { libc::setlocale(mask, std::ptr::null_mut()) }; + if old_locale.is_null() { + return Err(nix::Error::last().into()); + } + let new_locale = unsafe { libc::setlocale(mask, locale) }; + if new_locale.is_null() { + return Err(nix::Error::last().into()); + } + Ok(Locale { + mask, + old_locale, + }) + } } pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>, posix: bool) -> String { diff --git a/melib/src/lib.rs b/melib/src/lib.rs index dc651c225..6bac3218e 100644 --- a/melib/src/lib.rs +++ b/melib/src/lib.rs @@ -181,6 +181,7 @@ pub mod shellexpand { use smallvec::SmallVec; use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; + #[cfg(not(target_os = "netbsd"))] use std::os::unix::io::AsRawFd; use std::path::{Path, PathBuf}; diff --git a/src/conf.rs b/src/conf.rs index 7c35d97bc..2c4760cad 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1154,7 +1154,7 @@ identity="username@hostname.local" let mut buf = [0u8; 16]; f.read_exact(&mut buf)?; let mut filename = String::with_capacity(2 * 16); - for byte in buf { + for byte in &buf { write!(&mut filename, "{:02X}", byte).unwrap(); } let mut path = std::env::temp_dir();