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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:55:55+00:00 2026-06-09T21:55:55+00:00

Making a very basic shell given as assignment. Assuming I have a string ls

  • 0

Making a very basic shell given as assignment.
Assuming I have a string "ls a b c | grep a"

How can I get something like this

commands[0][0] = "ls"
commands[0][2] = "a"
commands[0][2] = "b"
commands[0][3] = "c"
commands[0][4] = NULL

commands[1][0] = "grep"
commands[1][1] = "a"
commands[1][2] = NULL

First split on "|" and then split further on spaces
and hence be able to execute execvp(commands[0][0], commands[0])

I can do single splitting just fine but this double splitting is somewhat troublesome

  • 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-09T21:55:57+00:00Added an answer on June 9, 2026 at 9:55 pm

    I have not tested with execvp

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #define WHITESPACE 1
    #define ARGUMENT 2
    #define COMMAND 3
    void process_command(const char *string)
    {
       char *argument = (char *) 0;
       char *args[100];
       char *const * commands[10];
       int commands_index = 0;
       int char_index = 0;
       int args_index = 0;
       int state = WHITESPACE;
       commands[commands_index] = &args[args_index];
       for (const char *p = string; *p != '\0'; p++)
       {
          if ( *p != ' ' )
          {
             if ( *p == '|' )
             {
                if ( state == ARGUMENT)
                {
                   argument[char_index] = '\0';
                   char_index = 0;
                }
                state = COMMAND;
                args[args_index] = 0;
                args_index++;
                commands_index++;
                commands[commands_index] = &args[args_index];
             }
             else
             {
                if ( state != ARGUMENT )
                {
                   argument = malloc(100);
                   args[args_index] = argument;
                   args_index++;
                   state = ARGUMENT;
                }
                argument[char_index] = *p;
                char_index++;
             }
          }
          else 
          {
             if ( state == ARGUMENT)
             {
                argument[char_index] = '\0';
                char_index = 0;
             }
             state = WHITESPACE;
          }
       }
       argument[char_index] = '\0';
       args[args_index] = 0;
       //execvp(commands[0][0],commands[0]);
       //execvp(commands[1][0],commands[1]);
    
       for (int i = 0; i <= commands_index; i++)
       {
          int j = 0;
          for (; commands[i][j] != 0;j++)
          {
             printf("commands[%d][%d] = \"%s\"\n",i,j,commands[i][j]);
          }
          printf("commands[%d][%d] = NULL\n",i,j);
       }
       printf("\n");
    }
    
    
    
    int main(void)
    {
    
       const char *string1 = "ls a b c | grep a";
       const char *string2 = "ls -al|grep txt";
       const char *string3 = "cat file.txt | grep hello |more";
       process_command(string1);
       process_command(string2);
       process_command(string3);
    }
    

    output:

    commands[0][0] = "ls"
    commands[0][1] = "a"
    commands[0][2] = "b"
    commands[0][3] = "c"
    commands[0][4] = NULL
    commands[1][0] = "grep"
    commands[1][1] = "a"
    commands[1][2] = NULL
    
    commands[0][0] = "ls"
    commands[0][1] = "-al"
    commands[0][2] = NULL
    commands[1][0] = "grep"
    commands[1][1] = "txt"
    commands[1][2] = NULL
    
    commands[0][0] = "cat"
    commands[0][1] = "file.txt"
    commands[0][2] = NULL
    commands[1][0] = "grep"
    commands[1][1] = "hello"
    commands[1][2] = NULL
    commands[2][0] = "more"
    commands[2][1] = NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a very basic menu but I can't seem to get it to
I'm making some frontend experiments and I'd like to have a very basic webserver
I'm making a very basic menu for my site and I have used the
This should be simple. I'm making a very basic app, based on the Utility
This seems so basic but for some reason I can't get it to work
Ok, I'm making a very basic vb.net winforms app, essentially you can drag files
I am making a very basic webpage, consisting of some text inside a fixed
Alright, here goes. I'm making a very basic app, and I want the user
So i am running into a very odd error. I am making a basic
I was making this very simple lex program (just an introductory program). But on

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.