Hello, I need help making my shell in c. The shell needs to ignore spaces, this means that you can put the spaces you want an the command needs to work, I already resolved that problem, the next problem is that if you put ; two commands need to be recognized for example "ls ; ps", I need help in that problem

P.D : I need to use execvp and fork

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
	int pid;
	int i,j,k;
	char s[200];                      
	char command[20][10];            
	char **order;                     
	char *orderptr[20];               
	int m;
	int bol = 0;
	
	
	fflush(stdin);
	printf(">");
	gets(s);
	
	/* while a command is input, parse it and split into words of
	   different size, each is then stored in the command array */
	while (strlen(s)!=0) {
	  for (k=0; k<20; k++) command[k][0]=(char) NULL;
	  for (k=0; k<20; k++) orderptr[k]=NULL;
	  i=j=0;
	
      //Spaces at the end	
	  m = strlen(s)-1; 
	  while ( m >= 0){
		if(s[m] == ' '){
		s[m] = (char) NULL;
		m--;
		}else{
		break;
		}
		
	  }

	  bol = 0;
	  for (k=0; k<strlen(s); k++) {
	    if (s[k]!=' ') {
	      command[i][j]=s[k];
	      j++; 
		bol = 1;
	    }
	    else {
		
		if (bol == 1){
	      command[i][j]=(char) NULL;
	      orderptr[i]=&command[i][0];
	      i++; j=0;
		bol = 0;
		}
	    }
	  }

	  /* Indicate there is no other argument */
	  command[i][j]= (char) NULL;
	  /* Update pointers */
	  orderptr[i]=&command[i][0];
	  order = &orderptr[0];;
	  ptr = strtok(*order,";");
	   while(ptr != NULL){
		printf("%sn",ptr);
		ptr = strtok(NULL, " ");
	  }
         
	  if ((pid = fork())>0) wait(NULL);
	  else {
	    execvp(*order,order);
	    printf("There was an error executing that command\n");
	    }
	  fflush(stdin);
	  printf(">");
	  gets(s);
	}
}

Thank you

Consider this an exercise in parsing, more than anything else. Also, write out the process to use with pseudo code before you start to write any C code. So, here is a brief p-code version of what a shell does

const char* progname = 0;
while not done
    read line
    do
        eat white space
        read word
        if (progname is nil) set progname to word
        else add word to arglist
    while not end-of-line
    if (progname is not nil) /* We have a command to execute */
        if (progname is "exit") /* Done with shell */
            done = true;
        else
            use path environment and progname to find program file to execute
            if program file found
                if ((pid = fork()) == 0) /* We are in child */
                    status = execvp(progfilename, arglist);
                    /* Any status return means execvp() failed - see errno */
                else if (pid > 0) /* We are in parent - wait for death of child */
                    waitpid(pid, 0, 0);
                else if (pid == -1) /* Error forking - see errno */
                endif
            else
                /* Command file not found */
                /* Check for and execute internal commands if implemented */
            endif
        endif
    endif
end-while-not-done

As you can see, this has set out the steps we need to do in order to have a functioning shell. Of course, the devil's in the details, but until you have your thinking and processes in order, you are wasting your time. And in case you are interested, I wrote my first shell in C about 30 years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.