Applications/cat - Added error check
[tpg/acess2.git] / Usermode / Applications / cat_src / main.c
1 /*
2  * Acess2 CAT command
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #define BUF_SIZE        1024
9
10 /**
11  * \fn int main(int argc, char *argv[])
12  * \brief Entrypoint
13  */
14 int main(int argc, char *argv[])
15 {
16          int    fd;
17          int    num;
18         char    buf[BUF_SIZE+1];
19
20         if(argc < 2) {
21                 printf("Usage: cat <file>\n");
22                 return -1;
23         }
24
25         fd = open(argv[1], OPENFLAG_READ);
26         if(fd == -1) {
27                 printf("Unable to open '%s' for reading\n", argv[1]);
28                 return -1;
29         }
30
31         do {
32                 num = read(fd, buf, BUF_SIZE);
33                 if(num < 0)     break;
34                 buf[num] = '\0';
35                 printf("%s", buf);
36         } while(num == BUF_SIZE);
37
38         close(fd);
39         printf("\n");
40
41         return 0;
42 }

UCC git Repository :: git.ucc.asn.au