简体   繁体   English

不了解自定义C Shell的逻辑

[英]Not understanding the logic of a custom C shell

So I am trying to debug a shell programmed in C 所以我试图调试用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

Now, I can make this shell execute one word commands like 'ls', or 'pwd', or go into vi and open up a new file. 现在,我可以使此Shell执行一个单词命令,例如“ ls”或“ pwd”,或者进入vi并打开一个新文件。 But multi-word arguments don't seem to be panning out. 但是多词论证似乎并没有解决。

I'm having trouble with the basic logic of the code. 我在代码的基本逻辑上遇到了麻烦。 I mean, looking at the block of code at the bottom, it seems like it has been coded to take in both arguments, but right now, only the first one is getting parsed. 我的意思是,看一下底部的代码块,似乎已经对这两个参数进行了编码,但是现在,只有第一个参数被解析。 What logic error am I making exactly? 我到底在犯什么逻辑错误? I'm interested in learning about this. 我对此感兴趣。

Maybe that's the only problem: after the loop that parses the arguments you have 也许这是唯一的问题:在解析参数的循环之后

input_args[1] = NULL;

so whatever you have done before, now you don't have any more arguments ( input_args[0] is the progam name). 因此,无论您以前做过什么,现在都不再需要其他参数( input_args[0]是程序名称)。 It surely should be 当然应该是

input_args[j] = NULL;

Edit 编辑

that could make your shell work but you would still have a memory leak as you first allocate memory for all input_args[i] (0 <= i < MAX_NUM_ARGS). 这可能会使您的Shell正常工作,但在您首先为所有input_args[i]分配内存(0 <= i <MAX_NUM_ARGS)时,仍然会发生内存泄漏。 So when you set input_args[j] = NULL that memory will never be freed again. 因此,当您设置input_args[j] = NULL ,该内存将永远不会再被释放。 A not very elegant, but working solution would be to call 不太优雅,但可行的解决方案是致电

free( input_args[j] );

before, but I would suggest only to allocate memory for the arguments that are actually needed. 之前,但我建议仅为实际需要的参数分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM