cat.c (577B)
1 #include <libc.h> 2 3 #define buf_size 1024 4 5 int main(int argc, char *argv[]) { 6 if (argc < 2) { 7 write(stderr, "usage: cat <file> [file] ...\n", 29); 8 return 1; 9 } 10 11 char buf[buf_size]; 12 ssize_t n; 13 14 for (int i = 1; i < argc; i++) { 15 int fd = open(argv[i], o_rdonly, 0); 16 if (fd < 0) { 17 write(stderr, "cat: cannot open file: ", 23); 18 write(stderr, argv[i], strlen(argv[i])); 19 write(stderr, "\n", 1); 20 continue; 21 } 22 23 while ((n = read(fd, buf, sizeof(buf))) > 0) { 24 write(stdout, buf, n); 25 } 26 27 close(fd); 28 } 29 return 0; 30 }