gpt4 book ai didi

c - 段错误 - strcpy() - C

转载 作者:行者123 更新时间:2023-11-30 14:20:40 24 4
gpt4 key购买 nike

我正在为命令行 shell 实现历史记录功能。我已经实现了一个循环数组来保存十个最近的命令。每个命令还用一个整数来标记,指定总命令是什么。例如,如果总共输入 30 个命令,则循环数组中的 10 个命令将编号为 (30, 29, 28, 27,...,21)。

如果用户要插入命令“r”,后跟一个标记十条指令之一的数字,则该指令应该运行。当我试图确保正确接受两个字的命令时,我不断遇到段错误。谁能帮忙指出问题所在。

int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

int position, count, rnum = 0;
char historyArray[10][MAX_LINE];
char *holder[MAX_LINE]={0};

while (1){ /* Program terminates normally inside setup */
background = 0;
printf("COMMAND->");
fflush(0);

setup(inputBuffer, args, &background); /* get next command */

position = (count % MOD_VAL);
strcpy(historyArray[position],args[0]);

if(!strcmp("rr",args[0]))
{
strcpy(historyArray[position],historyArray[((position-1)+MOD_VAL)%MOD_VAL]);
printf("%i",count);
printf("%c",'.');
printf("%c",' ');
printf("%s",historyArray[position]);
printf("%c",'\n');
strcpy(args[0],historyArray[position]);
}

else if(!strcmp("r",args[0])) //SEG FAULT OCCURING IN THIS ELSE-IF BLOCK!
{
//args[1] will hold given number
printf("%c",'\n');
printf("%s",args[0]);
printf("%s",args[1]);
printf("%s",args[2]);
printf("%c",'\n'); //PRINT STATEMENTS FOR DEBUGGING

strncpy(holder[0], args[2], MAX_LINE - 1); //SEG FAULT

rnum = atoi(args[1]);
strcpy(historyArray[position],historyArray[((position-(count-rnum))+MOD_VAL)%MOD_VAL]);
strcpy(args[0],historyArray[position]); //CHANGES VALUES OF args[1], args[2]

if(holder[0] != NULL)
{
strncpy(args[1],holder[0],MAX_LINE-1);
args[2] = NULL;
}
else
{
args[1] = NULL;
}

printf("%c",'\n');
printf("%s",args[0]);
printf("%s",args[1]);
printf("%s",args[2]);
printf("%c",'\n');
}

else if(!(strcmp("h",args[0]))||!(strcmp("history",args[0])))
{
int counter = 0;
while(counter < 10)
{
printf("%i",(count - counter));
printf("%c",'.');
printf("%c",' ');
printf("%s", historyArray[((position - counter + MOD_VAL)%MOD_VAL)]);
printf("%c",' ');
printf("%c",'\n');
counter ++;

if(counter > count)
break;
}
}
count++;

pid_t pid1; //Initialize pid_t variable to hold process identifier
pid1 = fork(); //Fork process and assign process identifier to "pid1"

if (pid1 == 0) //Child process
{
//Child process executes the command specified by the user and
//then quits.
execvp(args[0], args);
exit(0);
}
else //Parent process
{
if (background != 1)//Check for inclusion of '&' in command
{
wait(NULL); //Wait for child process to finish executing
}
}

/* the steps are:
(1) fork a child process using fork()
(2) the child process will invoke execvp()
(3) if background == 0, the parent will wait,
otherwise returns to the setup() function. */
}
}

感谢任何帮助!

-哑光

最佳答案

这里的 args 是字符指针数组。

但是strcpy需要两个参数 - 这应该是数组由malloc分配的内存的字符指针

但是您的 strcpy(historyArray[position],args[0]); 将一个参数作为字符指针,这将不会被接受。

因此您可以将 args[] 更改为 args[][]args[0] = malloc(some_no)segfault 将被删除。

关于c - 段错误 - strcpy() - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286014/

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