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

  • Home
  • SEARCH
  • 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 6711283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:07:17+00:00 2026-05-26T08:07:17+00:00

I want to execute a program from my code, and supply it with environment

  • 0

I want to execute a program from my code, and supply it with environment variables and arguments. AFAICT, execve is the right choice.

But, execve receives a path argument, not a filename, meaning it expects the first argument to be a path to the executable.

I know I can parse $PATH myself to find the path, but really, is there no other choice? Has no one else implemented it somewhere for me to use?

  • 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-26T08:07:18+00:00Added an answer on May 26, 2026 at 8:07 am

    Some systems may provide execvpe(). A Google search for ‘execvpe’ shows a variety of options, including at least one implementation (considerably more complex than what follows, but it includes most of execvp() in its own code).

    For those that do not, you can provide it for yourself:

    int execvpe(const char *program, char **argv, char **envp)
    {
        char **saved = environ;
        int rc;
        environ = envp;
        rc = execvp(program, argv);
        environ = saved;
        return rc;
    }
    

    You probably could survive without rc (just forcibly returning -1) since execvp() only ever returns -1 (and it only ever returns on an error).

    You probably do not even have to worry about thread safety in this code. The normal scenario where it is used will be in a child process just after a fork(), and at that point, there is only one thread in the process. If you think you may use it when there are multiple threads around, then you need to think rather carefully about whether it is safe to modify the global environment even briefly. Clearly, if the execvp() succeeds, there won’t be a problem (all the threads will be abruptly terminated). If the execvp() fails, then maybe one of the other threads would see the modified environment and might make bad decisions based on that. In which case, you would need to protect the environment appropriately (and that probably involves (mutual exclusion) locking in getenv(), setenv() and putenv() as well as in execvpe()).

    (The implementation of execvpe() that I found avoids the thread-safety issues by implementing execvp() logic and then using execve() to execute the program.)

    Normally, if execvpe() returns, the process will exit, so very often reinstating the environment is not going to affect the program. However, it is better to be safe than sorry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute a batch file from a java program. I am using
I have a small program I want to execute to test something #include <map>
How can I execute an external program like an exe file? I want to
So far to execute a Python program, I'm using > python file.py I want
I want to execute a php-script from php that will use different constants and
I want to execute an SQL query like: select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2'; Now the
I want to execute some Python code, typed at runtime, so I get the
I want to exceute a simple command which works from the shell but doesn't
I was looking into using some .NET code from within a Delphi program, I
From inside a PHP program I want to know the location of the binary

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.