X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/bef1210245bb3180a57f26405369654eaa477f63..1eeed1150149b63d6e49e033697454bc12b533b9:/lunaix-os/usr/cat.c diff --git a/lunaix-os/usr/cat.c b/lunaix-os/usr/cat.c new file mode 100644 index 0000000..9f0d35b --- /dev/null +++ b/lunaix-os/usr/cat.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +#define BUFSIZE 4096 + +static char buffer[BUFSIZE]; + +int +main(int argc, const char* argv[]) +{ + int fd = 0; + int size = 0; + struct file_stat stat; + for (int i = 1; i < argc; i++) { + fd = open(argv[i], FO_RDONLY); + + if (fd < 0) { + printf("open failed: %s (error: %d)", argv[i], fd); + continue; + } + + if (fstat(fd, &stat) < 0) { + printf("fail to get stat %d\n", errno); + return 1; + } + + if (!(stat.mode & F_MFILE)) { + printf("%s is a directory", argv[i]); + return 1; + } + + do { + size = read(fd, buffer, BUFSIZE); + write(stdout, buffer, size); + } while (size == BUFSIZE); + + close(fd); + } + + return 0; +} \ No newline at end of file