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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:53+00:00 2026-05-25T23:39:53+00:00

I am trying to use execve to run the ls command. Currently I’m running

  • 0

I am trying to use execve to run the ls command. Currently I’m running it with the following arguments:

execve(args[0], args, env_args)
//args looks like {"ls", "-l", "-a", NULL}
//env_args looks like {"PATH=/bin", "USER=me", NULL}

What I expected this to do was run the ls command using my new env_args meaning that it would look up ls in my PATH. However, this code actually doesn’t do anything and when I run the code it just returns to my command prompt without output.

Using the same args[] I was using execvp and ls worked and searched my current path.

Can you tell me what I am doing wrong?

What I am trying to do is write my own shell program where I can create and export my own environment and have exec use the environment I have defined in a char**. Essentially I am writing my own functions to operate on env_args to add and remove vars and when I call exec i want to be able to call exec on {“ls”, “-l”, NULL} and have it look down my new environments path variable for a valid program called ls. I hope this explains what I am doing a little better. I don’t think the extern environ var will work for me in this case.

  • 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-25T23:39:54+00:00Added an answer on May 25, 2026 at 11:39 pm

    The execve() function does not look at PATH; for that, you need execvp(). Your program was failing to execute ls, and apparently you don’t report failures to execute a program after the execve(). Note that members of the exec*() family of functions only return on error.

    You’d get the result you expected (more or less) if you ran the program with /bin as your current directory (because ./ls – aka ls – would then exist).

    You need to provide the pathname of the executable in the first argument to execve(), after finding it using an appropriate PATH setting.

    Or continue to use execvp(), but set the variable environ to your new environment. Note that environ is unique among POSIX global variables in that is it not declared in any header.

    extern char **environ;
    
    environ = env_args;
    execvp(args[0], &args[0]);
    

    You don’t need to save the old value and restore it; you’re in the child process and switching its environment won’t affect the main program (shell).


    This seems to work as I’d expect – and demonstrates that the original code behaves as I’d expect.

    #include <stdio.h>
    #include <unistd.h>
    
    extern char **environ;
    
    int main(void)
    {
        char *args[]     = { "ls", "-l", "-a", NULL };
        char *env_args[] = { "PATH=/bin", "USER=me", NULL };
    
        execve(args[0], args, env_args);
        fprintf(stderr, "Oops!\n");
    
        environ = env_args;
        execvp(args[0], &args[0]);
        fprintf(stderr, "Oops again!\n");
    
        return -1;
    }
    

    I get an ‘Oops!’ followed by the listing of my directory. When I create an executable ls in my current directory:

    #!/bin/sh
    echo "Haha!"
    

    then I don’t get the ‘Oops!’ and do get the ‘Haha!’.

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

Sidebar

Related Questions

I'm trying use epoch time dates in my series data. The array looks like
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use something like the below with a char array but it doesn't
Trying to use a DataGridView like the old VB6 FlexGrid, and add the coloumns
I'm currently trying to have my C program read Unix arguments from the user.
I revieving the following error when trying use mysql_real_escape() function Fatal error: Call to
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I am trying use a Java Uploader in a ROR app (for its ease
Trying to use an excpetion class which could provide location reference for XML parsing,

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.