2013-09-26 54 views
2

所以我有這樣的代碼了殼:定製外殼只帶一個參數

#include <stdio.h> 
#include <stdlib.h> 
#define MAX_NUM_ARGS 256 
#define SIZE 256 

void orders(char *command[SIZE]); 

int main() { 

    char buffer[SIZE]= ""; 
    //char input_args[MAX_NUM_ARGS]; 
    char **input_args = NULL; 
    int i = 0;// counting variable 
    int blah = 0; 

    printf("Welcome to the AY shell.\n"); 
    while(1){ 

    //initialize array of strings 
    //first free any prevously allocated memory 
    if (input_args != NULL) 
    { //memory has been allocated free it 
     for (i = 0; i <MAX_NUM_ARGS; i++) 
     { 
      free(input_args[i]); 
     } 
    } 
    //free array of strings 
    free(input_args); 

    //new allocate memory 
    input_args = (char**) malloc(sizeof(char*) * MAX_NUM_ARGS); 
    //check return value for error 
    if (input_args == NULL) 
    { 
     printf("We are out of memory. =(\n"); 
     continue; 
     //print error: out of memory, exit with a error code 
     exit(0); 
    } 
    //allocate memory for each string 
    for (i = 0; i <MAX_NUM_ARGS; i++) 
    { 
     input_args[i]= (char*)malloc(sizeof(char) * MAX_NUM_ARGS); 
     if(input_args[i] == NULL) 
      {//error 
      printf("Error, the input is empty."); 
      continue; 
      }//end of if statement 
    }//end of for loop 


    printf("~$: "); //prompts the user for input 
    fgets(buffer, sizeof(buffer), stdin); 
    //if the user types in exit, quit 
    if (strcmp(buffer, "exit\n") == 0){ 
     exit(0); 
    } //end of if statement 
    //if user types in clear, wipe the screen and repeat the lop 
    else if(strcmp(buffer, "clear\n")==0){ 

     system("clear");  
     continue; 

    }//end of else if 
    //should the user punch in nothing, repeat the loop 
    else if (strcmp(buffer, "\n") == 0) { 
     continue; 
    }//end of else if 


    input_args[1] = NULL; 

    for (i = 0; i < SIZE; i++) { 

     if(buffer[i] != '\n' && buffer[i] != ' ' && buffer[i] != '\t'){ 

      input_args[0][i] = buffer[i]; 
     } //end of if statement 
     else{ 
      input_args[0][i] = '\0'; 

     }//end of else statment 

    }//end of for loop 

    //if the input doesn't fall under the conditionals above, execute orders. 
    orders(input_args); 


    } //end of while loop 
    return 0; 

}//end of main function 

void orders(char *command[SIZE]){ 
//handles the commands of the shell 

    int retval = 0; //return value 
    int pid = 0; 
    int childValue = 0; 
    pid = fork(); 

    if (pid != 0){ 
    // printf("I'm the parent, waiting on the child.\n");//debug 
     pid = waitpid(-1, &childValue,0); 
    // printf("Child %d returned a value of %x in hex.\n", pid, childValue); 
     return;//return backs to the main prompt 
    }//end of if statement 
    else{ 
    // printf("I am the first child.\n"); 
     retval = execvp(command[0], command); 
     exit(2); 
     if (retval != -1){ 
      //print error! 
      printf("Invalid command!\n"); 
      exit(2); 
     } 
    }//end of else block 


}//end of orders function 

現在,執行清晰,退出,單個字的命令剛剛好,如ls或pwd。但是,諸如「vim」之類的多行命令不起作用,也不能更改目錄。

我在做什麼錯?

我在懷疑retval = execvp(command [0],command);造成問題,但我不太確定。有什麼想法嗎?我不想直接回答,因爲這是作業,只是朝正確的方向推進。

回答

2

本節:

input_args[1] = NULL; 

for (i = 0; i < SIZE; i++) { 

    if(buffer[i] != '\n' && buffer[i] != ' ' && buffer[i] != '\t'){ 

     input_args[0][i] = buffer[i]; 
    } //end of if statement 
    else{ 
     input_args[0][i] = '\0'; 

    }//end of else statment 

}//end of for loop 

限制input_args僅具有要使用的第一索引。我認爲這是你會找到一種方法,有一個j++;else子句中,並使用input_args[j][i]或類似的東西...

和你最後的評論也只能使用從第一項匹配這一點,因爲你retval = execvp(command[0], command);名單。