所以我寫了一些代碼來實現shell(Linux,GCC,C語言),它都可以工作,但是由於某些原因,代碼崩潰時我添加了歷史選項: 它確實沒有(另一個代碼),所以我只會把這裏放在你需要的地方。shell的執行歷史得到分段錯誤
問題是當我在鍵入quit
後需要在歷史記錄中保存一箇舊命令,並且當我鍵入退出時,它只是用分段錯誤(核心轉儲)進行壓縮。
歷史記錄保存在鏈接列表,命令字符串和下一個節點的節點的結構中,我也將頭部保存在主體中。關鍵是我只想保存最後15條命令,並且我不關心其他命令,所以每次我想打印列表時,我都會在循環的前15個節點上移動。
當我用GDB調試,我看到的是,代碼崩潰行是行後,他加入了第一個命令的歷史,但當前行真的不相關的歷史:
主:
int main()
{
history_ll* top;
char userInput [CHOICE_LENGTH];
char buff[PATH_MAX];
int flag=1;
cmdLine * head;
while (flag)
{
getcwd(buff, PATH_MAX);
printf("%s:~$ ",buff);
fgets(userInput,MAX_INPUT, stdin);
userInput[strlen(userInput)-1]=0;
historyAdder(userInput,&top);
if(strcmp(userInput,QUIT_OPTION)==0) //segmentation fault here!
{
flag=0;
}
else
{
//doesn't matter
}
}
return 0;
}
的historyAdder看起來是這樣的:
void historyAdder(const char *command,history_ll** top)
{
history_ll* node;
strcpy(node->command,command);
node->command[strlen(command)]=0;
if(historyLength!=0)
{
node->next= *top;
}
else
{
node->next= NULL;
}
*top = node;
historyLength++;
}
注:historyLength是一個全局變量
這是結構:
typedef struct history_ll{
char command[CHOICE_LENGTH];
struct history_ll *next;
}history_ll;
謝謝幫手!