I'm trying to make a program which invokes the ls and grep system calls using exec. Specifically I have to execute ls > tmp; grep -c pattern < tmp in order to count the number of files that fulfill the pattern. As you can see I save the content of ls in tmp file and then I want to use grep to count the files.
Let's supose pattern = txt. I'm trying things like the following code:
char *a = "ls > tmp";
char *b = " -c ";
char *fin = " < tmp";
char *comanda;
if((comanda = malloc(strlen(pattern)+strlen(pattern)+1)) != NULL){
  comanda[0] = '\0';   // ensures the memory is an empty string
  strcat(comanda,b);
  strcat(comanda, pattern);
  strcat(comanda,fin);
} else {
    return -1;
}
ret = execl("/bin/sh","sh","-c",a,NULL);
ret = execl("/bin/sh","sh","-c",comanda, NULL);
But it shows me the following error: ls: cannot access  > tmp: No such file or directory. So I don't know how to get the value of grep, because the execl function does not return the value, so how can I achieve the grep value?
 
     
     
    