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 6794363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:12:47+00:00 2026-05-26T18:12:47+00:00

Say in C, I want to call execvp() on any string command. Command can

  • 0

Say in C, I want to call execvp() on any string command. Command can just be:

char command[] = "ls -l";
char command[] = "rm *.txt";
char command[] = "cat makefile";

I want to put this command variable inside execvp(). So the exec() flavored function can just run with any kind of arbitrary command.

How can I do that? Thanks.

NOTE: system() is not allowed.

  • 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-26T18:12:48+00:00Added an answer on May 26, 2026 at 6:12 pm

    If you have to call execvp(), then you will need to split up those strings into an executable name and an array of arguments (the first being the “name” of the program and the last being a NULL pointer).

    That means something like:

    char cmd1[] = "ls";  char *args1[] = {"ls", "-l", NULL};
    char cmd1[] = "rm";  char *args1[] = {"rm", "*.txt", NULL}; // but see
                                                                // globbing below.
    char cmd1[] = "cat"; char *args1[] = {"cat", "makefile", NULL};
    

    This is a non-trivial exercise, especially if you want to allow for quoting, globbing, escaping and so forth.

    Quoting means you’ll have to be careful with commands like:

    rm "file with spaces.txt"
    

    in that you can’t simply break on the spaces – you’ll have to interpret items in the command much the same as the shell does. Simplistic breaking on spaces would give you a command with three arguments for that string above, rather than the correct one.

    By globbing, I mean you’ll almost certainly have problems with something like *.txt since it’s typically the shell which expands these arguments. Passing that directly to execvp() will result in a single argument of literally *.txt rather than many arguments matching all the text files in your current directory.

    Quoting means that you’ll have to handle things like:

    ls -l "file with spaces and \" quote in it"
    

    which will further complicate your parser.

    Don’t get me wrong, it can be done, but it’s probably a damn sight easier just using system().

    If you’re still thinking of going the execvp() route, you’ll have to:

    • split the string into separate tokens (rather hard, since you have to handle quotes and escapes).
    • glob all the arguments, meaning that those with wildcards in them (and only ones that aren’t escaped or protected by virtue of being inside quotes) are expanded into multiple arguments.
    • construct the argument array, with the command at the front and a NULL at the end.
    • call execvp() with the parameters being first element in that array and the address of the array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a template called /sample/_mytemplate.gsp. If I want to call this template
I am trying to figure this out, I want to call several rows say
Lets say I have a CMakeLists.txt and I want to call another include another
How do I trigger a delay, let's say I want to call a method
Say I have a function foo that I want to call n times. In
Say I want to get the HTML of http://www.google.com as a String using some
Say I want to have a method that takes any kind of number, is
I have a file a.txt with lines of commands I want to run, say:
I want to call a method which returns two values basically lets say my
Lets say I have a C++ program and I want to call functions from

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.