Skip to content

Commit bc623a1

Browse files
committed
wc accounts for invalid file/folder paths
1 parent c8e7c29 commit bc623a1

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@ if (Object.keys(options).length === 0) {
2727
// incrementally updated in the loop below
2828
const totals = { l: 0, w: 0, c: 0 };
2929

30+
// wc actually prints total if there is more than one argument, not more than one file
31+
// e.g. wc invalid.txt valid.txt
32+
// there will be a total line with the same stats are the valid.txt stats
3033
let fileCount = 0;
34+
3135
for (const path of paths) {
32-
if (fs.statSync(path).isDirectory()) {
36+
let isDir;
37+
try {
38+
isDir = fs.statSync(path).isDirectory();
39+
} catch (err) {
40+
console.error(`wc: ${path} open: No such file or directory`);
41+
fileCount++;
42+
continue;
43+
}
44+
45+
if (isDir) {
3346
console.eror(`wc: ${path}: read: Is a directory`);
3447
} else {
3548
fileCount++;

0 commit comments

Comments
 (0)