I'm trying a write a little program to run grep via execvp. This is basically identical to the problem I had here, but in this case, it only happens when I run grep (as opposed to echo, ls, emacs, etc which all work fine) and I've changed the incorrect wait() (I believe). I've also tried using quotes over the text I want to find
my code:
int main(void) {
int i;
char inputprogram[50];
char vars[50] = "a search.txt";
printf("input grep\n");
fflush(stdout);
fgets(inputprogram,50,stdin);
for(i = 0; i < 50; i++){
if(inputprogram [i] == '\n' ){
inputprogram[i] = 0;
}
}
char *arg [] = {inputprogram, vars , NULL};
printf(">%s<\n", arg[1]);
printf(">%s<\n", arg[0]);
int status = 0;
pid_t child;
(child = fork());
if(child == 0){
printf("execute\n");
execvp(inputprogram, arg);
exit(1);
}
else{
printf("parent waiting...\n");
wait(&status);
}
return EXIT_SUCCESS;
}
search.txt:
a
b
c
abc
input/output (# is in front of lines I typed, though is not part of the input):
shell> # ./work
input grep
# grep
>a search.txt<
>grep<
parent waiting...
execute
# a;dlghasdf
# go back
# :(