2014-04-08 6 views
0

기본 쉘을 만들고 있는데, 정상적으로 작동하지만 한 가지 문제가 있습니다. 현재와 ​​같은 args를 제공하면execvp - 이스케이프 처리 된 공백 문자를 사용하여 arg 처리하기

cat testtextfile 

execvp 명령이 정상적으로 작동합니다. 그러나 내가 비슷한 것을 제공한다면

cat hello\ world 

즉. 이름에 공백이있는 파일이 있으면 명령이 작동하지 않습니다. execvp에 이런 종류의 인수를 이해하게하려면 어떻게해야합니까?

다음 I [1] "-n"와 같은 뭔가 args3 변경하는 경우 작동 않는, 내 코드의 추출물 :

char *args3[3]; 

args3[0] = "cat"; 
args3[1] = "hello\\ world"; 
args3[2] = NULL; 

if ((child = fork()) == 0) { //child 
    printf("pid of child = %ld\n", (long) getpid()); 
    execvp(args3[0], args3); //arg[0] is the command 

    fprintf(stderr, "execvp failed \n"); 
    exit(1); 
} else { //parent 

    if (child == (pid_t)(-1)) { 
     fprintf(stderr, "fork failed.\n"); exit(1); 
    } else { 

     if (doNotWait == 0){ 
      c = wait(&cstatus); //wait for child 
      printf("child %ld exited with status = %d\n", 
      (long) c, cstatus); 
     } else { 
      printf("not waiting for child process\n"); 
     } 
    } 

} 
return 0; 
+0

코드를 입력해야합니다. Execvp는이 유형의 일에 대해 완전히 안전해야하므로 확률이 호출 코드의 문제입니다. – DrC

+0

몇 가지를 추가했습니다. – timeshift117

답변

1
args3[1] = "hello\\ world"; 

이어야

args3[1] = "hello world"; 

cat hello\ world에서 "hello world"를 두 단어로 취급하지 않으려면 "hello"와 "word"사이의 공백을 이스케이프 처리해야합니다. 따라서 쉘이 명령 행 인수를 올바르게 처리 한 후에 cat의 첫 번째 인수는 hello\ world이 아닌 hello world이어야합니다.