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的语言中删除。 –