diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..980562a87 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -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"); + const lines = content.split("\n"); + + 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); + } + + } +} \ 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..483cd2e60 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -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); +} \ 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..505499259 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -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"); + + 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) { + countFile(file); +} \ No newline at end of file