1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.
use crate::shell::{CommandOutput, Shell};
const WC_HELP_TEXT: &str = "\
Usage: wc [OPTION]... [FILE]...
Print newline, word, and byte counts for each FILE.
-l print the newline counts
-w print the word counts
-c print the byte counts
-m print the character counts
-L print the maximum display width
-h, --help display this help and exit
";
impl Shell {
pub fn cmd_wc(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
if args.contains(&"-h") || args.contains(&"--help") {
return CommandOutput::success(WC_HELP_TEXT.to_string());
}
// Counter flags SELECT which columns to print; they must not cancel
// each other (`wc -c -w` must show bytes AND words). Track the requested
// set, then fall back to the default three only when no flag was given.
let mut want_lines = false;
let mut want_words = false;
let mut want_bytes = false;
let mut want_chars = false;
let mut want_max_width = false;
let mut any_flag = false;
let mut files = Vec::new();
for arg in args {
if arg.starts_with("--") {
match *arg {
"--lines" => {
want_lines = true;
any_flag = true;
}
"--words" => {
want_words = true;
any_flag = true;
}
"--bytes" => {
want_bytes = true;
any_flag = true;
}
"--chars" => {
want_chars = true;
any_flag = true;
}
"--max-line-length" => {
want_max_width = true;
any_flag = true;
}
_ => crate::warn!("wc: warning: unsupported option '{}'", arg),
}
} else if arg.starts_with('-') && arg.len() > 1 {
for ch in arg.chars().skip(1) {
match ch {
'l' => {
want_lines = true;
any_flag = true;
}
'w' => {
want_words = true;
any_flag = true;
}
'c' => {
want_bytes = true;
any_flag = true;
}
'm' => {
want_chars = true;
any_flag = true;
}
'L' => {
want_max_width = true;
any_flag = true;
}
_ => crate::warn!("wc: warning: unsupported option '-{}'", ch),
}
}
} else {
files.push(arg.to_string());
}
}
let (show_lines, show_words, show_bytes, show_max_width, bytes_mode) = if any_flag {
(
want_lines,
want_words,
want_bytes || want_chars,
want_max_width,
!want_chars,
)
} else {
(true, true, true, false, true)
};
// GNU `wc`: each numeric field is right-aligned to the width of the
// largest displayed count (min 1). A single input (stdin/one file) is
// therefore un-padded (`wc -l < f` → `3`), not a fixed width of 7.
let mut rows: Vec<(Vec<usize>, Option<String>)> = Vec::new();
let mut totals: Vec<usize> = Vec::new();
if files.is_empty() {
let input = match stdin {
Some(s) => s,
None => return CommandOutput::error("wc: missing file operand\n".to_string(), 1),
};
// Prefer the byte-accurate stdin (`cat bin | wc -c`).
let raw = self
.take_binary_in()
.unwrap_or_else(|| input.as_bytes().to_vec());
let l = input.lines().count();
let w = input.split_whitespace().count();
let bc = if bytes_mode {
raw.len()
} else {
input.chars().count()
};
let maxw = input.lines().map(|x| x.chars().count()).max().unwrap_or(0);
let mut vals = Vec::new();
if show_lines {
vals.push(l);
}
if show_words {
vals.push(w);
}
if show_bytes {
vals.push(bc);
}
if show_max_width {
vals.push(maxw);
}
rows.push((vals, None));
} else {
let mut t = [0usize; 4];
for file in &files {
match self.vfs.read(file, &self.cwd) {
Ok(data) => {
let content = String::from_utf8_lossy(&data);
let l = content.lines().count();
let w = content.split_whitespace().count();
let bc = if bytes_mode {
data.len()
} else {
content.chars().count()
};
let maxw = content
.lines()
.map(|x| x.chars().count())
.max()
.unwrap_or(0);
t[0] += l;
t[1] += w;
t[2] += bc;
t[3] = t[3].max(maxw);
let mut vals = Vec::new();
if show_lines {
vals.push(l);
}
if show_words {
vals.push(w);
}
if show_bytes {
vals.push(bc);
}
if show_max_width {
vals.push(maxw);
}
rows.push((vals, Some(file.clone())));
}
Err(e) => {
return CommandOutput::error(format!("wc: {}: {}\n", file, e), 1);
}
}
}
if show_lines {
totals.push(t[0]);
}
if show_words {
totals.push(t[1]);
}
if show_bytes {
totals.push(t[2]);
}
if show_max_width {
totals.push(t[3]);
}
}
let width = rows
.iter()
.flat_map(|(v, _)| v.iter())
.chain(totals.iter())
.map(|v| v.to_string().len())
.max()
.unwrap_or(1)
.max(1);
let fmt_row = |vals: &[usize], name: Option<&str>| {
let mut s = vals
.iter()
.map(|v| format!("{:>w$}", v, w = width))
.collect::<Vec<_>>()
.join(" ");
if let Some(n) = name {
if !s.is_empty() {
s.push(' ');
}
s.push_str(n);
}
s
};
let mut output = String::new();
for (vals, name) in &rows {
output.push_str(&fmt_row(vals, name.as_deref()));
output.push('\n');
}
if files.len() > 1 {
output.push_str(&fmt_row(&totals, Some("total")));
output.push('\n');
}
CommandOutput::success(output)
}
}
#[cfg(test)]
mod tests {
use super::Shell;
use std::fs;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
fn mk_shell() -> Shell {
let n = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir =
std::env::temp_dir().join(format!("fastshell_wc_test_{}_{}", std::process::id(), n));
let _ = fs::remove_dir_all(&dir);
let vfs = crate::vfs::Vfs::new(dir).unwrap();
Shell::new(vfs)
}
#[test]
fn test_wc_help() {
let mut s = mk_shell();
let out = s.execute("wc", &["-h"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_wc_help_long() {
let mut s = mk_shell();
let out = s.execute("wc", &["--help"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
}