diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..e3ba88c80 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,52 @@ +// get CLI arguments +const args = process.argv.slice(2); + +// flags +const showLines = args.includes("-l"); +const showWords = args.includes("-w"); +const showBytes = args.includes("-c"); + +// get files (remove flags) +const files = args.filter((arg) => !arg.startsWith("-")); + +// helper functions +function countLines(text) { + return text.split("\n").length - 1; +} + +function countWords(text) { + return text.trim().split(/\s+/).filter(Boolean).length; +} + +function countBytes(text) { + return Buffer.byteLength(text, "utf8"); +} + +// loop through files +for (let i = 0; i < files.length; i++) { + const file = files[i]; + + try { + const content = fs.readFileSync(file, "utf8"); + + const lines = countLines(content); + const words = countWords(content); + const bytes = countBytes(content); + + let output = ""; + + // if no flag → show all + if (!showLines && !showWords && !showBytes) { + output = `${lines} ${words} ${bytes} ${file}`; + } else { + if (showLines) output += `${lines} `; + if (showWords) output += `${words} `; + if (showBytes) output += `${bytes} `; + output += file; + } + + console.log(output.trim()); + } catch (err) { + console.error(`wc: cannot open ${file}`); + } +} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..568c5475d --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,25 @@ +const fs = require("fs"); + +// input from terminal +const args = process.argv.slice(2); + +// flags +const showAll = args.includes("-a"); + +// get folder (default = current folder) +const folder = args.filter((arg) => !arg.startsWith("-"))[0] || "."; + +// read directory +const files = fs.readdirSync(folder); + +// loop and print +for (let i = 0; i < files.length; i++) { + const file = files[i]; + + // skip hidden files unless -a is used + if (!showAll && file.startsWith(".")) { + continue; + } + + console.log(file); +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..1d83e8fe4 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,54 @@ +const fs = require("fs"); + +// get CLI arguments +const args = process.argv.slice(2); + +// flags +const showLines = args.includes("-l"); +const showWords = args.includes("-w"); +const showBytes = args.includes("-c"); + +// get files (remove flags) +const files = args.filter((arg) => !arg.startsWith("-")); + +// helper functions +function countLines(text) { + return text.split("\n").length - 1; +} + +function countWords(text) { + return text.trim().split(/\s+/).filter(Boolean).length; +} + +function countBytes(text) { + return Buffer.byteLength(text, "utf8"); +} + +// loop through files +for (let i = 0; i < files.length; i++) { + const file = files[i]; + + try { + const content = fs.readFileSync(file, "utf8"); + + const lines = countLines(content); + const words = countWords(content); + const bytes = countBytes(content); + + let output = ""; + + // if no flag → show all + if (!showLines && !showWords && !showBytes) { + output = `${lines} ${words} ${bytes} ${file}`; + } else { + if (showLines) output += `${lines} `; + if (showWords) output += `${words} `; + if (showBytes) output += `${bytes} `; + output += file; + } + + console.log(output.trim()); + } catch (err) { + console.error(`wc: cannot open ${file}`); + } +}