From 7ef9641eeba53679a34ee9bad60aa99b89b4b9b5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 27 Mar 2026 12:25:52 +0100 Subject: [PATCH] refactor: replace nix::libc re-exports with direct libc imports Replace all `nix::libc::*` usages with direct `libc::*` imports to decouple libc access from the nix crate, as preparation for migrating from nix to rustix. Add direct libc dependency to crates that previously relied on nix's libc re-export: stty, env, sort, date, mknod, touch. --- Cargo.lock | 4 ++++ src/uu/env/Cargo.toml | 1 + src/uu/env/src/env.rs | 2 -- src/uu/mknod/Cargo.toml | 1 + src/uu/mknod/src/mknod.rs | 2 +- src/uu/sort/Cargo.toml | 1 + src/uu/sort/src/sort.rs | 3 --- src/uu/stty/Cargo.toml | 1 + src/uu/stty/src/stty.rs | 10 +++++----- src/uucore/src/lib/features/safe_traversal.rs | 1 - src/uucore/src/lib/features/signals.rs | 3 --- src/uucore/src/lib/mods/panic.rs | 2 -- tests/by-util/test_env.rs | 2 -- 13 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b6c0bfaf..9a95b8d0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3543,6 +3543,7 @@ version = "0.8.0" dependencies = [ "clap", "fluent", + "libc", "nix", "rust-ini", "thiserror 2.0.18", @@ -3797,6 +3798,7 @@ version = "0.8.0" dependencies = [ "clap", "fluent", + "libc", "nix", "uucore", ] @@ -4155,6 +4157,7 @@ dependencies = [ "fluent", "foldhash 0.2.0", "itertools 0.14.0", + "libc", "memchr", "nix", "rand 0.10.1", @@ -4215,6 +4218,7 @@ dependencies = [ "cfg_aliases", "clap", "fluent", + "libc", "nix", "uucore", ] diff --git a/src/uu/env/Cargo.toml b/src/uu/env/Cargo.toml index 46aa1be7c..eaa64eab6 100644 --- a/src/uu/env/Cargo.toml +++ b/src/uu/env/Cargo.toml @@ -26,6 +26,7 @@ uucore = { workspace = true, features = ["signals"] } fluent = { workspace = true } [target.'cfg(unix)'.dependencies] +libc = { workspace = true } nix = { workspace = true, features = ["signal"] } [[bin]] diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 334354425..1dae336cf 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -18,8 +18,6 @@ use native_int_str::{ Convert, NCvt, NativeIntStr, NativeIntString, NativeStr, from_native_int_representation_owned, }; #[cfg(unix)] -use nix::libc; -#[cfg(unix)] use nix::sys::signal::{ SigHandler::{SigDfl, SigIgn}, SigSet, SigmaskHow, Signal, signal, sigprocmask, diff --git a/src/uu/mknod/Cargo.toml b/src/uu/mknod/Cargo.toml index 03ad05b9d..8583bfb1e 100644 --- a/src/uu/mknod/Cargo.toml +++ b/src/uu/mknod/Cargo.toml @@ -24,6 +24,7 @@ doctest = false clap = { workspace = true } uucore = { workspace = true, features = ["mode", "fs"] } fluent = { workspace = true } +libc = { workspace = true } nix = { workspace = true } [features] diff --git a/src/uu/mknod/src/mknod.rs b/src/uu/mknod/src/mknod.rs index 39ac62cb6..c8b3a33e0 100644 --- a/src/uu/mknod/src/mknod.rs +++ b/src/uu/mknod/src/mknod.rs @@ -6,7 +6,7 @@ // spell-checker:ignore (ToDO) parsemode makedev sysmacros perror IFBLK IFCHR IFIFO sflag use clap::{Arg, ArgAction, Command, value_parser}; -use nix::libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t}; +use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t}; use nix::sys::stat::{Mode, SFlag, mknod as nix_mknod, umask as nix_umask}; use uucore::display::Quotable; diff --git a/src/uu/sort/Cargo.toml b/src/uu/sort/Cargo.toml index 09763a4f9..8deab232e 100644 --- a/src/uu/sort/Cargo.toml +++ b/src/uu/sort/Cargo.toml @@ -51,6 +51,7 @@ foldhash = { workspace = true } ctrlc = { workspace = true } [target.'cfg(unix)'.dependencies] +libc = { workspace = true } nix = { workspace = true, features = ["resource"] } [dev-dependencies] diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 8b321b55d..8f46439d0 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1410,8 +1410,6 @@ pub(crate) fn fd_soft_limit() -> Option { #[cfg(unix)] pub(crate) fn current_open_fd_count() -> Option { - use nix::libc; - fn count_dir(path: &str) -> Option { let entries = std::fs::read_dir(path).ok()?; let mut count = 0usize; @@ -1770,7 +1768,6 @@ fn locale_failed_to_set() -> bool { #[cfg(unix)] fn locale_failed_to_set() -> bool { - use nix::libc; unsafe { libc::setlocale(libc::LC_COLLATE, c"".as_ptr()).is_null() } } diff --git a/src/uu/stty/Cargo.toml b/src/uu/stty/Cargo.toml index e1fb8c5ba..0f9be60cc 100644 --- a/src/uu/stty/Cargo.toml +++ b/src/uu/stty/Cargo.toml @@ -24,6 +24,7 @@ uucore = { workspace = true, features = ["parser"] } fluent = { workspace = true } [target.'cfg(unix)'.dependencies] +libc = { workspace = true } nix = { workspace = true, features = ["ioctl", "term"] } [[bin]] diff --git a/src/uu/stty/src/stty.rs b/src/uu/stty/src/stty.rs index 686f00a82..bde8054e9 100644 --- a/src/uu/stty/src/stty.rs +++ b/src/uu/stty/src/stty.rs @@ -19,14 +19,14 @@ mod flags; use crate::flags::AllFlags; use crate::flags::COMBINATION_SETTINGS; use clap::{Arg, ArgAction, ArgMatches, Command}; -use nix::libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort}; +use libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort}; #[cfg(all( target_os = "linux", not(target_arch = "powerpc"), not(target_arch = "powerpc64") ))] -use nix::libc::{TCGETS2, termios2}; +use libc::{TCGETS2, termios2}; use nix::sys::termios::{ ControlFlags, InputFlags, LocalFlags, OutputFlags, SetArg, SpecialCharacterIndices as S, @@ -520,7 +520,7 @@ fn parse_rows_cols(arg: &str) -> Option { /// - Returns `None` if format is invalid fn parse_saved_state(arg: &str) -> Option> { let parts: Vec<&str> = arg.split(':').collect(); - let expected_parts = 4 + nix::libc::NCCS; + let expected_parts = 4 + libc::NCCS; // GNU requires exactly the right number of parts for this platform if parts.len() != expected_parts { @@ -692,7 +692,7 @@ fn print_terminal_size( { // For some reason the normal nix Termios struct does not expose the line, // so we get the underlying libc::termios struct to get that information. - let libc_termios: nix::libc::termios = termios.clone().into(); + let libc_termios: libc::termios = termios.clone().into(); let line = libc_termios.c_line; printer.print(&translate!("stty-output-line", "line" => line)); } @@ -824,7 +824,7 @@ fn string_to_flag(option: &str) -> Option> { None } -fn control_char_to_string(cc: nix::libc::cc_t) -> nix::Result { +fn control_char_to_string(cc: libc::cc_t) -> nix::Result { if cc == 0 { return Ok(translate!("stty-output-undef")); } diff --git a/src/uucore/src/lib/features/safe_traversal.rs b/src/uucore/src/lib/features/safe_traversal.rs index 98fc81dd9..f8aaa1609 100644 --- a/src/uucore/src/lib/features/safe_traversal.rs +++ b/src/uucore/src/lib/features/safe_traversal.rs @@ -24,7 +24,6 @@ use std::path::{Path, PathBuf}; use nix::dir::Dir; use nix::fcntl::{OFlag, openat}; -use nix::libc; use nix::sys::stat::{FchmodatFlags, FileStat, Mode, fchmodat, fstatat, mkdirat}; use nix::unistd::{Gid, Uid, UnlinkatFlags, fchown, fchownat, unlinkat}; use os_display::Quotable; diff --git a/src/uucore/src/lib/features/signals.rs b/src/uucore/src/lib/features/signals.rs index 275f7acd2..44d53a016 100644 --- a/src/uucore/src/lib/features/signals.rs +++ b/src/uucore/src/lib/features/signals.rs @@ -12,8 +12,6 @@ #[cfg(unix)] use nix::errno::Errno; -#[cfg(any(target_os = "linux", target_os = "android"))] -use nix::libc; #[cfg(unix)] use nix::sys::signal::{ SaFlags, SigAction, SigHandler, SigHandler::SigDfl, SigHandler::SigIgn, SigSet, Signal, @@ -548,7 +546,6 @@ static STARTUP_STATE_WAS_CAPTURED: AtomicBool = AtomicBool::new(false); #[cfg(unix)] #[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn capture_startup_state() { - use nix::libc; use std::mem::MaybeUninit; use std::ptr; diff --git a/src/uucore/src/lib/mods/panic.rs b/src/uucore/src/lib/mods/panic.rs index 3df3c4f19..f7f00f0c0 100644 --- a/src/uucore/src/lib/mods/panic.rs +++ b/src/uucore/src/lib/mods/panic.rs @@ -49,8 +49,6 @@ pub fn mute_sigpipe_panic() { /// variable. If set to "default", it restores SIGPIPE to SIG_DFL. #[cfg(unix)] pub fn preserve_inherited_sigpipe() { - use nix::libc; - // Check if parent specified that SIGPIPE should be default if std::env::var_os("RUST_SIGPIPE").is_some_and(|v| v == "default") { unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) }; diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 1a1d504a1..2726120e3 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -5,8 +5,6 @@ // spell-checker:ignore (words) bamf chdir rlimit prlimit COMSPEC cout cerr FFFD winsize xpixel ypixel Secho sighandler #![allow(clippy::missing_errors_doc)] -#[cfg(unix)] -use nix::libc; #[cfg(unix)] use nix::sys::signal::Signal; #[cfg(feature = "echo")]