Thread: Creating a simple shell using C

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Creating a simple shell using C

    I am currently in a beginning C/UNIX class and I am having major difficulties with the first assignment we have been given using C. We were given the source code for a simple shell and are supposed to implement a history feature into the code we've been given.

    It is supposed to keep a history file that records previous commands preceded by a number (like bash keeps). It is also supposed to be capable of supporting two additional features: when !(number) is typed at command line, it prints the last command that corresponds to that number, and when !(string) is typed at command line it prints the last command that begins with that string.

    I am extremely frustrated working on this assignment. The professor has provided very little information on how to complete this, and when I have asked for more details on how it should be done, he will only say it is involving string comparisons. I've tried to research the topic but I can't seem to find anything that pertains to this type of feature in the way that I am supposed to implement it. Does not help to be trying to learn how opening/reading/writing files works in C at the same time as string comparison, etc.

    Is there a resource that would be helpful for this? I am very, very frustrated. I don't want anyone to do my work for me but without understanding what is needed to accomplish this, I feel really at a loss.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is the history limited to say, the last 20 commands entered? If so, then I suggest creating an array of 20 commands, and treat it as a circular buffer. Since you are already keeping track of the end of the valid portion of the circular buffer, you can compute the index of the command corresponding to number so as to handle !(number). !(string) can be handled with the help of strncmp from <string.h>, starting from the end of the valid portion of the circular buffer to the start of the valid portion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    There was no limit on the history specified. It appears that what I am supposed to do is replicate the history features of csh.

    I found another piece of code from a student who I believe may be doing something similar:

    Code:
    while(fgets(input, sizeof(input), stdin) != NULL){
     
        filePrint = fopen(".simpleshell_history", "a");
        fileRead = fopen(".simpleshell_history", "r");      
     
        count++;
        fprintf(filePrint, "%d - %s", count, input);
        fclose(filePrint);
     
        if (strcmp(input,"history")==0){
            printf("%sn", input);
            fseek(fileRead, 0, SEEK_SET);
            int x = 0;
            while ((x = fgetc(fileRead)) != EOF){
                printf("%c", x); 
            }
        }
    }
    However, I tried running it just to see if it was even a reasonable start for the task, and the history started over at 1 every time the shell program was launched. So, it did not work. I am hoping to implement this in the simplest way possible but nothing I have tried thus far has worked.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jniewolak
    There was no limit on the history specified.
    Then a reasonable option is to use a dynamic array implemented using a pointer and malloc/realloc/free. Computing the number for !(number) would now be exceedingly easy, and !(string) just requires a linear search using strncmp, starting from the end.

    Quote Originally Posted by jniewolak
    However, I tried running it just to see if it was even a reasonable start for the task, and the history started over at 1 every time the shell program was launched. So, it did not work. I am hoping to implement this in the simplest way possible but nothing I have tried thus far has worked.
    I suggest that you ignore reading/writing to file for now. Assume that the shell program is not closed, and that there is enough memory to store the entire history.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Shell
    By Paul1485 in forum C Programming
    Replies: 7
    Last Post: 10-26-2012, 02:25 PM
  2. Creating a Unix Shell in C
    By Opel_Corsa in forum Linux Programming
    Replies: 2
    Last Post: 10-12-2006, 09:57 AM
  3. Creating Shell based game..Few questions
    By Saintdog in forum Game Programming
    Replies: 1
    Last Post: 12-01-2004, 08:14 PM
  4. Creating shell in C
    By LinMach in forum C Programming
    Replies: 8
    Last Post: 02-19-2003, 07:40 PM
  5. Help creating Mini-shell??
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 02-04-2002, 11:44 PM

Tags for this Thread