Re: how to write a password routine that doesn't echo to the screen?

Discussion in 'C Programming' started by Phoe6, Aug 7, 2004.

  1. Phoe6

    Phoe6 Guest

    Pflanzen Gold wrote:
    > Hi,
    >
    > When entering a password we don't really see the characters - we see
    > **** instead. How can I write such a routine in C? How can I 'echo
    > off' the inputs?

    ----
    I came to know from the Archives that ANSI Std C does not allow such a
    provision.
    One thing was for sure, you can never use getchar() function,so the
    hunt of other functions being and you will stumble upon getpass().
    Yo! Your job is done. :)
    Check with man getpass and heres a simple program to get started.

    /* getpass demo */

    #include<unistd.h>
    #include<stdio.h>


    int main(void)
    {
    char *my_pass;


    my_pass = getpass("Password: ");


    printf("I got the password");
    printf("%s",my_pass);


    return 0;
    }

    Cheers!
    Senthil

    HomePage: http://puggy.symonds.net/~senthil
    Phoe6, Aug 7, 2004
    #1
    1. Advertising

  2. Phoe6

    CBFalconer Guest

    Phoe6 wrote:
    >

    .... snip ...
    > Check with man getpass and heres a simple program to get started.
    >
    > /* getpass demo */
    >
    > #include<unistd.h>

    .... snip ...

    This is a non-standard header, and makes everything OT here.
    Please do not recommend non-standard code to anyone here.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 7, 2004
    #2
    1. Advertising

  3. Phoe6

    Moonie Guest

    if using dos gcc -DUSE_DOS
    if using win32 gcc -DUSE_WIN32
    if using conio gcc -c -DUSE_CONIO
    if using linux gcc -c -DUSE_LINUX

    when using windows include windows.h
    when using dos include dos.h
    when using conio include conio.h
    when using linux include stdio.h

    in fact stdio.h should be declared anyways.

    i hope this helps.





    //--------------------------------------------------------------------------
    int mygetch( void )
    {
    #if defined(USE_CONIO)
    return getch();
    #elif defined(USE_WIN32)
    HANDLE hInput;
    BYTE ch;
    DWORD n;

    /* Grab the STDIN handle. */
    hInput = GetStdHandle(STD_INPUT_HANDLE);
    /* Turn off everything but input processing. */
    SetConsoleMode(hInput,ENABLE_PROCESSED_INPUT);
    /* Read a single character from the console. */
    ReadConsole(hInput,&ch,1,&n,NULL);

    /* Return that character,
    getch() in conio traditionally returned an int.
    */
    return (int)ch;
    #elif defined(USE_DOS)
    union REGS Regin;
    static int chbuf = EOF;
    if ( chbuf != EOF )
    { Regin.h.al = (uchar) chbuf;
    chbuf = EOF;
    }
    else
    { Regin.h.ah = 0;
    int86( 0x16, &Regin, &Regin );
    chbuf = (Regin.h.al==0 || Regin.h.al==0xE0) ? Regin.h.ah
    EOF;
    }
    return (int) Regin.h.al;
    #else
    unsigned char x;
    read( 0, &x, 1 );
    return (int) x;
    #endif
    }

    char* getpass( const char *prompt )
    { int ch, len, maxlen=9;
    static char s[10], *p;

    len = 0;
    puts((char*)prompt);
    p = s;

    while ((ch = mygetch()) != '\r' && ch != '\n' && ch != 27 && le
    <= maxlen)
    { if (ch == '\b')
    { if ( len > 0 )
    { putchar('\b');
    putchar(' ');
    putchar('\b');
    --len;
    --p;
    }
    }
    else if (ch < 32 || ch > 127);
    else
    { putchar("*");
    *p++ = (char) ch;
    ++len;
    }
    }

    *p = '\0';
    return( s );



    -
    Mooni
    -----------------------------------------------------------------------
    Posted via http://www.codecomments.co
    -----------------------------------------------------------------------
    Moonie, Aug 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pflanzen Gold
    Replies:
    11
    Views:
    438
    Moonie
    Aug 12, 2004
  2. AAaron123
    Replies:
    2
    Views:
    2,106
    AAaron123
    Jan 16, 2009
  3. AAaron123
    Replies:
    1
    Views:
    1,321
    Oriane
    Jan 16, 2009
  4. ruby talk

    Get password Or no echo

    ruby talk, Oct 27, 2004, in forum: Ruby
    Replies:
    4
    Views:
    183
  5. Marc Soda

    Password Echo

    Marc Soda, May 9, 2007, in forum: Ruby
    Replies:
    6
    Views:
    186
    Dan Zwell
    May 10, 2007
Loading...

Share This Page