Skip to content

Commit 5340b68

Browse files
Cleaned up platform-specific code
1 parent 6e09ec0 commit 5340b68

File tree

22 files changed

+142
-172
lines changed

22 files changed

+142
-172
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

file/find.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ fn parse_expression(tokens: &mut Vec<&str>) -> Vec<Expr> {
149149
"-size" => {
150150
tokens.pop();
151151
if let Some(size) = tokens.pop() {
152-
let (size, in_bytes) = if size.ends_with('c') {
153-
(size[..size.len() - 1].parse::<u64>().unwrap_or(0), true)
152+
let (size, in_bytes) = if let Some(st) = size.strip_suffix('c') {
153+
(st.parse::<u64>().unwrap_or(0), true)
154154
} else {
155155
(size.parse::<u64>().unwrap_or(0), false)
156156
};
@@ -250,15 +250,15 @@ fn evaluate_expression(
250250
match expression {
251251
Expr::Not(inner) => {
252252
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
253-
not_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
253+
not_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
254254
}
255255
Expr::Or(inner) => {
256256
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
257-
or_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
257+
or_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
258258
}
259259
Expr::And(inner) => {
260260
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
261-
and_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
261+
and_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
262262
}
263263
_ => {}
264264
}
@@ -307,7 +307,7 @@ fn evaluate_expression(
307307
FileType::Fifo => file_type.is_fifo(),
308308
FileType::File => file_type.is_file(),
309309
FileType::Socket => file_type.is_socket(),
310-
FileType::Unknown => return Err(format!("Unknown argument to -type")),
310+
FileType::Unknown => return Err("Unknown argument to -type".to_owned()),
311311
};
312312
if !r {
313313
c_files.remove(file.path());
@@ -316,15 +316,15 @@ fn evaluate_expression(
316316
Expr::NoUser => {
317317
if let Ok(metadata) = file.metadata() {
318318
let uid = metadata.uid();
319-
if !users::get_user_by_uid(uid).is_none() {
319+
if users::get_user_by_uid(uid).is_some() {
320320
c_files.remove(file.path());
321321
}
322322
}
323323
}
324324
Expr::NoGroup => {
325325
if let Ok(metadata) = file.metadata() {
326326
let gid = metadata.gid();
327-
if !users::get_group_by_gid(gid).is_none() {
327+
if users::get_group_by_gid(gid).is_some() {
328328
c_files.remove(file.path());
329329
}
330330
}

ftw/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ enum ProcessFileResult {
422422
}
423423

424424
fn process_file<F, H>(
425-
path_stack: &Vec<Rc<[libc::c_char]>>,
425+
path_stack: &[Rc<[libc::c_char]>],
426426
dir_fd: &FileDescriptor,
427427
entry_filename: Rc<[libc::c_char]>,
428428
follow_symlinks: bool,
@@ -439,7 +439,7 @@ where
439439
Ok(md) => md,
440440
Err(e) => {
441441
err_reporter(
442-
Entry::new(dir_fd, &path_stack, &entry_filename, None),
442+
Entry::new(dir_fd, path_stack, &entry_filename, None),
443443
Error::new(e, ErrorKind::Stat),
444444
);
445445
return ProcessFileResult::NotProcessed;
@@ -583,7 +583,7 @@ where
583583
Err(e) => {
584584
if let Some(path_stack) = &path_stack {
585585
err_reporter(
586-
Entry::new(&starting_dir, &path_stack, &filename, None),
586+
Entry::new(&starting_dir, path_stack, &filename, None),
587587
Error::new(e, ErrorKind::Open),
588588
);
589589
}

gettext-rs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn textdomain<T: Into<Vec<u8>>>(domainname: T) -> Result<Vec<u8>, std::io::E
1818
}
1919

2020
pub fn gettext<T: Into<String>>(msgid: T) -> String {
21-
return msgid.into();
21+
msgid.into()
2222
}
2323

2424
#[macro_export]

m4/test-manager/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn update_snapshots(args: &Args, update: &UpdateSnapshots) {
6868
return false;
6969
}
7070

71-
if let Some(name) = update.test_case_name.as_ref().map(|s| s.as_str()) {
71+
if let Some(name) = update.test_case_name.as_deref() {
7272
if name != entry.path().file_stem().unwrap().to_str().unwrap() {
7373
return false;
7474
}

plib/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT"
77
repository = "https://github.com/rustcoreutils/posixutils-rs.git"
88

99
[dependencies]
10+
cfg-if = "1.0"
1011
libc.workspace = true
1112

1213
[lib]

plib/src/io.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ use std::path::PathBuf;
1313

1414
pub fn input_stream(pathname: &PathBuf, dashed_stdin: bool) -> io::Result<Box<dyn Read>> {
1515
// open file, or stdin
16-
let file: Box<dyn Read>;
1716
let path_str = pathname.as_os_str();
18-
if dashed_stdin && path_str == "-" {
19-
file = Box::new(io::stdin().lock());
20-
} else if !dashed_stdin && path_str == "" {
21-
file = Box::new(io::stdin().lock());
22-
} else {
23-
file = Box::new(fs::File::open(pathname)?);
24-
}
17+
18+
let file: Box<dyn Read> =
19+
if (dashed_stdin && path_str == "-") || (!dashed_stdin && path_str.is_empty()) {
20+
Box::new(io::stdin().lock())
21+
} else {
22+
Box::new(fs::File::open(pathname)?)
23+
};
2524

2625
Ok(file)
2726
}
2827

2928
pub fn input_stream_opt(pathname: &Option<PathBuf>) -> io::Result<Box<dyn Read>> {
3029
match pathname {
31-
Some(path) => input_stream(&path, false),
30+
Some(path) => input_stream(path, false),
3231
None => input_stream(&PathBuf::new(), false),
3332
}
3433
}

plib/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
pub mod curuser;
1111
pub mod group;
1212
pub mod io;
13-
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
14-
pub mod libc_aliases;
1513
pub mod lzw;
1614
pub mod modestr;
15+
pub mod platform;
1716
pub mod sccsfile;
1817
pub mod testing;
1918
pub mod utmpx;

plib/src/libc_aliases.rs

-10
This file was deleted.

plib/src/libc_aliases/musl.rs

-22
This file was deleted.

plib/src/platform.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// TODO
2+
// Avoid restating local alias names
3+
cfg_if::cfg_if! {
4+
if #[cfg(target_env = "musl")] {
5+
// https://git.musl-libc.org/cgit/musl/tree/include/utmpx.h?id=1e7f0fcd7ff2096904fd93a2ee6d12a2392be392
6+
pub const EMPTY: libc::c_short = 0_i16;
7+
pub const RUN_LVL: libc::c_short = 1_i16;
8+
pub const BOOT_TIME: libc::c_short = 2_i16;
9+
pub const NEW_TIME: libc::c_short = 3_i16;
10+
pub const OLD_TIME: libc::c_short = 4_i16;
11+
pub const INIT_PROCESS: libc::c_short = 5_i16;
12+
pub const LOGIN_PROCESS: libc::c_short = 6_i16;
13+
pub const USER_PROCESS: libc::c_short = 7_i16;
14+
pub const DEAD_PROCESS: libc::c_short = 8_i16;
15+
16+
// Remove when https://github.com/rust-lang/libc/issues/3190 is resolved
17+
// https://github.com/rust-lang/libc/commit/e3caaf6b0ea08ae294e25a861022c256a7535ec4#diff-5822a2981791fb0bb7689a921abdc2133cc73116ee125eabefad3a9374056b7a
18+
extern "C" {
19+
pub fn getutxent() -> *mut libc::utmpx;
20+
pub fn getutxid(ut: *const libc::utmpx) -> *mut libc::utmpx;
21+
pub fn getutxline(ut: *const libc::utmpx) -> *mut libc::utmpx;
22+
pub fn pututxline(ut: *const libc::utmpx) -> *mut libc::utmpx;
23+
pub fn setutxent();
24+
pub fn endutxent();
25+
}
26+
27+
type LocalPIoctlOp = libc::c_int;
28+
type LocalPPriorityWhichT = libc::c_int;
29+
} else {
30+
pub use libc::{
31+
endutxent,
32+
getutxent,
33+
setutxent,
34+
BOOT_TIME,
35+
DEAD_PROCESS,
36+
EMPTY,
37+
INIT_PROCESS,
38+
LOGIN_PROCESS,
39+
NEW_TIME,
40+
OLD_TIME,
41+
RUN_LVL,
42+
USER_PROCESS,
43+
};
44+
45+
type LocalPIoctlOp = libc::c_ulong;
46+
47+
cfg_if::cfg_if! {
48+
if #[cfg(target_os = "macos")] {
49+
type LocalPPriorityWhichT = libc::c_int;
50+
} else {
51+
type LocalPPriorityWhichT = libc::__priority_which_t;
52+
}
53+
}
54+
}
55+
}
56+
57+
pub type PIoctlOp = LocalPIoctlOp;
58+
pub type PPriorityWhichT = LocalPPriorityWhichT;

plib/src/sccsfile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ fn parse_deltas(lines: &[&str]) -> Result<Vec<SccsDelta>, &'static str> {
167167
comments: String::new(),
168168
});
169169
} else if in_delta_section {
170-
if line.starts_with("c ") {
171-
current_comments.push_str(&line[2..]);
170+
if let Some(st) = line.strip_prefix("c ") {
171+
current_comments.push_str(st);
172172
current_comments.push('\n');
173173
} else if line.starts_with("e") {
174174
if let Some(last_delta) = deltas.last_mut() {

plib/src/utmpx.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
//
99

1010
extern crate libc;
11-
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
12-
use crate::libc_aliases::{endutxent, getutxent, setutxent};
11+
use crate::platform::{self, endutxent, getutxent, setutxent};
1312
use std::ffi::CStr;
1413

1514
#[derive(Debug)]
@@ -24,18 +23,16 @@ pub struct Utmpx {
2423
}
2524

2625
pub fn ut_type_str(typ: libc::c_short) -> &'static str {
27-
// TODO: Remove "libc_aliases" when https://github.com/rust-lang/libc/issues/3190 is resolved
28-
use crate::libc_aliases as libc;
2926
match typ {
30-
libc::BOOT_TIME => "BOOT_TIME",
31-
libc::DEAD_PROCESS => "DEAD_PROCESS",
32-
libc::EMPTY => "EMPTY",
33-
libc::INIT_PROCESS => "INIT_PROCESS",
34-
libc::LOGIN_PROCESS => "LOGIN_PROCESS",
35-
libc::NEW_TIME => "NEW_TIME",
36-
libc::OLD_TIME => "OLD_TIME",
37-
libc::RUN_LVL => "RUN_LVL",
38-
libc::USER_PROCESS => "USER_PROCESS",
27+
platform::BOOT_TIME => "BOOT_TIME",
28+
platform::DEAD_PROCESS => "DEAD_PROCESS",
29+
platform::EMPTY => "EMPTY",
30+
platform::INIT_PROCESS => "INIT_PROCESS",
31+
platform::LOGIN_PROCESS => "LOGIN_PROCESS",
32+
platform::NEW_TIME => "NEW_TIME",
33+
platform::OLD_TIME => "OLD_TIME",
34+
platform::RUN_LVL => "RUN_LVL",
35+
platform::USER_PROCESS => "USER_PROCESS",
3936

4037
_ => "(unknown)",
4138
}

process/renice.rs

+3-37
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use clap::Parser;
1515
use errno::{errno, set_errno};
1616
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
1717
use libc::{getpwnam, passwd};
18+
use plib::platform::PPriorityWhichT;
1819
use plib::PROJECT_NAME;
1920
use std::ffi::CString;
2021
use std::io;
@@ -82,24 +83,7 @@ fn parse_id(which: u32, input: &str) -> Result<u32, &'static str> {
8283
fn xgetpriority(which: u32, id: u32) -> io::Result<i32> {
8384
set_errno(errno::Errno(0));
8485

85-
// Prevent accidental shadowing by using a block
86-
let which_cast = {
87-
#[cfg(all(not(target_os = "macos"), not(target_env = "musl")))]
88-
{
89-
which
90-
}
91-
92-
#[cfg(all(not(target_os = "macos"), target_env = "musl"))]
93-
{
94-
which as i32
95-
}
96-
97-
#[cfg(target_os = "macos")]
98-
{
99-
which as i32
100-
}
101-
};
102-
let res = unsafe { libc::getpriority(which_cast, id) };
86+
let res = unsafe { libc::getpriority(which as PPriorityWhichT, id) };
10387

10488
let errno_res = errno().0;
10589
if errno_res == 0 {
@@ -112,25 +96,7 @@ fn xgetpriority(which: u32, id: u32) -> io::Result<i32> {
11296
}
11397

11498
fn xsetpriority(which: u32, id: u32, prio: i32) -> io::Result<()> {
115-
// Prevent accidental shadowing by using a block
116-
let which_cast = {
117-
#[cfg(all(not(target_os = "macos"), not(target_env = "musl")))]
118-
{
119-
which
120-
}
121-
122-
#[cfg(all(not(target_os = "macos"), target_env = "musl"))]
123-
{
124-
which as i32
125-
}
126-
127-
#[cfg(target_os = "macos")]
128-
{
129-
which as i32
130-
}
131-
};
132-
133-
let res = unsafe { libc::setpriority(which_cast, id, prio) };
99+
let res = unsafe { libc::setpriority(which as PPriorityWhichT, id, prio) };
134100

135101
if res < 0 {
136102
let e = io::Error::last_os_error();

sys/ipcs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ fn get_current_date() -> String {
277277
}
278278

279279
fn display_ipc_status(args: &Args) {
280+
// TODO:
281+
// - add source
280282
println!("IPC status from {} as of {}", "source", get_current_date());
281283

282284
if args.message_queues {

0 commit comments

Comments
 (0)