Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Created my own shell need help processing a sequence of commands/programs separated by ";"

Posted on 2006-06-17
Medium Priority
Last Modified: 2010-04-15
Ok i have created my own shell but i am having trouble creating it to process a sequence of commands/programs separated by ";"

This is sofar what i have could you please just tell me what to add to my code to make this work?

#include <stdio.h>
#include <string.h>
#include <unistd.h>

pid_t myFork();
int readArgs(const char*,char*[]);

int main (int argc, char *argv[]){
      pid_t pid;
      char line [255];
      char *argList[20];
      int ampersand, status, i;

      printf ("This program executes commands and \n");
      printf ("progams for you \n");

        printf ("To exit, enter CTR-C, or enter\n");
        printf ("a program name with its arguments> ");
      if ((pid = myFork ()) == -1){
         perror("impossible to fork");
      if (pid>0)       //This is the parent
        if (ampersand) //Background execution
            printf("Process [%d]\n", pid);
            waitpid(pid, &status,0);
            printf("My child has terminated\n");
        else            //this is the child
            if (execvp(argList[0], argList)==-1){
            perror("child Process");

pid_t myFork(){
      static int count=0;

      if (count<=20)

int readArgs(const char *line, char *argList[]){
      static int yes=0;
      int i=0, offset=0;
      char name[50];
      int found=0;

      while (yes & argList[i] !=NULL)
      i=0;            //reset i to ZERO
      while(sscanf(line+offset, "%s", name)==1){
            argList[i] = (char *)malloc(strlen(name)+1);
            while(line[offset]==' ') offset++;  //skip blanks
            offset += strlen(name);
      if (!strcmp(argList[i-1], "&")){
            argList[i-1] = NULL;
            found = 1;
            if (argList[i-1][strlen(argList[i-1])-1]=='&'){
              found = 1;
            argList[i] = NULL;

Question by:dush_1986
  • 3
LVL 14

Expert Comment

ID: 16927558
first something else;

>     static int yes=0;

>     while (yes & argList[i] !=NULL)
>       free(argList[i++]);

I asume you do this because you got errors the first time you free argList? It is better to initialize argList before you use it.

     char *argList[20];
LVL 14

Accepted Solution

cwwkie earned 400 total points
ID: 16927565
> to process a sequence of commands/programs separated by ";"

there are diffent ways to do that, but I think it is the easiest if you use strtok to split the string (http://cplusplus.com/ref/cstring/strtok.html), and than process each substring the usual way.

Expert Comment

ID: 16988354

I started to debug your program and ended up in writing a new one :-) In the following program "input" variable in main() represents the format which you are trying to parse (i am not mistaken).

The general idea is that in the main program tokenize my input on ";". Hence each token will represent a program name and its respective arguments which will be taken care by construct_cmd function.
Each command is represented by a Command struct. Which is the output of construct_cmd function. A Command Struct contains all what your program needs i.e. program name, its arguments and a boolean
idicating whether it should run in background or not.

Have a look at it. If you dont understand feel free to ask.

typedef enum bool

typedef struct
      char      *cmd_name;
      char      **args;
      int      nargs;
      bool      run_in_bg;      

void free_cmd(Command *cmd2free);
void print_cmd(Command *cmd2print);
Command *construct_cmd(const char *cmd);

int main(int argc, char *argv[])
      char            *input = strdup("prog p1_o1 p1_o2 p1_o3 &;     prog2 p2_o1 ; prog3; prog4 &");
      char            *buf;
      char            *str;
      Command      *cmds[10];
      int            i = 0;
      char            *tok;
      int            ncmds = 0;

      // create a modifiable copy of input
      str = strdup(input);

      tok = strtok_r(str, ";", &buf);

      for (i = 0; tok != NULL; i++)
            cmds[i] = construct_cmd(tok);
            tok = strtok_r(NULL, ";", &buf);

      ncmds = i;

      // print all cmds

      for (i = 0; i < ncmds; i++)

      // free all cmds
      for (i = 0; i < ncmds; i++)


      return 0;

Command *construct_cmd(const char *cmd_str)
      char      *tok;
      char      *buf;
      int      nalloc = 2;
      char      **cmd_args;
      int      i = 0;

      Command *cmd = (Command *)malloc(sizeof(Command));
      memset(cmd, 0, sizeof(Command));

      cmd_args = (char **)malloc(nalloc * sizeof(char *));
      // create a modifiable copy of cmd
      char      *str = strdup(cmd_str);

      // a command is given as <prog_name> <option_list>
      // so now tokenize str on the basis of spaces

      tok = strtok_r(str, " ", &buf);
      // first token is always the program name
      cmd->cmd_name = strdup(tok);
      // the rest are its arguments
      tok = strtok_r(NULL, " ", &buf);
      while (tok != NULL)
            // special case
            if (strcmp(tok, "&") == 0)
                  cmd->run_in_bg = true;
                  if (i == nalloc)
                        nalloc *= 2;
                        cmd_args = (char **)realloc(cmd_args, nalloc * sizeof(char *));

                  cmd_args[i++] = strdup(tok);

            tok = strtok_r(NULL, " ", &buf);

      cmd->nargs = i;

      if (cmd->nargs > 0)
            // deallocate extra cmd_args elements
            for (i = i; i < nalloc; i++)

            cmd->args = cmd_args;


      return cmd;

void print_cmd(Command *cmd2print)
      int      i;
      printf("Name = %s\n", cmd2print->cmd_name);
      printf("Nargs = %d\n", cmd2print->nargs);
      printf("RunInBg = %s\n", cmd2print->run_in_bg==true?"true":"false");

      for (i = 0; i < cmd2print->nargs; i++)
            printf("arg[%d] = %s\n", i, cmd2print->args[i]);

void free_cmd(Command *cmd2free)
      int      i;

      for (i = 0; i < cmd2free->nargs; i++)

LVL 14

Expert Comment

ID: 16995079

you should not give full answers to homework questions (see http://www.experts-exchange.com/help.jsp#hi130)
But there is no problem helping and guiding to an answer.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question