| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mosapi.h" |
| 4 | |
| 5 | #define BUFSIZE 4096 |
| 6 | |
| 7 | bool do_cat_file(const char *path) |
| 8 | { |
| 9 | fd_t fd = open(path, flags: OPEN_READ); |
| 10 | if (fd < 0) |
| 11 | { |
| 12 | fprintf(stderr, format: "failed to open file '%s'\n" , path); |
| 13 | return false; |
| 14 | } |
| 15 | |
| 16 | do |
| 17 | { |
| 18 | char buffer[BUFSIZE] = { 0 }; |
| 19 | size_t sz = syscall_io_read(fd, buffer, BUFSIZE); |
| 20 | if (sz == 0) |
| 21 | break; |
| 22 | |
| 23 | if (IS_ERR_VALUE(sz)) |
| 24 | { |
| 25 | fprintf(stderr, format: "failed to read file '%s'\n" , path); |
| 26 | syscall_io_close(fd); |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | fwrite(ptr: buffer, size: 1, nmemb: sz, stdout); |
| 31 | } while (true); |
| 32 | |
| 33 | syscall_io_close(fd); |
| 34 | |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | int main(int argc, char *argv[]) |
| 39 | { |
| 40 | if (argc < 2) |
| 41 | { |
| 42 | fputs(s: "usage: cat <file>...\n" , stderr); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | for (int i = 1; i < argc; i++) |
| 47 | do_cat_file(path: argv[i]); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |