Skip to content

Commit 7dfeb46

Browse files
committed
ps: Implement -l, -ly, -X field selection flags
1 parent 2e3fe79 commit 7dfeb46

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

src/uu/ps/src/mapping.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ pub(crate) fn job_format_codes() -> Vec<String> {
5151
.to_vec()
5252
}
5353

54+
/// Returns the long format codes (for -l flag).
55+
pub(crate) fn long_format_codes() -> Vec<String> {
56+
[
57+
"f", "s", "uid", "pid", "ppid", "c", "pri", "ni", "addr", "sz", "wchan", "tname", "time",
58+
"ucmd",
59+
]
60+
.map(Into::into)
61+
.to_vec()
62+
}
63+
64+
/// Returns the modified long format codes (for -ly flags).
65+
pub(crate) fn long_y_format_codes() -> Vec<String> {
66+
[
67+
"s", "uid", "pid", "ppid", "c", "pri", "ni", "rss", "sz", "wchan", "tname", "time", "ucmd",
68+
]
69+
.map(Into::into)
70+
.to_vec()
71+
}
72+
5473
/// Returns the default codes with PSR column (for -P flag).
5574
pub(crate) fn default_with_psr_codes() -> Vec<String> {
5675
["pid", "psr", "tname", "time", "ucmd"]
@@ -85,6 +104,15 @@ pub(crate) fn vm_format_codes() -> Vec<String> {
85104
.to_vec()
86105
}
87106

107+
/// Returns the register format codes (for -X flag).
108+
pub(crate) fn register_format_codes() -> Vec<String> {
109+
[
110+
"pid", "stackp", "esp", "eip", "tmout", "alarm", "stat", "tname", "time", "command",
111+
]
112+
.map(Into::into)
113+
.to_vec()
114+
}
115+
88116
/// Collect mapping from argument
89117
pub(crate) fn default_mapping() -> HashMap<String, String> {
90118
let mut mapping = HashMap::new();
@@ -100,8 +128,10 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
100128
append("_right2", "R2R2R2R2");
101129
append("_unlimited", "U");
102130
append("_unlimited2", "U2");
131+
append("addr", "ADDR"); // undocumented
103132
append("ag_id", "AGID");
104133
append("ag_nice", "AGNI");
134+
append("alarm", "ALARM"); // undocumented
105135
append("args", "COMMAND");
106136
append("atime", "TIME");
107137
append("blocked", "BLOCKED");
@@ -259,6 +289,7 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
259289
append("time", "TIME");
260290
append("timens", "TIMENS");
261291
append("times", "TIME");
292+
append("tmout", "TMOUT"); // undocumented
262293
append("tname", "TTY");
263294
append("tpgid", "TPGID");
264295
append("trs", "TRS");

src/uu/ps/src/ps.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use clap::crate_version;
1313
use clap::{Arg, ArgAction, ArgMatches, Command};
1414
use mapping::{
1515
collect_code_mapping, default_codes, default_mapping, default_with_psr_codes,
16-
extra_full_format_codes, full_format_codes, job_format_codes, signal_format_codes,
17-
user_format_codes, vm_format_codes,
16+
extra_full_format_codes, full_format_codes, job_format_codes, long_format_codes,
17+
long_y_format_codes, register_format_codes, signal_format_codes, user_format_codes,
18+
vm_format_codes,
1819
};
1920
use parser::{parser, OptionalKeyValue};
2021
use prettytable::{format::consts::FORMAT_CLEAN, Row, Table};
@@ -57,6 +58,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5758
extra_full_format_codes()
5859
} else if matches.get_flag("j") {
5960
job_format_codes()
61+
} else if matches.get_flag("l") && matches.get_flag("y") {
62+
long_y_format_codes()
63+
} else if matches.get_flag("l") {
64+
long_format_codes()
6065
} else if matches.get_flag("P") {
6166
default_with_psr_codes()
6267
} else if matches.get_flag("s") {
@@ -65,6 +70,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6570
user_format_codes()
6671
} else if matches.get_flag("v") {
6772
vm_format_codes()
73+
} else if matches.get_flag("X") {
74+
register_format_codes()
6875
} else if arg_formats.is_empty() {
6976
default_codes()
7077
} else {
@@ -200,6 +207,12 @@ pub fn uu_app() -> Command {
200207
.action(ArgAction::SetTrue)
201208
.help("job format"),
202209
)
210+
.arg(
211+
Arg::new("l")
212+
.short('l')
213+
.action(ArgAction::SetTrue)
214+
.help("long format"),
215+
)
203216
.arg(
204217
Arg::new("P")
205218
.short('P')
@@ -225,6 +238,18 @@ pub fn uu_app() -> Command {
225238
.action(ArgAction::SetTrue)
226239
.help("virtual memory format"),
227240
)
241+
.arg(
242+
Arg::new("y")
243+
.short('y')
244+
.action(ArgAction::SetTrue)
245+
.help("do not show flags, show rss vs. addr (used with -l)"),
246+
)
247+
.arg(
248+
Arg::new("X")
249+
.short('X')
250+
.action(ArgAction::SetTrue)
251+
.help("register format"),
252+
)
228253
.arg(
229254
Arg::new("format")
230255
.short('o')

tests/by-util/test_ps.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ fn test_job_format() {
5757
check_header("-j", &["PID", "PGID", "SID", "TTY", "TIME", "CMD"]);
5858
}
5959

60+
#[test]
61+
#[cfg(target_os = "linux")]
62+
fn test_long_format() {
63+
check_header(
64+
"-l",
65+
&[
66+
"F", "S", "UID", "PID", "PPID", "C", "PRI", "NI", "ADDR", "SZ", "WCHAN", "TTY", "TIME",
67+
"CMD",
68+
],
69+
);
70+
}
71+
72+
#[test]
73+
#[cfg(target_os = "linux")]
74+
fn test_long_format_with_y() {
75+
check_header(
76+
"-ly",
77+
&[
78+
"S", "UID", "PID", "PPID", "C", "PRI", "NI", "RSS", "SZ", "WCHAN", "TTY", "TIME", "CMD",
79+
],
80+
);
81+
}
82+
6083
#[test]
6184
#[cfg(target_os = "linux")]
6285
fn test_psr_format() {
@@ -97,6 +120,17 @@ fn test_virtual_memory_format() {
97120
);
98121
}
99122

123+
#[test]
124+
#[cfg(target_os = "linux")]
125+
fn test_register_format() {
126+
check_header(
127+
"-X",
128+
&[
129+
"PID", "STACKP", "ESP", "EIP", "TMOUT", "ALARM", "STAT", "TTY", "TIME", "COMMAND",
130+
],
131+
);
132+
}
133+
100134
#[test]
101135
#[cfg(target_os = "linux")]
102136
fn test_code_mapping() {

0 commit comments

Comments
 (0)