【问题标题】:Not understanding the logic of a custom C shell不了解自定义 C shell 的逻辑
【发布时间】:2013-10-02 18:03:58
【问题描述】:

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

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

【问题讨论】:

标签: c arrays string shell arguments


【解决方案1】:

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

input_args[1] = NULL;

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

input_args[j] = NULL;

编辑

这可以让你的 shell 工作,但是当你第一次为所有 input_args[i] (0 input_args[j] = NULL 时,该内存将永远不会再被释放。一个不是很优雅但可行的解决方案是调用

free( input_args[j] );

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

【讨论】:

    猜你喜欢
    • 2018-07-19
    • 1970-01-01
    • 2018-03-18
    • 2013-11-15
    • 1970-01-01
    • 2017-01-01
    • 2021-12-16
    • 2011-02-19
    • 2019-05-27
    相关资源
    最近更新 更多