I am using execvp
for execing a new process for the command grep -l night *
. Here is my code:
char * argument[5];
char keyword[] = "night";
argument[0] = (char *) malloc (sizeof(char)*25);
argument[1] = (char *) malloc (sizeof(char)*25);
argument[2] = (char *) malloc (sizeof(char)*25);
argument[3] = (char *) malloc (sizeof(char)*25);
argument[4] = (char *) malloc (sizeof(char)*25);
argument[0] = "grep";
argument[1] = "-l";
strcpy(argument[2],keyword);
argument[3] = "*";
argument[4] = NULL;
execvp ("grep", argument);
But I am getting the output from this program as "grep: *: No such file or directory"
which is incorrect as executing the same command from the shell results in the list of
files which contain the text "night". I do suspect that the *
in the command list is being
sent as a string with quotes to exec
. Is my assumption correct? If so how can I rectify this problem? Any help would be highly appreciated.
Grep does not understand the "*" argument. Usually the shell expands such arguments (it's called globbing). Of course, since
exec
functions don't start a shell, you don't get that functionality.You can:
system(3)
,popen(3)
)glob(3)
, essentially doing the shells jobEDIT
You could probably write it as this (untested):