c - shell 中的历史功能

标签 c linux shell unix history

我必须实现 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/

相关文章:

指针(地址)可以为负吗?

c++ - 我可以在 main() 函数之外使用 GetAsyncKeyState() 吗?

cocoa - 如何创建一个创建新站点的 Coda 插件?

linux - acquia服务器上的shell脚本调度

c - 自动删除双向链表中的元素

c - 嵌套 If..else - 错误输出,直接转到 C 编程语言中的最后一个 else

linux - 如何在循环中连接两个带有标识符的文件?

linux - 生成随机二进制文件

php - 使目录权限被拒绝,但权限设置正确

linux - 如何grep字符串的行号,在其他文件中复制同一行