Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:57:51+00:00 2026-05-31T01:57:51+00:00

I am using execvp for execing a new process for the command grep -l

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:57:53+00:00Added an answer on May 31, 2026 at 1:57 am

    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:

    • Use functions which do start a shell (system(3), popen(3))
    • Use glob(3), essentially doing the shells job

    EDIT

    You could probably write it as this (untested):

    glob_t globbuf;
    globbuf.gl_offs = 3;
    
    /* Globbing magic. */
    glob("*", GLOB_DOOFFS, NULL, &globbuf);
    
    /* That's what the 3 was for. */
    globbuf.gl_pathv[0] = "grep";
    globbuf.gl_pathv[1] = "-l";
    globbuf.gl_pathv[2] = "night";
    
    /* Conveniently, the list is NULL-terminated. */
    execvp("grep", globbuf.gl_pathv);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to replace the current process with a new one using os.execv, this
I am trying to spawn a new process using execve() from unistd.h on Linux.
I have a question about using os.execvp in Python. I have the following bit
I'm trying to solve a problem I've got where a child process runs execvp()
I'm having trouble using dup2 to make a c program execute a command such
I have compiled my application on Linux (Intel) machine using this command gcc –g
I am trying to create multiple processes using fork() and execvp() calls, but so
I'm developing a program which executes a program using execvp. It needs to capture
Say I want to spawn a process and run execv to execute a command
I have a C++ function that I'd like to call using execvp(), due to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.