Add TD2 exercice2
[TD_IML.git] / TD2 / exercise2 / exercise.c
diff --git a/TD2/exercise2/exercise.c b/TD2/exercise2/exercise.c
new file mode 100644 (file)
index 0000000..ae5ed84
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+int main() {
+    int out_file_fd = open("ouput.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
+    if (out_file_fd < 0) {
+        perror("open failure\n");
+        exit(EXIT_FAILURE);
+    }
+
+    dup2(out_file_fd, STDOUT_FILENO);
+
+    execl("/bin/ls", "ls", (char*)NULL);
+
+    close(out_file_fd);
+
+    return EXIT_SUCCESS;
+}