2017-02-28 5 views
0

커맨드 라인 인터프리터를 한 줄에 둘 이상의 커맨드로 생성하고 싶습니다. 다음과 같은 keybord 명령을 입력합니다 : pwd ; ls ; cat file ; ls -l커맨드 라인 intepreter in c

나는 이것을 작성했는데 누군가 경고 메시지를 표시 할 수 있습니까?

경고는 다음과 같습니다 나쁜 영어에 대한

ex.c: In function ‘Execute’: 

ex.c:33:18: warning: passing argument 1 of ‘execvp’ from incompatible pointer type [-Wincompatible-pointer-types] 
     if(execvp(cmd, pin) == -1) { 

In file included from ex.c:3:0: 

/usr/include/unistd.h:581:12: note: expected ‘const char *’ but argument is of type ‘char **’ 
extern int execvp (const char *__file, char *const __argv[]) 


ex.c: In function ‘main’: 

ex.c:105:17: warning: passing argument 1 of ‘Execute’ from incompatible pointer type [-Wincompatible-pointer-types] 

     Execute(pin[0],pin); 


ex.c:21:6: note: expected ‘char **’ but argument is of type ‘char *’ 
void Execute(char* cmd[],char* pin[]) { 
     ^~~~~~~ 

죄송합니다. Execute 기능에

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

#define MAX_SIZE 512 

//Clean function 
void Clean(char* cmd[],char* pin[]) {   //Clean Array 

    int a; 
    for(a=0; a < 40; a++){ 
      cmd[a] = NULL; 
      pin[a] = NULL; 
    }   
} 

//execute commands 

void Execute(char* cmd[],char* pin[]) { 

    pid_t pid; 
    pid = fork();  

    switch(pid) { 
     case -1: 
      printf("DEBUG:Fork Failure\n"); 
      exit(-1); 
     case 0: 
      execvp(*cmd, pin); 

      if(execvp(cmd, pin) == -1) { 
       printf("Command Not Found\n"); 
       exit(0); 
      } 

     default: 
     wait(NULL); 
     printf("DEBUG:Child Finished\n");  
    } 
} 

int main() { 
    char* cnd; 
    char* cmd[40]; 
    char* pin[40]; 
    char* array; 
    int how_much; 

    char *input = malloc (MAX_SIZE); 

    if (input == NULL) { 
     printf ("No memory\n"); 
     return 1; 
    } 

    Clean(cmd,pin); 

    printf("shell> "); 

    fgets (input, MAX_SIZE, stdin); 

    if ((strlen(input)>0) && (input[strlen (input) - 1] == '\n')) { 
     input[strlen (input) - 1] = '\0'; 
    } 

    printf("INPUT: %s\n", input); 

//searching for ";" 

    cnd = strtok(input, ";"); 

    int i = 0; 

    while(cnd != NULL) {  
     cmd[i] = cnd; 
     i++; 
     cnd = strtok(NULL, ";"); 
    } 

    cmd[i] = NULL; 
    how_much = i; 

// searching for " "  

    for(int j=0; j < how_much; j++) {    
     array = strtok(input, " "); 

     int k=0; 

     while(array != NULL) { 
      pin[k] = array; 
      k++; 
      array = strtok(NULL, " "); 
     } 

     pin[k] = NULL; 

     Execute(pin[0],pin); 
    } 

    free (input); 

    return 1; 
} 
+0

BTW'array = strtok (input, "");'실수 일 수 있습니다. – BLUEPIXY

+0

나는 모든 것을 포함하고 pinakas = pin (실수로 죄송합니다) – Xri

+0

수정했습니다, 이것은 제 실제 코드 – Xri

답변

0

:

... 
case 0: 
    execvp(*cmd, pin); 

    if (execvp(cmd, pin) == -1) { 
    ... 

는 먼저 execvp(*cmd, pin) 다음 execvp(cmd, pin) 있습니다. 이것이 잘못된 것을 제안해야합니까?

그리고 여기 : 첫 번째 인수 Execute에서

Execute(pin[0],pin); 

배열 char*을 기대하지만, 당신은 char*을 제공합니다.

아마도 더 많은 문제가있을 수 있습니다.

+0

나는 당신의 도움을 간구합니다. 나는 이것을 해결하려고 노력할 것이다. – Xri