We help IT Professionals succeed at work.
Get Started

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

dush_1986 asked
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;

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE