Thread: Simple shell simulate the linux 'history' command

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    3

    Question Simple shell simulate the linux 'history' command

    I have a simple C shell that recognizes change directory and exit commands. Any other commands are printed as is to the command line. I need to simulate the linux 'history' command using an array of stucts. Can anyone help me get started on this? I'm having trouble with how to do this. I believe I need to malloc size for this but not sure how to and how much to..

    Code:
    struct cmd{
        char * cmd;
    }

    This is my simple shell:

    Code:
    #define MAX_LENGTH 4096
    #define DELIMS " \t\r\n"
    
    
    int main(int argc, char *argv[]) {
      char buffer[MAX_LENGTH];
    
    
      while (1) {
        fprintf(stderr, "$");
        
        //Exit if not reading from stdin
        if (!fgets(buffer, MAX_LENGTH, stdin)) {
          break;
        }
    
    
        char* token = strtok(buffer, DELIMS); //store each token
        if(token != NULL) {
          //exit
          if(strcmp(token, "exit") == 0) {
            ExitSmash();
          }
          //cd
          else if (strcmp(token, "cd") == 0) {
            char* args = strtok(0, DELIMS);
            ChangeDirectory(args);
          }
          //tokenize & print to command line
          else {
            int i = 0;
            while(token != NULL) {
              printf ("[%d] %s\n",i++, token);
              token = strtok (NULL, DELIMS);
            }  
          }
        }
      }
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So start with
    char history [10][BUFSIZ];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    why is history a char array? Shouldn't it be an array of stucts?
    Sorry, beginner here, this is all new to me. Just learnt about pointers and structs..

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ivar5000 View Post
    why is history a char array? Shouldn't it be an array of stucts?
    Sorry, beginner here, this is all new to me. Just learnt about pointers and structs..
    Maybe it could be a struct in the future if the program gets more complicated, like when you start using dynamic memory.

    For now though, the commands are strings, so you can simply store commands from the past in an array of strings.

    Start simple, get it working, and then gradually make it more complex.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    Sorry I wasn't clear earlier. Among other things, this happens to be an excercise on structs & pointers, so I am required to use an array of cmd structs which stores 25 of the last entered commands into history and to have a history.c and history.h with the following functions:

    • Init_history // Makes sure all buffers (struct array) are initialized and ready to use.
      • This is equivalent to a constructor in Java.

    • add_history // adds command into a cmd struct and append to your history buffer
      • This is equivalent to a method in Java.

    • clear_history // properly fees all memory and clears the history buffer
      • This is equivalent to a method in Java. Unlike Java, however, you are responsible for freeing any memory you malloc’ed (there is no garbage collector).

    • print_history // Prints the history to the command line (See the example above)
      • This is similar to a toString in Java, except it is not returning a String, it is just printing.


    So here's what I tried,

    Code:
    ...
    typedef struct Command_struct{
      char* commandString;
    } Command;
    ...
    
    Command  cmd[25];
    int count = 0;
    
    while (1) {
        fprintf(stderr, "$");
    
        //Exit if not reading from stdin
        if (!fgets(buffer, MAX_LENGTH, stdin)) {
          break;
        }
    
    cmd[count].commandString = buffer;
    
        char* token = strtok(buffer, DELIMS); //store each token
    ...
    count++;
    ...
    Am I doing this right, or do i need to call init_history first? How do I initialize this?
    Last edited by ivar5000; 10-20-2017 at 07:38 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need a char array (and strcpy) to make an actual copy of the command line.

    Otherwise you just end up with 25 pointers to buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. History Command Log File
    By Matus in forum Tech Board
    Replies: 1
    Last Post: 04-29-2009, 07:55 PM
  2. How do I simulate a program with a \frac{}{} command
    By xbusterx in forum C++ Programming
    Replies: 21
    Last Post: 09-30-2008, 08:51 PM
  3. Executing 'alias' Linux shell command from C
    By lan_zer0 in forum Linux Programming
    Replies: 12
    Last Post: 10-03-2007, 12:53 PM
  4. simple question... how od you simulate...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 12-05-2004, 01:01 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread