renamed and cleaned up export header files to match linux convention
[lunaix-os.git] / lunaix-os / usr / cat.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #define BUFSIZE 4096
7
8 static char buffer[BUFSIZE];
9
10 int
11 main(int argc, const char* argv[])
12 {
13     int fd = 0;
14     int size = 0;
15     struct file_stat stat;
16     for (int i = 1; i < argc; i++) {
17         fd = open(argv[i], FO_RDONLY);
18                 
19         if (fd < 0) {
20             printf("open failed: %s (error: %d)", argv[i], fd);
21             continue;
22         }
23
24         if (fstat(fd, &stat) < 0) {
25             printf("fail to get stat %d\n", errno);
26             return 1;
27         }
28
29         if (((stat.st_mode >> 16) & F_DIR)) {
30             printf("%s is a directory", argv[i]);
31             return 1;
32         }
33
34         do {
35             size = read(fd, buffer, BUFSIZE);
36             if (size < 0) {
37                 printf("error while reading: %d\n", size);
38                 break;
39             }
40             write(stdout, buffer, size);
41         } while (size == BUFSIZE);
42
43         close(fd);
44     }
45
46     return 0;
47 }