2012-08-08 123 views
0

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

你的问题到底是什么? – DevSolar 2012-08-08 10:37:48

+0

在进程完成之前使用fork()和waitpid不是更好吗? – stetro 2012-08-08 10:41:19

+0

我很害怕这些问题的措辞 - 我正在寻找正确的exe命令在这里使用。 – 2012-08-08 12:07:32

回答

3

基本上,让你需要fork的过程中,父母经营儿童execvpwaitwaitpidexecvp后运行。考虑到这一点,做一些你自己的研究;-)

+0

您还应该输入正确的输入和输出! – stetro 2012-08-08 10:45:10

+1

嗯,是的,如果你需要与其输入和输出进行通信。 – 2012-08-08 10:48:11