0

我创建了一个 shell,当我使用 system(1) 时它可以工作,但规范说不可以。我试图在最后使用 execvp ,但我不太确定如何去做。任何帮助的机会将不胜感激。

代码 ->

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 );
}
4

1 回答 1

3

基本上,要在execvp需要fork进程后继续运行,请execvp在子进程中运行waitwaitpid在父进程中运行。考虑到这一点,自己做一些研究;-)

于 2012-08-08T10:41:42.960 回答