diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..231db1649 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,40 @@ +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"); + +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; +} + +process.stdout.write(output.join("\n") + "\n"); \ No newline at end of file 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 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