Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
441c23d9cdbff8cfbd4c11b232a8c1040448a4b0
[simgrid.git] / tools / tesh2 / w32 / src / Read.c
1 #include <fcntl.h>\r
2 #include <stdlib.h>\r
3 #include <stdio.h>\r
4 #include <sys/stat.h>\r
5 #include <sys/types.h>\r
6 #include <errno.h>\r
7 \r
8 #ifndef STDOUT_FILENO\r
9 #define STDOUT_FILENO   1\r
10 #endif\r
11 \r
12 typedef unsigned char byte_t;\r
13 \r
14 int\r
15 main(int argc, char* argv[])\r
16 {\r
17         FILE* stream;\r
18         struct stat s = {0};\r
19         char* buffer;\r
20         int i = 0;\r
21         char c;\r
22         int failed;\r
23 \r
24         stream = fopen(argv[1], "r");\r
25 \r
26         if(!stream)\r
27         {\r
28                 fprintf(stderr, "fopen() failed withe the error %d\n", errno);\r
29                 return EXIT_FAILURE;\r
30         }\r
31         \r
32         if(stat(argv[1], &s) < 0)\r
33         {\r
34                 fclose(stream);\r
35                 fprintf(stderr, "stat() failed withe the error %d\n", errno);\r
36                 return EXIT_FAILURE;\r
37         }\r
38         \r
39         \r
40         if(!(buffer = (byte_t*) calloc(s.st_size + 1, sizeof(byte_t))))\r
41         {\r
42                 fclose(stream);\r
43                 fprintf(stderr, "calloc() failed withe the error %d\n", errno);\r
44                 return EXIT_FAILURE;\r
45         }\r
46         \r
47         \r
48         while((c = getc(stream)) != EOF)\r
49                 if((int)c != 13)\r
50                         buffer[i++] = c;\r
51         \r
52         failed = ferror(stream);\r
53         \r
54         if(!failed || buffer)\r
55                 write(STDOUT_FILENO, buffer, strlen(buffer));\r
56                 \r
57         fclose(stream);\r
58         free(buffer);\r
59         \r
60         return failed ? EXIT_FAILURE : EXIT_SUCCESS;\r
61                 \r
62 }\r
63 \r