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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:19:22+00:00 2026-06-04T06:19:22+00:00

I am building a shell and am having some trouble with the system call

  • 0

I am building a shell and am having some trouble with the system call ‘execvp’. I saw some other questions on this topic but they were vague, and did not appear to be completely addressed (whoever asked the questions didn’t provide much information and didn’t get good answers).

Obviously I have my own command line and I’m reading user input from stdin like

mysh some/path $ ps -a 

and I am building an args array as a char ** and the array itself works (I think) since when I print out the values within my function it shows

args[0] = 'ps'
args[1] = '-a'
args[2] = '(null)'

So, I call fork and execvp(cmnd, args) within my process, where cmnd is “ps” and args is as above, and perror etc.

I get

'Error: no such file or directory.'  

Do I need to put in the $PATH variable? Am I doing something else whacky?

Here’s my code for generating the args array:

char ** get_args(char * cmnd) {
int index = 0;
char **args = (char **)emalloc(sizeof(char *));
char * copy = (char *) emalloc(sizeof(char)*(strlen(cmnd)));
strncpy(copy,cmnd,strlen(cmnd));
char * tok = strtok(copy," ");
while(tok != NULL) {
    args[index] = (char *) emalloc(sizeof(char)*(strlen(tok)+1));
    strncpy(args[index],tok,strlen(tok)+1);
    index++;
    tok = strtok(NULL," ");
    args = (char**) erealloc(args,sizeof(char*)*(index+1));
}
args[index] = NULL;
return args;
}

(emalloc and erealloc are just malloc and realloc with error checking built in)

So then I do this:

void exec_cmnd(char*cmnd, char**args) {
pid_t pid;
if((pid=fork())==0) {
    execvp(cmnd, args);
    perror("Error");
    free(args);
    free(cmnd);
    exit(1);
}
else {
    int ReturnCode;
    while(pid!=wait(&ReturnCode)) {
        ;
    }
}
}

And like I said above, when execvp is called inside my process it fails when I supply any arguments whatsoever but works without them (i.e. when argv == {‘ps’, NULL} )

If you need more information don’t hesitate to ask. I need to solve this.

  • 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-06-04T06:19:23+00:00Added an answer on June 4, 2026 at 6:19 am

    It think you passed whole command line in first argument to execvp

    you have to separate first token(command name) from cmnd to pass as first argument of execvp

    You can call it as

    execvp(args[0], args);

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

Sidebar

Related Questions

I'm having some problems with this system call and probably with kmalloc. Well ,
I was having some other problems with some ports building for the wrong CPU
I've hit a road block while building this shell program which includes some javascript
Our internal build system uses a shell script to setup the environment for building
I'm building a Shell Script that has a if function like this one: if
I am having problems building a library in VC9, but which was previously successfully
I'm building a pet software that the description is: a shell on the web.
I'm building a silverlight project based on the Prism 2.1 version. Added a shell
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a search with some custom objects and three scopes: All , Active ,

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.