Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the new integrated files version (use xbt data structures instead my own data...
[simgrid.git] / tools / tesh2 / w32 / src / Read.c
diff --git a/tools/tesh2/w32/src/Read.c b/tools/tesh2/w32/src/Read.c
new file mode 100644 (file)
index 0000000..441c23d
--- /dev/null
@@ -0,0 +1,63 @@
+#include <fcntl.h>\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+#include <sys/stat.h>\r
+#include <sys/types.h>\r
+#include <errno.h>\r
+\r
+#ifndef STDOUT_FILENO\r
+#define STDOUT_FILENO  1\r
+#endif\r
+\r
+typedef unsigned char byte_t;\r
+\r
+int\r
+main(int argc, char* argv[])\r
+{\r
+       FILE* stream;\r
+       struct stat s = {0};\r
+       char* buffer;\r
+       int i = 0;\r
+       char c;\r
+       int failed;\r
+\r
+       stream = fopen(argv[1], "r");\r
+\r
+       if(!stream)\r
+       {\r
+               fprintf(stderr, "fopen() failed withe the error %d\n", errno);\r
+               return EXIT_FAILURE;\r
+       }\r
+       \r
+       if(stat(argv[1], &s) < 0)\r
+       {\r
+               fclose(stream);\r
+               fprintf(stderr, "stat() failed withe the error %d\n", errno);\r
+               return EXIT_FAILURE;\r
+       }\r
+       \r
+       \r
+       if(!(buffer = (byte_t*) calloc(s.st_size + 1, sizeof(byte_t))))\r
+       {\r
+               fclose(stream);\r
+               fprintf(stderr, "calloc() failed withe the error %d\n", errno);\r
+               return EXIT_FAILURE;\r
+       }\r
+       \r
+       \r
+       while((c = getc(stream)) != EOF)\r
+               if((int)c != 13)\r
+                       buffer[i++] = c;\r
+       \r
+       failed = ferror(stream);\r
+       \r
+       if(!failed || buffer)\r
+               write(STDOUT_FILENO, buffer, strlen(buffer));\r
+               \r
+       fclose(stream);\r
+       free(buffer);\r
+       \r
+       return failed ? EXIT_FAILURE : EXIT_SUCCESS;\r
+               \r
+}\r
+\r