Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");

const args = process.argv.slice(2);

let numberLines = false;
let numberNonBlank = false;
const files = [];

for (const arg of args) {
if (arg === "-n") {
numberLines = true;
} else if (arg === "-b") {
numberNonBlank = true;
} else {
files.push(arg);
}
}

if (numberNonBlank) {
numberLines = false;
}

let lineNumber = 1;

for (const file of files) {
try {
const contents = fs.readFileSync(file, "utf8");
const lines = contents.split("\n");

lines.forEach((line, index) => {
const output = index < lines.length - 1 ? line + "\n" : line;

if (numberNonBlank) {
if (line.trim() === "") {
process.stdout.write(output);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I am looking at the output of blank lines I get some odd formatting, do you see this in your testing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I found the formatting issue in my testing. It was caused by the trailing newline being treated as an additional blank line. I fixed it so blank lines are handled correctly, and the output now matches the standard cat command. @LonMcGregor

} else {
process.stdout.write(`${String(lineNumber).padStart(6)}\t${output}`);
lineNumber++;
}
} else if (numberLines) {
process.stdout.write(`${String(lineNumber).padStart(6)}\t${output}`);
lineNumber++;
} else {
process.stdout.write(output);
}
});
} catch (err) {
console.error(`cat: ${file}: ${err.message}`);
}
}
52 changes: 52 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require("fs");

const args = process.argv.slice(2);

let onePerLine = false;
let showHidden = false;
let paths = [];

for (let i = 0; i < args.length; i++) {
if (args[i] === "-1") {
onePerLine = true;
} else if (args[i] === "-a") {
showHidden = true;
} else {
paths.push(args[i]);
}
}
if (paths.length === 0) {
paths.push(".");
}

for (let i = 0; i < paths.length; i++) {
let path = paths[i];

try {
if (fs.statSync(path).isFile()) {
console.log(path);
} else {
let files = fs.readdirSync(path);

files.sort();

for (let j = 0; j < files.length; j++) {
let file = files[j];
if (!showHidden && file.startsWith(".")) {
continue;
}
if (onePerLine) {
console.log(file);
} else {
process.stdout.write(file + " ");
}
}

if (!onePerLine) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this last empty log doing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds the final newline after the filenames have been written with process.stdout.write(), which does not add one automatically. The !onePerLine condition ensures this only happens in the default format, where filenames are displayed on the same line. @LonMcGregor

console.log();
}
}
} catch (error) {
console.log("Cannot access: " + path);
}
}
82 changes: 82 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const fs = require("fs");

const args = process.argv.slice(2);

let countLines = false;
let countWords = false;
let countBytes = false;

let files = [];

for (let arg of args) {
if (arg === "-l") {
countLines = true;
} else if (arg === "-w") {
countWords = true;
} else if (arg === "-c") {
countBytes = true;
} else {
files.push(arg);
}
}

if (!countLines && !countWords && !countBytes) {
countLines = true;
countWords = true;
countBytes = true;
}

let totalLines = 0;
let totalWords = 0;
let totalBytes = 0;
let filesCounted = 0;

function countFile(fileName) {
try {
const content = fs.readFileSync(fileName, "utf8");
let lines = content.split("\n").length - 1;
let words = content
.trim()
.split(/\s+/)
.filter((word) => word.length > 0).length;
let bytes = Buffer.byteLength(content);
totalLines += lines;
totalWords += words;
totalBytes += bytes;
filesCounted++;
let result = "";
if (countLines) {
result += lines + " ";
}
if (countWords) {
result += words + " ";
}
if (countBytes) {
result += bytes + " ";
}
result += fileName;
console.log(result);
} catch (error) {
console.log("Cannot read file: " + fileName);
}
}

for (let file of files) {
countFile(file);
}

if (filesCounted > 1) {
let result = "";
if (countLines) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This printing code looks to be repeated, do you think you could reduce the duplication somehow?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I reduced the duplication by moving the repeated output-building logic into a shared formatResult() function. Both the individual file results and the final total result now use this function, keeping the formatting consistent and easier to maintain. @LonMcGregor

result += totalLines + " ";
}
if (countWords) {
result += totalWords + " ";
}
if (countBytes) {
result += totalBytes + " ";
}
result += "total";

console.log(result);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you have multiple files, is the output formatting as good as it could be? How could it be made neater?

@mervereis mervereis Aug 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output could be neater. The numeric values should be right-aligned in consistent columns, matching the standard wc format. I updated the formatting so both individual file results and the total row use aligned columns, making the output easier to read. @LonMcGregor

}
Loading