Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a example of Tesh variables
[simgrid.git] / tools / tesh2 / w32 / src / Write.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 STDIN_FILENO\r
9 #define STDIN_FILENO    0\r
10 #endif\r
11 \r
12 int\r
13 main(int argc, char* argv[])\r
14 {\r
15         size_t number_of_bytes_to_read = 4096;\r
16         int number_of_bytes_readed = 0;\r
17         char buffer[4096] = {0};\r
18         int failed = 0;\r
19         int fd;\r
20         \r
21         if((fd = creat(argv[1], _S_IREAD | _S_IWRITE)) < 0)\r
22         {\r
23                 fprintf(stderr,"creat() failed with the error %d\n",errno);\r
24                 return EXIT_FAILURE;\r
25         }\r
26                 \r
27         do\r
28         {\r
29                 number_of_bytes_readed = read(STDIN_FILENO, buffer, number_of_bytes_to_read);\r
30                 \r
31                 if(number_of_bytes_readed < 0 && errno != EINTR && errno != EAGAIN) \r
32                 {\r
33                         fprintf(stderr, "read failure %d\n", errno);\r
34                         failed = 1;\r
35                         break;\r
36                 }\r
37                 \r
38                 if(number_of_bytes_readed > 0) \r
39                 {\r
40                         write(fd, buffer, strlen(buffer));\r
41                         memset(buffer, 0, number_of_bytes_to_read);\r
42                 }\r
43                 \r
44         \r
45         }while(number_of_bytes_readed);\r
46         \r
47         close(fd);\r
48         \r
49         return failed ? EXIT_FAILURE : EXIT_SUCCESS;\r
50                 \r
51 }\r
52 \r