From 7533df86e00042926af4a67f1b81bd555490bf9b Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 6 Sep 2021 18:54:40 +0300 Subject: [PATCH] Fix compilation for netbsd-9.2 $ rustc -V rustc 1.52.1 $ cargo -V cargo 1.52.0 Pre-requisite steps needed for build: - Needed to install mozilla certs - Needed to set OPENSSL_DIR=/usr --- melib/src/datetime.rs | 31 +++++++++++++++++++++++++++++++ melib/src/lib.rs | 1 + src/conf.rs | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/melib/src/datetime.rs b/melib/src/datetime.rs index 124d6486..eaea9e9a 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 dc651c22..6bac3218 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 7c35d97b..2c4760ca 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();