不了解自定义C shell的逻辑

时间:2013-09-26 16:37:55

标签: c arrays string shell arguments

所以我试图调试用C编程的shell

#include <stdio.h>
#include <string.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 j = 0;// second counting variable (thank you Nathan)
    int next_counter = 0;

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

    // sees to it that the buffer is clean (thanks erik =) )
    memset(buffer, '\0', sizeof(buffer));
    i = 0;
    j = 0; //ensure that the counting variables are reset


    //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. =( Can't run. Sorry!\n");

        return -1; //Thank you Erik for this idea!
    }
    //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.");
            return -1;
            }//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 loop
    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




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

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

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

        }//end of else statment

    }//end of for loop

    input_args[1] = NULL;

    //block down here handles the command arugments
    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);

    }//end of if statement
    else{
    //  printf("I am the first child.\n");
        retval = execvp(input_args[0], input_args);
        //exit(2);
        if (retval != -1){
            //print error!
            printf("Invalid command!\n");
            exit(2);
        }
    }//end of else block

   } //end of while loop
    return 0;

}//end of main function

现在,我可以让这个shell执行一个单词命令,如'ls'或'pwd',或者进入vi并打开一个新文件。但多字的论点似乎并没有过时。

我遇到了代码的基本逻辑问题。我的意思是,看看底部的代码块,似乎它已被编码为接受两个参数,但是现在,只有第一个被解析。我到底做了什么逻辑错误?我有兴趣了解这一点。

1 个答案:

答案 0 :(得分:1)

也许这是唯一的问题:在解析你拥有的参数的循环之后

input_args[1] = NULL;

所以无论你以前做过什么,现在你没有任何更多的参数(input_args[0]是程序名称)。肯定应该是

input_args[j] = NULL;

修改

可以使你的shell工作,但是当你第一次为所有input_args[i](0 <= i&lt; MAX_NUM_ARGS)分配内存时,你仍然会有内存泄漏。因此,当您设置input_args[j] = NULL时,永远不会再释放内存。一个不太优雅,但工作的解决方案是打电话

free( input_args[j] );

之前,但我建议只为实际需要的参数分配内存。