0

所以我正在尝试调试用 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 并打开一个新文件。但多词争论似乎并没有出现。

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

4

1 回答 1

1

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

input_args[1] = NULL;

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

input_args[j] = NULL;

编辑

input_args[i]这可以让你的 shell 工作,但是当你第一次为所有(0 <= i < MAX_NUM_ARGS)分配内存时,你仍然会有内存泄漏。因此,当您设置input_args[j] = NULL该内存时,将永远不会再被释放。一个不是很优雅但可行的解决方案是调用

free( input_args[j] );

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

于 2013-09-26T16:57:24.630 回答