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
54 changes: 54 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 hasTrailingNewline = contents.endsWith("\n");
const lines = hasTrailingNewline
? contents.slice(0, -1).split("\n")
: contents.split("\n");

lines.forEach((line, index) => {
const output =
index < lines.length - 1 || hasTrailingNewline ? 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

process.stdout.write("\n");
}
}
} catch (error) {
console.log("Cannot access: " + path);
}
}
73 changes: 73 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 formatResult(lines, words, bytes, fileName) {
let result = "";
if (countLines) {
result += String(lines).padStart(8);
}
if (countWords) {
result += String(words).padStart(8);
}
if (countBytes) {
result += String(bytes).padStart(8);
}
return result + " " + fileName;
}

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++;
console.log(formatResult(lines, words, bytes, fileName));
} catch (error) {
console.log("Cannot read file: " + fileName);
}
}

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

if (filesCounted > 1) {
console.log(formatResult(totalLines, totalWords, totalBytes, "total"));
}
Loading