Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// get CLI arguments
const args = process.argv.slice(2);

// flags
const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showBytes = args.includes("-c");

// get files (remove flags)
const files = args.filter((arg) => !arg.startsWith("-"));

// helper functions
function countLines(text) {
return text.split("\n").length - 1;
}

function countWords(text) {
return text.trim().split(/\s+/).filter(Boolean).length;
}

function countBytes(text) {
return Buffer.byteLength(text, "utf8");
}

// loop through files
for (let i = 0; i < files.length; i++) {
const file = files[i];

try {
const content = fs.readFileSync(file, "utf8");

const lines = countLines(content);
const words = countWords(content);
const bytes = countBytes(content);

let output = "";

// if no flag → show all
if (!showLines && !showWords && !showBytes) {
output = `${lines} ${words} ${bytes} ${file}`;
} else {
if (showLines) output += `${lines} `;
if (showWords) output += `${words} `;
if (showBytes) output += `${bytes} `;
output += file;
}

console.log(output.trim());
} catch (err) {
console.error(`wc: cannot open ${file}`);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks like the whole cat.js file has wc tool inside, not cat

}
}
25 changes: 25 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");

// input from terminal
const args = process.argv.slice(2);

// flags
const showAll = args.includes("-a");

// get folder (default = current folder)
const folder = args.filter((arg) => !arg.startsWith("-"))[0] || ".";

// read directory
const files = fs.readdirSync(folder);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What happens if no folder is found?


// loop and print
for (let i = 0; i < files.length; i++) {
const file = files[i];

// skip hidden files unless -a is used
if (!showAll && file.startsWith(".")) {
continue;
}

console.log(file);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

real ls default output puts all files on one line separated by spaces; one-per-line requires the -1 flag. The -1 flag is not implemented here.

}
54 changes: 54 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require("fs");

// get CLI arguments
const args = process.argv.slice(2);

// flags
const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showBytes = args.includes("-c");

// get files (remove flags)
const files = args.filter((arg) => !arg.startsWith("-"));

// helper functions
function countLines(text) {
return text.split("\n").length - 1;
}

function countWords(text) {
return text.trim().split(/\s+/).filter(Boolean).length;
}

function countBytes(text) {
return Buffer.byteLength(text, "utf8");
}

// loop through files
for (let i = 0; i < files.length; i++) {
const file = files[i];

try {
const content = fs.readFileSync(file, "utf8");

const lines = countLines(content);
const words = countWords(content);
const bytes = countBytes(content);

let output = "";

// if no flag → show all
if (!showLines && !showWords && !showBytes) {
output = `${lines} ${words} ${bytes} ${file}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Numbers are not right-aligned. Real wc uses fixed-width columns

} else {
if (showLines) output += `${lines} `;
if (showWords) output += `${words} `;
if (showBytes) output += `${bytes} `;
output += file;
}

console.log(output.trim());
} catch (err) {
console.error(`wc: cannot open ${file}`);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing totals row for multiple files. When more than one file is given, real wc prints a total line at the end.

Loading