diff --git a/TP5/Exo1/exo1_2.c b/TP5/Exo1/exo1_2.c index 89001c4..16b9914 100644 --- a/TP5/Exo1/exo1_2.c +++ b/TP5/Exo1/exo1_2.c @@ -1,46 +1,19 @@ +#define _GNU_SOURCE +#include #include #include #include #include -#include -#include -#include - +// ls -i -l /tmp >> log +// int main(int argc, char *argv[]) { - if (argc < 2) - { - printf("Usage: %s \n", argv[0]); - return EXIT_FAILURE; - } - - int pip, fd[2], outfile, d; - char buf; - pip = pipe(fd); - assert(pip != -1); - - pid_t p = fork(); - assert(p != -1); - - outfile = open(argv[1], O_WRONLY | O_CREAT, 0644); - - switch (p) - { - case 0: - close(fd[0]); - d = dup2(fd[1], 1); - execl("/usr/bin/ls", "ls", "-i", "-l", "/tmp", NULL); - close(fd[1]); - exit(0); - default: - close(fd[1]); - while (read(fd[0], &buf, sizeof(char)) > 0) - { - write(outfile, &buf, sizeof(char)); - } - break; - } - - close(outfile); - return EXIT_SUCCESS; -} \ No newline at end of file + int redirection = open("./log",O_WRONLY | O_APPEND | O_CREAT , 0644); + assert(redirection >= 0); + // dup2 ferme 1, et duplique redirection + dup2(redirection,1); + close(redirection); + execlp("ls","ls","-i","-l","/tmp",NULL); + assert(0); + return 0; +}