¿Cómo usar execvp() con grep?

I am trying to exec() a call to grep inside of my C program. I am looking for the number of occurrences of a string pattern in a given file, which can be achieved with:

$ grep -e my_string -F my_file.txt -c

Which gives a number output as expected, such as:


However, what I hoped would be equivalent seems to be having some difficulties. The following code:

char *arg_list[] = {"-e", "my_string", "-F", "my_file.txt", "-c", "\0"};

int pid;
if ((pid = fork()) == 0) {
    if (execvp("grep", arg_list) < 0)

Resultados en:

 -e: : No such file or directory

There are two issues. First, I just want the number-- not the file in which it is found. Second, I'm not sure why it is trying to assert that the -e flag needs to be a file. If I switch around the argument list to be:

char *arg_list[] = {"-F", "my_file.txt", "-e", "my_string", "-c", "\0"};

Then I get back:

-F: : No such file or directory

I'm not sure where to start looking for why this is happening. I'm also not sure why the output of the grep call from the command-line is just a number, but the exec()'d grep call outputs both the filename and number.

¡Cualquier ayuda sería apreciada! Gracias.


Solution: include the program name in the argument list, or simply use the system() call. Also, RTFM.

preguntado el 03 de mayo de 12 a las 20:05

Do you really need to use execvp? Maybe try just using system(). -

Also, try putting "grep" as the first arg in your args_list. -

Both work perfectly. It's an exercise in using exec(), that's all. But thank you for pointing out what was right under my nose! -

1 Respuestas

You have two problems (insert JWZ quote about regular expressions here):

  1. The first argument in the array passed exec se pasa como argv[0] to the new executable. Ordinarily, this is the name of the executable, and it's what programs use to figure out their own name. So you're telling grep that its name is -e, and its arguments are my_string -F my_file.txt -c "". It's not getting the -e argument like you might expect, so it misinterprets its arguments and gives you an error.

  2. You're not properly NULL-terminating the array of arguments to exec. The last argument must be a literal NULL, no una cuerda "\0". That string is an empty string, but it's not a null pointer.

contestado el 03 de mayo de 12 a las 21:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.