#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ctype.h>
#define MAX 5000
int main( int argc, char *argv[])
{ int pipen[2];
pipe(pipen);
int pipe2[2];
pipe(pipe2);
char buf[MAX];
pid_t child_pid;
if((child_pid = fork()) < 0 )
{
perror("fork failure");
exit(1);
}
if(child_pid == 0)
{
close(pipen[1]);
close(pipe2[0]);
dup2(pipen[0],0);
dup2(pipe2[1],1);
close(pipen[0]);
close(pipe2[1]);
execl("/usr/bin/tr","/usr/bin/tr","[:lower:]", "[:upper:]",NULL);
}
else
{
FILE* inputfile;
inputfile = fopen("in","r");
char c;
if (inputfile) {
int i=0;
while ((c = getc(inputfile)) != EOF){
buf[i]=c;
i++;
if(i>=MAX){break;
buf[i-1]='\0';
}
}
buf[i]='\0';
fclose(inputfile);
}
close(pipen[0]);
close(pipe2[1]);
write(pipen[1],buf,sizeof(buf));
close(pipen[1]);
read(pipe2[0],&buf,sizeof(buf));
close(pipe2[1]);
printf("----->\n%s\n",buf);
}
return 0;
}
This is one way to solve this problem,As I know we can directly write
execl("/bin/ls","ls","/home/Martin/Documents","-l",NULL); or something but cant
directly write tr command which you have mentioned, That is why I have used dup2 and fork system calls to do the job as you want. I am also new to these stuff .If anything went wrong please elaborate :)