From fa3da24c51d1ad6f979724b525807b66600784e8 Mon Sep 17 00:00:00 2001 From: Onur Atas <114289826+onurat@users.noreply.github.com> Date: Tue, 5 May 2026 18:25:52 +0100 Subject: [PATCH 1/3] Create ls.js Complete ls implementation with -1 and -a support --- implement-shell-tools/ls/ls.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..b9d0c14b1 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,25 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +const showAll = args.includes("-a"); + +const dirArg = args.find(arg => !arg.startsWith("-")); +const directory = dirArg || "."; + +fs.readdir(directory, (err, files) => { + if (err) { + console.error(err.message); + return; + } + + let output = files; + + if (!showAll) { + output = files.filter(file => !file.startsWith(".")); + } else { + output = [".", "..", ...files]; + } + + output.forEach(file => console.log(file)); +}); \ No newline at end of file From 8566cc49006f46608d9a9671cf77c7e003e382aa Mon Sep 17 00:00:00 2001 From: Onur Atas <114289826+onurat@users.noreply.github.com> Date: Tue, 5 May 2026 18:54:11 +0100 Subject: [PATCH 2/3] Complete Module Tools: ls, cat, wc implementations Complete Module Tools: ls, cat, wc implementations --- implement-shell-tools/cat/cat.js | 42 +++++++++++++++++++++++ implement-shell-tools/wc/wc.js | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 implement-shell-tools/cat/cat.js create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..a463947f8 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,42 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +const showN = args.includes("-n"); +const showB = args.includes("-b"); + +const files = args.filter(a => !a.startsWith("-")); + +let content = files.map(f => fs.readFileSync(f, "utf8")).join(""); + +let lines = content.split("\n"); + +// remove ONLY final empty line caused by trailing newline +if (lines[lines.length - 1] === "") { + lines.pop(); +} + +let output = []; + +if (showN) { + let i = 1; + for (const line of lines) { + output.push(`${String(i).padStart(6)} ${line}`); + i++; + } +} else if (showB) { + let i = 1; + for (const line of lines) { + if (line !== "") { + output.push(`${String(i).padStart(6)} ${line}`); + i++; + } else { + output.push(""); + } + } +} else { + output = lines; +} + +// IMPORTANT: add final newline like real cat +process.stdout.write(output.join("\n") + "\n"); \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..d4fc1cec0 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,58 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +const showLines = args.includes("-l"); +const showWords = args.includes("-w"); +const showBytes = args.includes("-c"); + +const files = args.filter(a => !a.startsWith("-")); + +function getStats(text) { + return { + lines: text.split("\n").length - 1, + words: text.trim().split(/\s+/).filter(Boolean).length, + bytes: Buffer.byteLength(text) + }; +} + +function format(num) { + return String(num).padStart(8); +} + +let totalLines = 0; +let totalWords = 0; +let totalBytes = 0; + +files.forEach(file => { + const content = fs.readFileSync(file, "utf8"); + const stats = getStats(content); + + totalLines += stats.lines; + totalWords += stats.words; + totalBytes += stats.bytes; + + let output = []; + + if (showLines) output.push(format(stats.lines)); + else if (showWords) output.push(format(stats.words)); + else if (showBytes) output.push(format(stats.bytes)); + else output.push(format(stats.lines), format(stats.words), format(stats.bytes)); + + output.push(file); + + console.log(output.join(" ")); +}); + +if (files.length > 1) { + let output = []; + + if (showLines) output.push(format(totalLines)); + else if (showWords) output.push(format(totalWords)); + else if (showBytes) output.push(format(totalBytes)); + else output.push(format(totalLines), format(totalWords), format(totalBytes)); + + output.push("total"); + + console.log(output.join(" ")); +} \ No newline at end of file From 984e65391efdc43f53e8d53c3361672e1e2aa60c Mon Sep 17 00:00:00 2001 From: Onur Atas <114289826+onurat@users.noreply.github.com> Date: Tue, 5 May 2026 19:04:01 +0100 Subject: [PATCH 3/3] Update cat.js --- implement-shell-tools/cat/cat.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index a463947f8..231db1649 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -11,7 +11,6 @@ let content = files.map(f => fs.readFileSync(f, "utf8")).join(""); let lines = content.split("\n"); -// remove ONLY final empty line caused by trailing newline if (lines[lines.length - 1] === "") { lines.pop(); } @@ -38,5 +37,4 @@ if (showN) { output = lines; } -// IMPORTANT: add final newline like real cat process.stdout.write(output.join("\n") + "\n"); \ No newline at end of file