Skip to content

Commit 5aaa4cd

Browse files
committed
w: Fix formatting of command line value
1 parent cd37fba commit 5aaa4cd

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/uu/w/src/w.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ fn format_time(time: String) -> Result<String, chrono::format::ParseError> {
140140
}
141141

142142
#[cfg(target_os = "linux")]
143-
fn fetch_cmdline(pid: i32) -> Result<String, std::io::Error> {
143+
fn fetch_cmdline(pid: i32) -> String {
144144
let cmdline_path = Path::new("/proc").join(pid.to_string()).join("cmdline");
145145
fs::read_to_string(cmdline_path)
146+
.unwrap_or_default()
147+
.split_terminator("\0")
148+
.collect::<Vec<_>>()
149+
.join(" ")
146150
}
147151

148152
#[cfg(target_os = "linux")]
@@ -168,7 +172,7 @@ fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
168172
idle_time: fetch_idle_time(entry.tty_device())?,
169173
jcpu: format!("{jcpu:.2}"),
170174
pcpu: fetch_pcpu_time(entry.pid()).unwrap_or_default().to_string(),
171-
command: fetch_cmdline(entry.pid()).unwrap_or_default(),
175+
command: fetch_cmdline(entry.pid()),
172176
};
173177
user_info_list.push(user_info);
174178
}
@@ -452,11 +456,8 @@ mod tests {
452456
fn test_fetch_cmdline() {
453457
// uucore's utmpx returns an i32, so we cast to that to mimic it.
454458
let pid = process::id() as i32;
455-
let path = Path::new("/proc").join(pid.to_string()).join("cmdline");
456-
assert_eq!(
457-
fs::read_to_string(path).unwrap(),
458-
fetch_cmdline(pid).unwrap()
459-
);
459+
let cmdline = std::env::args().collect::<Vec<String>>().join(" ");
460+
assert_eq!(cmdline, fetch_cmdline(pid));
460461
}
461462

462463
#[test]

0 commit comments

Comments
 (0)