0

I have created a shell, and it works when I use system(1), but the specs say not to. I am trying to use execvp at the end and I'm not really too sure on how to do it. Any chance of help would be appreciated.

Code ->

char *token = NULL;
char line[LINE_MAX];
char *line2 = NULL;
char *tempraryToken = NULL;
char *command = NULL;
char args[LINE_MAX];    

int numSpaces = 0;
int i;
int strleng = 0;
while( 1 )
{
    if( scanf(" %[^\n]", line) > 0) ) //prune off the newline char 
        token = strtok( line, ";" )   //break up different commands 
                                      //on the same line by ;
        do{
            strleng = strlen(token);
            for( i = 0; i < strleng; i++ )
            {
                if(token[i] == ' ') numSpaces++; //find out if there are spaces
            }
            i = 0;
            if( numSpaces >= 1 ) //if there are spaces
            {
                 line2 = token;
                 temporaryToken = strtok( line2, " ") //break by spaces
                 do{
                     //if it's before any spaces
                     if(i == 0){
                         command = temporaryToken;
                     }
                     else strcat(args, temporaryToken);
                 strtok( NULL, " ");
                 while (temporaryToken != NULL);
            }
        execvp(command, args); //this could be any of the exe commands
                               //that's what I'm looking for
        token = strtok( NULL, ";" ) //move to next token
        while( token != NULL );
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sam P
  • 453
  • 6
  • 19

1 Answers1

3

Basically, to keep running after execvp you need to fork your process, run execvp in child and wait or waitpid for it in parent. Having this in mind do some research of your own ;-)

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173