我創建了一個shell,它在我使用system(1)時有效,但規格說不適用。 我想在最後使用execvp,我並不太確定如何去做。任何幫助的機會,將不勝感激。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);
}
你的問題到底是什麼? – DevSolar 2012-08-08 10:37:48
在進程完成之前使用fork()和waitpid不是更好嗎? – stetro 2012-08-08 10:41:19
我很害怕這些問題的措辭 - 我正在尋找正確的exe命令在這裏使用。 – 2012-08-08 12:07:32