Changement exo 1.2 TP5

This commit is contained in:
Tom MOGULJAK 2023-10-06 11:31:48 +02:00
parent 5276b50139
commit c971482d73

View File

@ -1,46 +1,19 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
// ls -i -l /tmp >> log
//
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <file>\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;
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;
}