Thread: Creating shell in C

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    14

    Angry Creating shell in C

    I'm trying to create a shell in c. One of the problems Ive ran into is, how to implement input and output redirection and pipes. How can I modify my code here in order to make it support those things?

    I can execute single command without a problem but when I try to redirect the output it doesnt work.

    Any advice is much appreciated.

    Code:
    #include <stdio.h>
    
    main()
    {
    char *path, *argv[20], buf[80], n, *p;
    int m, status, inword, continu;
    
    while(1) {
    
    inword = 0;
    p = buf;
    m = 0;
    continu=0;
    
    printf( "\nshhh> ");
    while ( ( n = getchar() ) != '\n'  || continu ) {
    	if ( n ==  ' ' ) { 
    	    if ( inword ) {
    		inword = 0;
    		*p++ = 0;
    	    }
    	}
    	else if ( n == '\n' ) continu = 0;
    	ele if ( n == '\\' && !inword ) continu = 1;
    	else {
    	    if ( !inword ) {
    		 inword = 1;
    		 argv[m++] = p;
    		 *p++ = n;
    	     }
    	     else 
    		 *p++ = n;
    	}
    }	
    *p++ = 0;
    argv[m] = 0;
    
    if ( strcmp(argv[0],"quit") == 0 ) exit (0);
    
    if ( fork() == 0 )
    	{
    	execvp( argv[0], argv );
    	printf ( " didn't exec \n ");
    	}
    
    wait(&status);
    }
    }
    Last edited by LinMach; 02-18-2003 at 10:25 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code. Thanks
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    int main( void )
    {
        printf("
    ############
    ### \  / ###
    ## \ \/ / ##
    ### \  / ###
    #### \/ ####
    ############
    A Shell, in C.
    ");
        return 0;
    }
    It's not a very good shell, but it's the best I could do in a pinch, on the phone, with limited ascii graphics.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    I apologize for my ignorance, I should have taken the time to read the faq.

  5. #5
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    where were you 8 months ago when I was writing a shell vVv?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    I'm also trying to make a little history function for my shell.

    Im new to C so please bear with my simple questions.

    In order to print the lines im entering into my shell would i use a line like this?

    Code:
    while( argv[i] !=0)
    {
      open("history.dat");  
      fprintf("history.dat", "%s",argv[i]);
      close("history.dat");
    }
    I know this doesnt work because ive tried it. Am I on the right path with the commands?

    Thanks

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>In order to print the lines im entering into my shell would i use a line like this?
    When you read in the commands, I presume you read them into a char array? If so, I doubt its called argv, as this is the convention used to command line args passed in when the program starts.

    You might do something like this:

    - Read input from user (fgets)
    - Write output to history file (fopen/fprintf/fclose)
    - Validate/Action command

    Also, you would probably want to consider storing the history in memory as well, so that you can go back without needing to open/read the file. You could do this with an array of char arrays.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    Im trying to read from the argv I already have that stores the commands the users enter into the shell. I was thinking I could just use that array and spit it out to a file. I didnt know it could be saved in memory. How would I go about doing that?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, first things first. I suggest you get the shell working properly, then worry about a history element.

    But for when you get there... The history would be maintained by the parent process, which is acting as the shell. The child processes, as created by fork() would not log any history as they did not receive input directly from the user.

    Now, lets say for example you have buf[80] that holds one command. To keep a history of commands you need something like this:

    char history[MAX_NUM_COMMANDS][MAX_COMMAND_LENGTH];
    int CurrentHistoryPoint = 0;

    When you have read a command into buf, simply copy it to history[CurrentHistoryPoint], then increment CurrentHistoryPoint ready for the next command. This gives you an array of commands that you can then manipulate as you see fit.

    There are other ways too, this is only to get you started.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating Shell based game..Few questions
    By Saintdog in forum Game Programming
    Replies: 1
    Last Post: 12-01-2004, 08:14 PM
  3. Creating processes
    By xErath in forum Linux Programming
    Replies: 5
    Last Post: 10-09-2004, 01:36 PM
  4. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM