how to hide characters on screen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • torno

    how to hide characters on screen

    Hi,
    Can someone guide me as to how I can modify the code below to hide
    characters from displaying on the screen as they are entered? If there is
    a better way of achieving this, please let me know.

    I'm running this on unix (Solaris8).

    thanks.

    #include <stdio.h>

    int main(void)
    {
    int passwdstr;

    printf ( "Enter password: ");
    while ( ( passwdstr=getch ar() ) != '\r' ) {
    putchar ('*');
    }
    return 0;
    }



  • E. Robert Tisdale

    #2
    Re: how to hide characters on screen

    torno wrote:[color=blue]
    >
    > Can someone guide me as to how I can modify the code below to hide
    > characters from displaying on the screen as they are entered?
    > If there is a better way of achieving this, please let me know.
    >
    > I'm running this on unix (Solaris8).
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > int passwdstr;
    >
    > printf ( "Enter password: ");
    > while ( ( passwdstr=getch ar() ) != '\r' ) {
    > putchar ('*');
    > }
    > return 0;
    > }[/color]
    [color=blue]
    > cat main.c[/color]
    #include <unistd.h>
    #include <stdio.h>

    int main(int argc, char* argv[]) {
    char *getpass(const char* prompt);
    fprintf(stdout, "password = %s\n",
    getpass("Enter password: "));
    return 0;
    }
    [color=blue]
    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main[/color]
    Enter password:
    password = password

    Comment

    • Richard Bos

      #3
      Re: how to hide characters on screen

      "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote:
      [color=blue]
      > torno wrote:
      >[color=green]
      > > Can someone guide me as to how I can modify the code below to hide
      > > characters from displaying on the screen as they are entered?
      > > If there is a better way of achieving this, please let me know.[/color][/color]

      Read the FAQ. The FAQ is your friend.
      <http://www.eskimo.com/~scs/C-faq/q19.1.html>. Admittedly, the way this
      question is phrased in the index for section 19 is incomplete; but a
      search of the text version (posted here regularly) on "password"
      would've turned up this question.
      [color=blue][color=green]
      > > cat main.c[/color]
      > #include <unistd.h>[/color]

      This is not ISO C, and therefore off-topic.
      [color=blue]
      > char *getpass(const char* prompt);[/color]

      This is not ISO C, and therefore off-topic. Moreover, function
      declarations within other functions (yes, even within main()) are a Bad
      Thing.
      [color=blue]
      > getpass("Enter password: "));[/color]

      This is not ISO C, and therefore off-topic.
      [color=blue][color=green]
      > > gcc -Wall -std=c99 -pedantic -o main main.c[/color][/color]

      We don't care what your broken compiler says, it's still off-topic.

      I'd tell you to read the bloody FAQ, as well, but in your case it would
      probably be a waste of time, since you don't appear to give a damn.

      Richard

      Comment

      Working...