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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:59:33+00:00 2026-06-15T22:59:33+00:00

Well, I’m trying to write a shell for linux using C. Using the functions

  • 0

Well, I’m trying to write a shell for linux using C. Using the functions fork() and execl(), I can execute each command, but now I’m stuck trying to read the arguments:

char * command;
char ** c_args = NULL;

bytes_read = getline (&command, &nbytes, stdin);

command = strtok(command, "\n ");
int arg = 0;
c_arg = strtok(NULL, "\n ");
while( c_arg != NULL ) {
    if( c_args == NULL ) {
        c_args = (char**) malloc(sizeof(char*));
    }
    else {
        c_args = (char**) realloc( c_args, sizeof(char*) * (arg + 1) );
    }
    c_args[arg] = (char*) malloc( sizeof(char)*1024 );
    strcpy( c_args[arg], c_arg );
    c_arg = strtok(NULL, "\n ");
    arg++;
}
...
pid_t pid = fork()
...
...
execl( <path>, command, c_args, NULL)
...
...

That way I get errors from the command when I try to pass arguments, for example:

ls -l

Gives me:

ls: cannot access p��: No such file or directory

I know that the problem is the c_args allocation. What’s wrong with it?

Cheers.

  • 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-15T22:59:35+00:00Added an answer on June 15, 2026 at 10:59 pm

    You can’t use execl() for a variable list of arguments; you need to use execv() or one of its variants (execve(), execvp(), etc). You can only use execl() when you know all the arguments that will be present at compile time. In most cases, a general shell won’t know that. An exception is when you do something like:

    execl("/bin/sh", "/bin/sh", "-c", command_line, (char *)0);
    

    Here, you’re invoking the shell to run a single string as the command line (with no other arguments). However, when you’re dealing with what people type at the keyboard in a full shell, you won’t have the luxury of knowing how many arguments they typed at compile time.

    At its simplest, you should be using:

    execvp(c_args[0], c_args);
    

    The zeroth argument, the command name, should be what you pass to execvp(). If that’s a simple file name (no /), then it will look for the command in directories on your $PATH environment variable. If the command name contains a slash, then it will look for the (relative or absolute) file name specified and execute that if it exists, and fail if it does not. The other arguments should all be in the null-terminated list c_args.

    Now, there may also be other memory allocation issues; I’ve not scrutinized the code. You could check them, though, by diagnostic printing of the argument list:

    char **pargs = c_args;
    while (*pargs != 0)
        puts(*pargs++);
    

    That prints each argument on a separate line. Note that it doesn’t stop until it encounters a null pointer; it is crucial that you null terminate your list of pointers to the argument strings.

    This bit of your code:

    c_args[arg] = (char*) malloc( sizeof(char)*1024 );
    strcpy( c_args[arg], c_arg );
    

    looks like overkill in the usual case, and an inadequate memory allocation in the extreme cases. When you’re copying strings around, allocate enough length. I see that you’re using strtok() to bust apart a string — it’ll do for the early incarnations of a shell, but when you get to process command lines like ls -l>$tmp, you will find strtok()‘s penchant for trampling over your delimiter before you get to read it becomes a major liability. However, while you’re using it, you probably don’t have to copy the arguments like that; you can just set c_args[arg++] = result_from_strtok;. When you do need to copy, you should probably use strdup(); it doesn’t forget to allocate enough space for the trailing '\0', for example, and neither over-allocates nor under-allocates.

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

Sidebar

Related Questions

Well this kind of n00b question but I still can't figure it out. I
Well, can they? Like jQuery using a fadeIn function for something on a C++
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
Well I can start the thread when I press togglebutton to ON and it
Well, I have a application which somehow requires some system resources, but how do
well, I am trying to include a header file in my project, while the
Well, I was trying to fix this program, and I keep getting the errors
Well here is what I'm trying to do. I want to convert a array
Well, I'm trying to reuse a portion of C# code. It's an abstract class
Well I have a 50000+ row table so I can't load all rows into

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.