0

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

Edited by Straocaste: n/a

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by rubberman
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.