I try to get the output of the execvp and print it to a named pipe. But how can I do this? Here is my code :
void *run(void *comm)
{
struct command *com = (struct command *)comm;
int fd[2];
pipe(fd);
if (fork() == 0)
{
close(fd[0]);
dup2(fd[1], 1);
dup2(fd[1], 2);
close(fd[1]);
execvp(com->list[0], com->list);
}
else
{
char buffer[1024] = {0};
close(fd[1]);
unlink(com->namePipe);
if(mkfifo(com->namePipe, 0644) != 0)
{
fprintf(stderr, "Impossible de créer le tube nommé.\n");
exit(EXIT_FAILURE);
}
if((fd[0] = open(com->namePipe, O_WRONLY)) == -1)
{
fprintf(stderr, "Impossible d'ouvrir l'entrée du tube nommé.\n");
exit(EXIT_FAILURE);
}
while (read(fd[0], buffer, sizeof(buffer)) != 0)
{
write(1, buffer, strlen(buffer));
memset (buffer, 0, sizeof(buffer));
}
}
return NULL;
}
I'm not allowed to use popen.
EDIT : I did this but not work ^^