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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:44:56+00:00 2026-05-25T02:44:56+00:00

If you call store_commands() method anywhere in my code, execution of the command fails

  • 0

If you call store_commands() method anywhere in my code, execution of the command fails for some reason.

such as my main method

int main (int argc,  char * argv[]) {

        char  *command;

        store_commands(); // problem


        while ( (command = readline(" $ "))!= NULL ) {  // scan stdin  

                rl_bind_key('\t',rl_complete);


                splitCommands(&mainVars, command, argv);   
        }


        return 0;
}

my store commands method

void store_commands() {
        char *newEnv;
        DIR * dir;
        char *new ;          
        struct dirent * entry;
        char *env = getenv("PATH");
        do { 
                newEnv = strsep(&env, ":");

                if(newEnv != NULL) 
                        if(strlen(newEnv) > 0) {

                                dir = opendir(newEnv);
                                if( dir == NULL ) break;
                                if(flag == 1) {
                                        flag = 0;
                                        while((entry = readdir(dir)) != NULL) {
                                                new = malloc(strlen(entry->d_name) + 1) ;  
                                                new = strcpy(new, entry->d_name);

                                                commands[++count] = new;  // add possible commands into an array
                                                //printf("---%i %s\n", count ,commands[count]);
                                        }
                                }
                                closedir(dir); // close directory
                        }
        } while(newEnv);

}

test cases

without store_commands()

**ls**
comm[0]: 'ls' and comm[1]: '(null)' // command received here

Makefile        
main.c                      
target
libedit.2.dylib 

with store_commands()

**ls**
comm[0]: 'ls' and comm[1]: '(null)' // command received here again but....
Execution of the command is failed 
: No such file or directory 
  • 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-25T02:44:56+00:00Added an answer on May 25, 2026 at 2:44 am

    You are corrupting the environment with strsep. Call strdup on your env.

    A minimal example:

    #include <stdlib.h>
    
    int main ()
    {
        char* z = getenv("PATH");   // <---- z points to the actual env, not a copy
        *z = 0;                      // <---- corrupt the environment
        system("ls");                // <---- fail
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I call sortable.stop() to make an ajax call to store some data after drag
I call stored function with such signature: FUNCTION ADDDAYS (city VARCHAR2, startDate DATE, numDays
Suppose we have a DAL method public void BuyProduct(int productId, int quantity, int buyerId);
I have an interface method public void Execute(ICommand command); which needs to pass known
I have some code that allows administrators to manage their organization's users/groups etc and
I have the following code (using Python 2.7): # shared command line options, like
Is it possible to store the results of a call to exec sp_executesql in
I would like to store the cookies from one open-uri call and pass them
I want to call a JavaScript function through PHP and store the returned value
I'm trying to make a system call in Python and store the output 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.