2013-12-09 2 views
2

저는 C에서 꽤 기본적인 UNIX 쉘에서 작업하고 있습니다.이 프로젝트에서는 실제 쉘 명령어를 실행하기 위해 fork()execvp()을 사용하려고 시도하고 있습니다. 하나의 인수 (예를 들어, ls -lecho howareyoutoday 완벽하게 작동)와 같은 명령으로 잘 작동하는 것처럼 보이지만 여러 인수가있는 명령을 실행할 수없는 문제가 발생합니다 (echo how are you today 실행되지 않습니다). 이 문제의 원인을 찾는 데 도움이되도록 내 코드/근거를 안내 할 것입니다.execvp가 여러 인수없이 실패했습니다.

   char *token; 
       int count=0; 
       pid_t childProc; 
       int childStatus; 
       pid_t tpid; 
       char* argv[256]; 
       int i=1; 

         childProc = fork(); 

          if (childProc==0) { 
            //CHILD PROCESS IS HERE 
            argv[0] = malloc(strlen(token)); 
            argv[0] = token; 

            token=strtok(NULL," \n\t()<>|&;"); 
            while(token!=NULL) { 
              argv[i]=malloc(strlen(token)); 
              argv[i]=token; 
              token=strtok(NULL," \n\t()<>|&;"); 
              i++; 
            } 
            execvp(argv[0],argv); 

            //if this executes execvp fails 
            printf("failure to execute\n"); 
            i=0; 
            exit(0); 
          } 
          else { 
            //PARENT PROCESS IS HERE 
            do { 
              tpid = wait(&childStatus); 
            } while(tpid != childProc); 
          } 

따라서 기본 자식 전화 번호 fork() 호출로 시작합니다. 그 자식 프로세스에서, 나는 argv 어레이의 첫 번째 요소에 메모리를 할당한다. 이전의 strtok 호에서 비롯된 token 호는 에 할당됩니다. 새 token이 생성되어 다음 argv 요소에 추가됩니다. 남은 토큰에 대해이 과정을 반복합니다.

argv 배열이 완료되면 execvp이 호출되고 첫 번째 인수는 명령 이름을 포함하고 두 번째 인수는 전체 argv 배열입니다. 명령이 실행되지 않으면 execvp이 리턴되고이를 표시하는 메시지가 인쇄됩니다.

왜 내가 위에서 언급 한 여러 인수 문제가 있는지 알 수 없습니다. 어떤 도움이나 제안이라도 대단히 감사하겠습니다!

#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main() { 
    char buffer[256]; 
    char *token; 
    int count=0; 
    pid_t childProc; 
    int childStatus; 
    pid_t tpid; 
    char* argv[256]; 
    int i=1; 
    int j=0; 

    while(1) { 
      fgets(buffer, 256, stdin); 

      token=strtok(buffer," \n\t()<>|&;"); 

      while (token==NULL) { 
        fgets(buffer,256,stdin); 
        token=strtok(buffer," \n\t()<>|&;"); 
      } 

        if (strcmp(token,"exit")==0) { 
          exit(0); 
        } 
        else if (strcmp(token,"cd")==0) { 
          token = strtok(NULL, " \n\t()<>|&;"); 

          if (chdir(token)!=0) { 
            perror("Error: "); 
          } 
        } 
        else { 
          childProc = fork(); 

          if (childProc==0) { 
            argv[0] = malloc(strlen(token)); 
            argv[0] = token; 

            token=strtok(NULL," \n\t()<>|&;"); 
            while(token!=NULL) { 
              argv[i]=malloc(strlen(token)); 
              argv[i]=token; 
              token=strtok(NULL," \n\t()<>|&;"); 
              i++; 
            } 
            execvp(argv[0],argv); 

            //if this executes execvp fails 
            printf("failure to execute\n"); 
            i=0; 
            exit(0); 
          } 
          else { 
            do { 
              tpid = wait(&childStatus); 
            } while(tpid != childProc); 
          } 
        } 
    } 
} 

답변

0

당신은 execvp에 인수 문자열을 종료 null로 할 필요가 다음과 같이

는 참고로, 전체 프로그램의 코드입니다.

if (childProc == 0) 
{ 
    argv[0] = malloc(strlen(token)); 
    argv[0] = token; 

    token = strtok(NULL, " \n\t()<>|&;"); 

    while (token != NULL) 
    { 
     argv[i] = malloc(strlen(token)); 
     argv[i] = token; 
     token = strtok(NULL, " \n\t()<>|&;"); 
     i++; 
    } 

    argv[i] = NULL; //<--- insert here 

    if (execvp(argv[0], argv) == -1) 
    { 
     printf("failure to execute because %s\n", strerror(errno)); 
     exit(0); 
    } 
}