summaryrefslogtreecommitdiff
path: root/docs/examples/unix_cat.md
diff options
context:
space:
mode:
authorLuca Casonato <lucacasonato@yahoo.com>2020-05-07 00:21:13 +0200
committerGitHub <noreply@github.com>2020-05-06 18:21:13 -0400
commit34ec3b225425cecdccf754fbc87f4a8f3728890d (patch)
tree35db52bf25ccf64425692116197df61a69ea8838 /docs/examples/unix_cat.md
parent846c049c9b3ab36d0893292a204c4d0a18de4c8e (diff)
Multi page manual (#5110)
Diffstat (limited to 'docs/examples/unix_cat.md')
-rw-r--r--docs/examples/unix_cat.md24
1 files changed, 24 insertions, 0 deletions
diff --git a/docs/examples/unix_cat.md b/docs/examples/unix_cat.md
new file mode 100644
index 000000000..7534ef0d0
--- /dev/null
+++ b/docs/examples/unix_cat.md
@@ -0,0 +1,24 @@
+## An implementation of the unix "cat" program
+
+In this program each command-line argument is assumed to be a filename, the file
+is opened, and printed to stdout.
+
+```ts
+for (let i = 0; i < Deno.args.length; i++) {
+ let filename = Deno.args[i];
+ let file = await Deno.open(filename);
+ await Deno.copy(file, Deno.stdout);
+ file.close();
+}
+```
+
+The `copy()` function here actually makes no more than the necessary kernel ->
+userspace -> kernel copies. That is, the same memory from which data is read
+from the file, is written to stdout. This illustrates a general design goal for
+I/O streams in Deno.
+
+Try the program:
+
+```shell
+$ deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd
+```