我创建了一个 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 );
}