Password prompt in unix w/o \b

This is a discussion on Password prompt in unix w/o \b within the C++ Programming forums, part of the General Programming Boards category; c/c++ newbie is trying to improve the c++ classes by, well, making them more c++ized. C seems easier to me ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    46

    Password prompt in unix w/o \b

    c/c++ newbie is trying to improve the c++ classes by, well, making them more c++ized. C seems easier to me but that's no reason to not learn c++.

    Yes, I looked at the faqs & did a search (web & local) before posting this. Doing a print char erase char sequence for a password prompt, like in the faq just rubs me wrong. Here's what I've got but it's so very c & I was wondering if there is a more natural c++ized way of doing this.
    Code:
    //#############################################################################
    // Password = Prompt for same (password is always 16 chars in class)
    //#############################################################################
    void DB::Password(char* pass) throw (TerminalError){
        struct termios ttyinit; //Term info before noecho for password prompt
        struct termios ttynew;  //Used to set noecho for password prompt
        FILE*  ttyin;           //Redirect input  to the terminal
        FILE*  ttyout;          //Redirect output to the terminal
    
        //=========================================================================
        // Need to redirect i/o to the terminal for this prompt
        //=========================================================================
        ttyin  = fopen("/dev/tty", "r");
        ttyout = fopen("/dev/tty", "w");
        if (!ttyin || !ttyout){
            if (ttyin)  fclose(ttyin);
            if (ttyout) fclose(ttyout);
            throw TerminalError();
        }
    
        //=========================================================================
        // Need to save old terminal settings & then set up NOECHO
        //=========================================================================
        tcgetattr(fileno(ttyin), &ttyinit);
        ttynew = ttyinit;
        ttynew.c_lflag &= ~ECHO;
    
        //=========================================================================
        // Now prompt for the SQL password
        //=========================================================================
        fprintf(ttyout, "Enter SQL password: ");
        fflush(ttyout);
        if (tcsetattr(fileno(ttyin), TCSAFLUSH, &ttynew)) throw TerminalError();
        fgets(pass, 16, ttyin);
        for (int i = 0; pass[i] && i < 16; i++) if (pass[i] == '\n') pass[i] = 0;
        tcsetattr(fileno(ttyin), TCSANOW, &ttyinit);
        fprintf(ttyout, "\n");
    
        fclose(ttyin);
        fclose(ttyout);
    }
    OK ignore what the code tags did to my comments

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    Thanks for the info, I didn't know that this function existed. However, when I do a man on this (linux) it says...

    DESCRIPTION
    This function is obsolete. Do not use it.
    And then lists lots of weird behaivors like not obeying control-c etc. Being a newbie I tend to heed these warnings more than Senior Member may because you know better which rules to break than I. I will however test this function & see how it differs from and improves upon my hand rolled one.

    The bottom line is that it's not critical to change, infact, the ability to leverage my meager C abilities is a nice springboard into C++.

    I thought I'd try & see if there might be a stream method for turning off the echo that was poorly documented or something. No luck.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 09:54 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Hide Password On Unix
    By Moepi in forum C Programming
    Replies: 3
    Last Post: 05-24-2002, 07:33 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21