我必须实现 hist 命令,包括 !k 和 !!
2个函数:
void addInHistory(char **history,char *command,int *list_size,int history_capacity)
{
int index=*(list_size);
if(command[0]!='\n')
{
if(index==history_capacity-1)
{
printf("History is full.Deleting commands.");
}
else
{
char current_command[COMMAND_SIZE];
strcpy(current_command,command);
history[index++]=current_command;
}
}
}
void printHistory(char **history,int size)
{
int i;
for(int i=0;i<=size;i++)
{
printf("%d. %s\n",i+1,history[i]);
}
}
如有任何帮助,我们将不胜感激。
最佳答案
对于 C 解决方案
char current_command[COMMAND_SIZE];
strcpy(current_command,command);
history[index++]=current_command;
应该是
history[index++]= strdup(command);
使用完毕后请务必将其释放。
关于c - shell 中的历史功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19878371/