首页 > 解决方案 > cronology 功能可访问最近插入到 C 语言中的 shell Linux 中的命令

问题描述

我正在尝试在我的简单 shell 中创建一个功能,它可以跟踪用户输入的最后 10 个命令。问题是当我在圆形数组中输入命令时,它会被最后输入的命令填充。我不明白为什么,提前谢谢。

在 p[0] 我有命令

主功能提示


  void prompt();
  void history(char *p[]);
  void showBuffer();

  char* cbuffer[10] = {0};
  int idx = 0;


int main (int argc, char* argv[])
  {


      //Set the PWD to the Home Directory
      chdir(getenv("HOME"));
      printf("Shell is running..\n");
      printf("\n");

      char com[MAX_STRING];
      char *pars[MAX_ARGS],buff[MAX_ARGS];
      int w, pid, s;


  while (TRUE) {

      prompt();
      w = read_command(com, pars);
      //check for the background or foreground implementation
      //w=0 -> background
      //w=1 foreground
      printf("%d \n",w);

     // check if no command was read 
      if (strlen(com) == 0)
    continue;

     // com are the commands.. /bin/cd..
     //pars[0] it' the first token(command)  .. pars[1] the second token

      pid = fork();      
      if (pid < 0) { // fork FAILED!
    printf("ERROR!: impossible to execute the command: %s", com);
    continue;
      }
      if (pid != 0) // FATHER
          if (w)
              waitpid(-1, &s, 0);
          else continue;
      else execv(com, pars); // CHILD

    }

printf("terminated");
    return 0;    
  }

  void prompt()
  {
    char hostname[MAX_STRING];
    char buff[MAX_ARGS];
    gethostname(hostname,MAX_STRING);

    printf("@%s",hostname);
    printf(": %s",getcwd(buff,MAX_STRING));
    printf(" >> ");
  }



填充和显示数组的函数

//function that populates the array 

void history(char *p[])
 {

  cbuffer[idx] = p[0];
  printf("comando nel BUFFER-------------->> %s \n", cbuffer[idx]);
  printf("%d indice, idx \n", idx);

  idx++;
  if(idx == 10)
    {
     showBuffer();
     idx = 0;
    }
}

//show buffer 
void showBuffer()
{
  for(int i = 0; i < 10; i++) printf("%s e`il comando contenuto\n", cbuffer[i]);
}


函数读取命令和调用历史(char *p[])

int read_command(char *c, char *p[])
  {
    char tmp[MAX_STRING],copy_path[MAX_STRING], *s;
    const char *path=getenv("PATH");
    int i;
    struct stat buf;


    // it reads from the  stdin in a SECURE way
    get_string(tmp, MAX_STRING);

     // the user just push the   carriage return
    if (strlen(tmp) == 0) {
      c[0] = '\0';

      return 0;
    }

    // extract the command name (first token)
    s = strtok(tmp, "  ");
    strcpy(c,s);


    //Pass a tmp path for don't change the getenv("PATH");
    strcpy(copy_path,path);


    // PARAMETER ARRANGEMENT    
    i=0; 
    while (s != NULL) {
      if (p[i] == NULL)
          p[i] = (char *) malloc(MAX_STRING); // MEMORY ALLOCATION


      strcpy(p[i++], s);

      s = strtok (NULL, " ");
    }

    p[i] = NULL;

/******* i try to save command in array************/

      if(p[0]!=NULL && strcmp(p[0],"!!!") !=0)
      {
         history(p);
      }


      else if(strcmp(p[0], "!!!")==0)
         {
           for(int i = 0; i <= 10; i++){
             printf("%d --> %s \n", i, cbuffer[i]);
           }
           showBuffer();
         }

    //Foreground
      return 1;
  }



输出 :

comando nel BUFFER--------->> cd // last command insert, the above commands are different from "cd"
9 indice, idx
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command

标签: clinux

解决方案


推荐阅读