0
我正在嘗試編寫一個模擬的shell程序,並帶有一個打印shell中10個最新條目的歷史命令。問題是,當使用history命令時,不是打印輸入的行,而是爲每個條目打印addToHistory內部數組參數的名稱「history」。陣列的打印元素打印數組的名稱
這是代碼。
void addToHistory(char *line, char **history, int num) {
// insert line into first element of history
// move elements backward to make room
if(num == 0) {
history[0] = line;
}
else if (num > 1 && num < HISTORY_BUFFER) {
printf("%d", num);
printf("\n");
for(int i = num-1;i > 0;i--) {
history[i] = history[i-1];
}
history[0] = line;
}
else if (num > HISTORY_BUFFER) {
printf("%d", num);
printf("\n");
for(int i = HISTORY_BUFFER-1;i > 0;i--) {
history[i] = history[i-1];
}
history[0] = line;
}
}
int main(void)
{
char *args[MAX_LINE/2 + 1]; /* command line arguments */
char *history[HISTORY_BUFFER];
char line[64];
int should_run = 1; /* flag to determine when to exit program */
int num = 0;
while (should_run) {
printf("osh> ");
fflush(stdout);
gets(line); /* read in the command line */
printf("\n");
parse(line, args); // function for splitting input into seperate strings; works fine
if (strcmp(args[0], "exit") == 0) { /* is it an "exit"? */
should_run = 0; /* exit if it is */
}
else if (strcmp(args[0], "history") == 0) {
if (num == 0) {
printf("No commands in history. Please enter a command and try again\n");
}
else if (num < 10) {
for(int i = 0;i < num;i++) {
printf("%d ", i);
printf(history[i]);
printf("\n");
}
}
else {
for(int i = 0;i < 10;i++) {
printf("%d ", i);
printf(history[i]);
printf("\n");
}
}
}
/* snip */
else {
addToHistory(line, history, num);
num++;
// executeProcess(args);
}
}
}
經過10項產生的輸出是一樣的東西
osh> history
0 history
1 history
2 history
3 history
4 history
5 history
6 history
7 history
8 history
9 history
其中,「歷史」應改爲無論是輸入到當時的殼。一次輸入後,輸出簡單地是0 history', so the behavior is present in all iterations of
addToProcess`。
在任何情況下都不應該使用'gets()'。它在C99中被棄用,並且完全從C11的語言中刪除。 –