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
33 changes: 33 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require("fs");

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

const numberAll = args[0] === "-n";
const numberNonBlank = args[0] === "-b";

const files = (numberAll || numberNonBlank)
? args.slice(1)
: args;

let lineNumber = 1;

for (const file of files) {
const content = fs.readFileSync(file, "utf8");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need to guard this block in similar manner to ls?

const lines = content.split("\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If a file ends in \n, what is the last element of content.split("\n")? Combined with console.log adding its own newline, how does cat sample-files/1.txt compare to the real output — same lines, or one extra blank?


for (const line of lines) {

if (numberAll) {
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
lineNumber++;

} else if (numberNonBlank && line !== "") {
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
lineNumber++;

} else {
console.log(line);
}

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

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

let oneLine = false;
let showHidden = false;
let targets = [];

for (const arg of args) {
if (arg === "-1") {
oneLine = true;
} else if (arg === "-a") {
showHidden = true;
} else {
targets.push(arg);
}
}

if (targets.length === 0) {
targets.push(".");
}

function listDirectory(directory) {
let files = fs.readdirSync(directory);

if (!showHidden) {
files = files.filter(file => !file.startsWith("."));
}

files.sort();

if (oneLine) {
console.log(files.join("\n"));
} else {
console.log(files.join(" "));
}
}

function listTarget(target) {
try {
const stats = fs.statSync(target);

if (stats.isDirectory()) {
listDirectory(target);
} else {
console.log(target);
}

} catch (error) {
console.error(`ls: cannot access '${target}': No such file or directory`);
}
}

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

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

let countLines = false;
let countWords = false;
let countBytes = false;
let files = [];

for (const 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 no flags are given, wc shows all three
if (!countLines && !countWords && !countBytes) {
countLines = true;
countWords = true;
countBytes = true;
}

function countFile(filename) {
const content = fs.readFileSync(filename, "utf8");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

your ls example wraps the file access in try/catch block. Do we need the same block here?


const lines = content.split("\n").length - 1;
const words = content.trim() === "" ? 0 : content.trim().split(/\s+/).length;
const bytes = Buffer.byteLength(content);

let output = [];

if (countLines) {
output.push(lines);
}

if (countWords) {
output.push(words);
}

if (countBytes) {
output.push(bytes);
}

output.push(filename);

console.log(output.join(" "));
}

for (const file of files) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Run the real wc sample-files/* with several files — what extra line appears at the bottom that yours doesn't produce? Where in this loop could you accumulate the counts to print it?

countFile(file);
}
Loading