C语言 在简单的 UNIX 外壳中实现历史记录的问题



我正在尝试为一个简单的外壳实现历史功能。历史记录应保存最近执行的 10 个命令。我将代码放在下面,但是遇到了一些问题。

首先,当我输入一个(或两个)命令并键入history以显示历史记录时,不会显示任何内容。但是,当我再输入几个命令时,将显示整个历史记录(就像它应该的那样),但每个历史记录索引旁边会显示一串零。

做错了什么,我该如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
char *history[10][MAX_LINE];
int placePointer;

/**
 * setup() reads in the next command line, separating it into distinct tokens
 * using whitespace as delimiters. setup() sets the args parameter as a 
 * null-terminated string.
 */
void setup(char inputBuffer[], char *args[],int *background)
{
    int length, /* # of characters in the command line */
        i,      /* loop index for accessing inputBuffer array */
        start,  /* index where beginning of next command parameter is */
        ct;     /* index of where to place the next parameter into args[] */
    ct = 0;
    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  
    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0){
        perror("error reading the command");
        exit(-1);           /* terminate with error code of -1 */
    }
    /* examine every character in the inputBuffer */
    for (i = 0; i < length; i++) { 
        switch (inputBuffer[i]){
        case ' ':
        case 't' :               /* argument separators */
            if(start != -1){
                args[ct] = &inputBuffer[start];    /* set up pointer */
                ct++;
            }
            inputBuffer[i] = ''; /* add a null char; make a C string */
            start = -1;
            break;
        case 'n':                 /* should be the final char examined */
            if (start != -1){
                args[ct] = &inputBuffer[start];     
                ct++;
            }
            inputBuffer[i] = '';
            args[ct] = NULL; /* no more arguments to this command */
            break;
        case '&':
            *background = 1;
            inputBuffer[i] = '';
            break;
        default :             /* some other character */
            if (start == -1)
                start = i;
        } 
    }    
    args[ct] = NULL; /* just in case the input line was > 80 */
} 
void displayHistory(){
    printf("Display History:n");
    int i = placePointer;
    int j;
    int counter;
    while(counter < 10) {
        printf("%d: ",counter);
        for (j = 0; j < MAX_LINE; j++) {
          printf("%d",history[i][j]);
        }
        printf("n");
        i = (i + 1) % 10;
        counter++;
    }
}
/*
void runHistoryAt(int index){
    printf("Run History At:n");
    char *arg1 = &history[placePointer + index][0];
    char *argLine[MAX_LINE/2+1];
    int j;
    for (j = 0; j < MAX_LINE/2+1; j++) {
      *argLine[j] = history[placePointer + index][j];
    }
    execvp(arg1,argLine);
}*/
int main(void)
{
    char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

    while (1){            /* Program terminates normally inside setup */
        background = 0;
        printf("COMMAND->");
            fflush(0);
            setup(inputBuffer, args, &background);       /* get next command */
        /* the steps are:
         (1) fork a child process using fork()
         (2) the child process will invoke execvp()
         (3) if background == 0, the parent will wait, 
            otherwise returns to the setup() function. */
        pid_t pid = fork();
        printf("Fork created.n");

        if(pid < 0){
            printf("Fork failed.n");
        }else if(pid == 0){
            if( strcmp(args[0],"history") == 0){ /*  Print History */
                displayHistory();
            }else if(strcmp(args[0],"r") == 0){ /*  r num */
                int index = (int) args[1];
                /*runHistoryAt( index - 1);*/
            }else if(strcmp(args[0],"rr") == 0){ /*  Run recent */
                /*runHistoryAt(0);*/
            }else{  /*  Execute normally */
                printf("executing..., adding to history buffern");
                /* Add args to history buffer */
                int j;
                for (j = 0; j < sizeof(args); j++) {
                    history[placePointer][j] = args[j];
                }
                placePointer = (placePointer + 1) % 10;
                /* Execute!  */
                execvp(args[0],args);
            }
        }
        if(background == 0){
            wait(NULL);
        }else{
          setup(inputBuffer, args, &background);
        }
    }
}

所有args指针都是指向inputBuffer的指针,其中包含最近输入的输入行。 因此,当您将args保存到history时,您只是保存指针,而不是实际指向字符串,字符串仍然(仅)在inputBuffer中。 当您阅读下一个命令时,它会覆盖inputBuffer,使所有保存的history指针无效 - 它们现在指向当前命令的某个部分,而不是旧命令。

最新更新