2013-09-25 4 views
1

셸을 만들었으며 명령 프롬프트에서 strtok과 공백 구분 기호를 사용하여 입력을 구문 분석했습니다. 나는 "cp x y"명령으로 작동하는 동안 ls 또는 ls -l과 같은 특정 명령이 작동하지 않는 이유를 모르겠습니다. 여기 내 코드입니다 :빌드 한 셸에 대한 사용 권한이 거부되었습니다.

#include <stdio.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/types.h> 
#include <assert.h> 

void execute(char **argv) 
{ 
    int status; 
    int pid = fork(); 
    if (pid <0) //fork error has happened 
    { 
     perror("Can't fork a child process\n"); 
     exit(EXIT_FAILURE); 
    } 
    if (pid==0) //It's the child process and can run his task 
    { 
    execvp(argv[0],argv); 
     perror("error"); 
    } 
    else //pid>0 it's the parent and should wait for the child 
    { 
     int status; 
     // int wc = wait(&status); 
     // assert(wc != -1); 

     //while(wait(&status)!=pid) 
     // ; 
     wait(NULL); //the way taught in the class 
    } 


} 

int main (int argc, char **argv) 

{ 
    char input[256]; 
    char *args[256]; 
    char **next = args; 
    char *temp; 
    while (1) 
    { 
     printf("mysh>"); 
     fgets(input,256,stdin); 
     input[strlen(input) - 1] = 0; 
     if (strcmp(argv[0], "exit\n")==0) 
      exit(EXIT_SUCCESS); 
     else 
     { 
       temp = strtok(input, " "); 
       while(temp != NULL) 
        { 
        printf("%s\n", temp); 
        *next++ = temp; 
        temp = strtok(NULL, " "); 
        }  
       *next = NULL; 

       execute(args); 
      //execvp(args[0],args); //When I put this command here it just runs once though it is in a while loop so we have to use fork! 

     } 

    } 

    return 0; 


} 

여기에 그 실행의 데모입니다 :

./basic_shell 
mysh>ls 
ls 
basic_shell basic_shell.c basic_shell.c~ basic_shell_OK.c fork fork.c 
mysh>ls 
ls 
ls: cannot access ls: No such file or directory 
mysh>ls 
ls 
ls: cannot access ls: No such file or directory 
ls: cannot access ls: No such file or directory 
mysh>ls 
ls 
ls: cannot access ls: No such file or directory 
ls: cannot access ls: No such file or directory 
ls: cannot access ls: No such file or directory 

을 내가 cp 명령을 실행할 때 다음과 같이 작동합니다

./basic_shell 
mysh>cp fork.c fork_cp.c 
cp 
fork.c 
fork_cp.c 
mysh>cp fork_cp.c copy_fork.c 
cp 
fork_cp.c 
copy_fork.c 
cp: target `copy_fork.c' is not a directory 

것은 당신이 나를 인도시겠습니까 왜 내 껍데기가 어색하고 예기치 않게 행동하니?

답변

1

당신은 하나의 충분 fork

int pid = fork(); 
if ((pid = fork()) <0) 

를 두 번 호출이있다. 첫번째.

오류의 경우 문자열에는 먼저 제거해야하는 \n 터미네이터가 포함되어 있습니다. 당신이 라인에서 255 개 이상의 문자를 입력 할 수 없을거야 가정, fgets(input,256,stdin);을 수행 한 후에는

input[strlen(input) - 1] = 0; 

\n을 제거해야한다.

+0

그 포크() 건 잡기에 감사드립니다. 무효화하는 것에 대해, 당신이 말한 것은 작동하지 않았고 다음과 같은 오류가 있습니다 : fgets (input, 256, stdin); stdin [strlen (stdin) - 1] = 0; 'basic_shell.c : 함수 'main'에서 : basic_shell.c : 48 : 경고 : 호환되지 않는 포인터 유형에서 'strlen'인수 1을 전달하십시오. /usr/include/string.h:399 : 'const char * '하지만 인수가'struct _IO_FILE * '유형입니다. basic_shell.c : 485 : 오류 :'int type '에서'struct _IO_FILE '유형에 할당 할 때 호환되지 않는 유형 –

+0

불량합니다. 'stdin'이 아니다. 답을 다시 써. –

+0

예, 두 번 변경해야합니다. 나는 잠에 가야한다 :) 감사합니다 –