How would I use the dup2() and pipe() functions to commit a redirect of the output of ls into the input of wc. Therefore printing the word count.
here is my attempt, however this is not working.
if(!strcmp(*ptr,"|")){ //argv[] contains the input partitioned by spaces
//ignore the ptr for this case
char arr[2];
pipe(arr); // 0 is read 1 is write
dup2(stdout,arr[1]); // my redirected output
int a = fork();
if(a == 0){
close(arr[0]);
execvp(argv[0],argv);
}
wait(NULL);
dup2(0,arr[0]); // my redirected input
int b = fork();
if(b == 0){
close(arr[1]);
execvp(argv[2],argv);
}
wait(NULL);
close(arr[0]);
close(arr[1]);
return;
}