gpt4 book ai didi

c - 不理解自定义 C shell 的逻辑

转载 作者:行者123 更新时间:2023-11-30 15:45:27 25 4
gpt4 key购买 nike

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

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

最佳答案

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

input_args[1] = NULL;

所以无论您之前做了什么,现在您都不再有任何参数(input_args[0] 是程序名称)。应该是这样的

input_args[j] = NULL;

编辑

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

free( input_args[j] );

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

关于c - 不理解自定义 C shell 的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19033849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com