Thread: Own Shell: Displaying prompt and write a command

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    15

    Own Shell: Displaying prompt and write a command

    Hi,

    I'm building my own shell, I've search on the forums but I couldn't find this anywhere.

    I have something like this:

    Code:
    void showPrompt(){
       char buff[200];
      getcwd(buff, sizeof(buff));
       printf("%s:> ", buff);
    }
    
    .
    .
    .
    And on the main function:

    Code:
    .
    .
    .
    do{
      showPrompt();
      if(strcmp(argv[0], "Pwd") == 0){
        printf("%s\n", Pwd() );
      }
    
      if(strcmp(argv[0], "Exit") == 0){
        exit(1);
      }
    }while(Exit != 1);
    what happens when I run the program is that every char, word, number and even any command I write, it gives me the current location.

    I assume that my showPrompt function is very badly done and the program is showing me the getcwd/pwd all the time?

    Could anyone help me out making a good showPrompt function?


    Thanks in advance!
    Last edited by xyphen; 12-08-2009 at 04:02 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    In this case
    Code:
    void showPrompt(){
       char buff[200];
       printf("%s:> ", buff);
    }
    buff is not initialized.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Sorry, I forgot to insert a line while copying the code. I fixed it above.

    Any ideas?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why wouldn't it give you the current location? It's part of your prompt that you keep displaying in a loop.


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

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Making some tests with a variable "command" instead of using argv, I made it work with the command "Exit"

    if I now enter let's say "a", it won't give me the current location like before.

    I think I have a good showPrompt after all..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. C++ and command prompt commands
    By homeyg in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2004, 06:20 PM
  3. Write in many command prompts
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-15-2004, 01:32 AM