Results 1 to 5 of 5

Thread: Displaying command prompt

  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    66

    Displaying command prompt

    I know this has to be a very dumb question, but I can't yet pull it off. I am writing a simple c++ program that is supposed to display the command prompt that has these three elements
    1)the command number starting at 1
    2)name of the machine
    3)current working directory

    The problem is...I don't know how to do that in a C++ program..


    Ok I have that much figured out, now the problem is I want to pass a string value to
    system(). When i try to do
    system(s); when s is declared as string s="";
    This gives me an error about converting string to char*....Any Ideas?

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    You should be able to do system(s.cstr()); or something like that. As for the other things you want. I think you will have to use getenv() to get that info from your environment, except for the command count. That can just be a counter. Good luck.

  3. #3
    Member
    Join Date
    Apr 2006
    Posts
    66
    Do I have to #include anything? to use the cstr(), because i get the error
    'struct std::string' has no member name 'cstr()'


    HA its c_str();

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    1)the command number starting at 1
    2)name of the machine
    3)current working directory
    There are plenty of ways for point 2 (uname -n, /etc/hostname, ...).
    Sometimes, there is an environmental variable $HOSTNAME, but might
    not be available in the context of the application. Below,
    I simply use the gethostname-function in sys/socket.h.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    
    int main(){
            char *USER;
            char *PWD;
            char *HOSTNAME;
            char clinput[1024];
            char chostname[256];
    
            int command_nr;
    
    	gethostname(&chostname,sizeof(chostname));
            HOSTNAME=chostname;
    
            command_nr=1;
            while (1){
    
                    USER=getenv("USER");
                    PWD=getenv("PWD");
                    printf("%.5d %s@%s:%s$ ",command_nr,USER,HOSTNAME,PWD);
    
                    gets(clinput);
                    system(clinput);
    
                    command_nr++;
            }
    	return 0;
    }
    It is a horrible code for some reason. I conciously use the gets()-function in
    this old-style little program - do you see why?
    Tonto, where are you when I need you


    As per your string-problem.

    System expects a char-buffer (zero-limited string), but you provide
    a string-object. The way to convert a string-object to a char-buffer is via
    c_str(), as already mentioned by h3r3tic.

    ...please post your code when finished
    /edit: if I understand you correctly, you have some working "simple fake shell"
    by now...

    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  5. #5
    Member
    Join Date
    Apr 2006
    Posts
    66
    Thanks this really helped, I ended up just using the s.c_str().

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •