diff options
Diffstat (limited to 'examples/cat.c')
| -rw-r--r-- | examples/cat.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/cat.c b/examples/cat.c new file mode 100644 index 0000000..c8d229d --- /dev/null +++ b/examples/cat.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* cat.c | ||
| 2 | |||
| 3 | Prints files specified on command line to the console. */ | ||
| 4 | |||
| 5 | #include <stdio.h> | ||
| 6 | #include <syscall.h> | ||
| 7 | |||
| 8 | int | ||
| 9 | main (int argc, char *argv[]) | ||
| 10 | { | ||
| 11 | bool success = true; | ||
| 12 | int i; | ||
| 13 | |||
| 14 | for (i = 1; i < argc; i++) | ||
| 15 | { | ||
| 16 | int fd = open (argv[i]); | ||
| 17 | if (fd < 0) | ||
| 18 | { | ||
| 19 | printf ("%s: open failed\n", argv[i]); | ||
| 20 | success = false; | ||
| 21 | continue; | ||
| 22 | } | ||
| 23 | for (;;) | ||
| 24 | { | ||
| 25 | char buffer[1024]; | ||
| 26 | int bytes_read = read (fd, buffer, sizeof buffer); | ||
| 27 | if (bytes_read == 0) | ||
| 28 | break; | ||
| 29 | write (STDOUT_FILENO, buffer, bytes_read); | ||
| 30 | } | ||
| 31 | close (fd); | ||
| 32 | } | ||
| 33 | return success ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| 34 | } | ||
