#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main(void) {
if(mkfifo("fifo", S_IRWXU) < 0 && errno != EEXIST) {
perror("Fifo error");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if(pid == 0) /*dziecko*/
{
int fifo_write_end = open("fifo", O_WRONLY);
if(fifo_write_end < 0)
{
perror("fifo_write_end error");
exit(EXIT_FAILURE);
}
if(dup2(fifo_write_end, STDOUT_FILENO) < 0)
{
perror("dup2 fifo_write_end error");
exit(EXIT_FAILURE);
}
if(execlp("/bin/ls", "ls", "-al", NULL) < 0)
{
perror("execlp error");
exit(EXIT_FAILURE);
}
}
if(pid > 0) /*rodzic*/
{
int fifo_read_end = open("fifo", O_RDONLY);
if(fifo_read_end < 0)
{
perror("fifo_read_end error");
exit(EXIT_FAILURE);
}
if(dup2(fifo_read_end, STDOUT_FILENO) < 0)
{
perror("dup2 fifo_read_end error");
exit(EXIT_FAILURE);
}
int atxt = open("a.txt", O_WRONLY|O_CREAT, S_IRWXU);
if(atxt < 0)
{
perror("a.txt open error");
exit(EXIT_FAILURE);
}
if(dup2(atxt,STDOUT_FILENO) < 0)
{
perror("dup2 atxt error");
exit(EXIT_FAILURE);
}
if(execlp("/usr/bin/tr", "tr", "a-z", "A-Z", NULL) < 0)
{
perror("tr exec error");
exit(EXIT_FAILURE);
}
}
if(pid < 0)
{
perror("Fork error");
exit(EXIT_FAILURE);
}
return 0;
}
Program doesn't stop . I have no idea why . It should execute ls -al | tr a-z A-Z and write it in file a.txt.
And if someone can , please explain me how to do ls-al | tr a-z A-Z | tr A-Z a-z > a.txt . For another tr I need second mkfifo right ? I'm not sure how it's working and if should i close write or read descriptor here . With "pipe" it was nesesery.
Thanks for any help !