Reading Input without echoing chars

Reading Input without echoing chars

Post by Joseph C. Ki » Wed, 03 Jul 1996 04:00:00



I'm looking for code or suggesting on how to read a string without having
the chars that are being typed echoed to the screen.  Just like what
most UNIX's do when you type your passwd during login.

I have already retrieved NetBSD source for login.c and getpass.c but
I cannot figure how the code they used achieves this.

I'm sure it is rather easy and I'm just overlooking something.

Joseph C King
Sysadmin
KCSD

 
 
 

Reading Input without echoing chars

Post by Andrew Gier » Wed, 03 Jul 1996 04:00:00



Quote:>I'm looking for code or suggesting on how to read a string without having
>the chars that are being typed echoed to the screen.  Just like what
>most UNIX's do when you type your passwd during login.

I believe this is in the FAQ.

Quote:>I have already retrieved NetBSD source for login.c and getpass.c but
>I cannot figure how the code they used achieves this.

>I'm sure it is rather easy and I'm just overlooking something.

The secret is: turn off ECHO in the terminal driver. This is done
using calls to tcgetattr()/tcsetattr() if using POSIX systems, or
ioctl(TCGETA/TCSETA) on SysV, or by the crude (but portable) method
of doing system("stty -echo").


"Usenet is like a herd of performing elephants with diarrhea; massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it." [Gene Spafford]

 
 
 

Reading Input without echoing chars

Post by Gabor Egres » Wed, 03 Jul 1996 04:00:00



: I'm looking for code or suggesting on how to read a string without having
: the chars that are being typed echoed to the screen.  Just like what
: most UNIX's do when you type your passwd during login.

: I have already retrieved NetBSD source for login.c and getpass.c but
: I cannot figure how the code they used achieves this.

: I'm sure it is rather easy and I'm just overlooking something.

: Joseph C King
: Sysadmin
: KCSD

try the following code
code begins ---------------------------------
#include <termios.h>
#include <unistd.h>
/****************************************************************************
noEcho :
    turn off echo; characters typed will not be echoed to stdout
    IN  : void
    OUT : 0 on correct functioning; -1 on error
****************************************************************************/
noEcho()
{
    struct termios attr;

    if(tcgetattr(0,&attr) < 0)
        return -1;

    attr.c_lflag &= ~ECHO;

    if(tcsetattr(0,TCSANOW,&attr) < 0)
        return -1;

    return 0;

Quote:}

/****************************************************************************
Echo :
    put console into echo mode
    IN  : void
    OUT : 0 on correct functioning; -1 on error
****************************************************************************/
Echo()
{
    struct termios attr;

    if(tcgetattr(0,&attr) < 0)
        return -1;

    attr.c_lflag |= ECHO;

    if(tcsetattr(0,TCSANOW,&attr) < 0)
        return -1;

    return 0;

Quote:}

end code -----------------------------------
--
---------------------------------------------------------------------


Canada

No beast so fierce but knows some touch of pity
But I know none, And therefore am no beast
              Richard III., William Shakespeare
vim : the best editor
---------------------------------------------------------------------

 
 
 

Reading Input without echoing chars

Post by robe » Thu, 04 Jul 1996 04:00:00



 >I'm looking for code or suggesting on how to read a string without having
 >the chars that are being typed echoed to the screen.
 >I have already retrieved NetBSD source for login.c and getpass.c but
 >I cannot figure how the code they used achieves this.

Basically, what is does is put /dev/tty in NO_ECHO mode with termios
(rather icky stuff).

                                                                robert

 
 
 

Reading Input without echoing chars

Post by Shado » Fri, 05 Jul 1996 04:00:00



Quote:>I'm looking for code or suggesting on how to read a string without having
>the chars that are being typed echoed to the screen.  Just like what
>most UNIX's do when you type your passwd during login.

It deals mostly with setting the tty attributes to remove the ECHO flag
on the tty. There's a number of ways to do this. Allow me to give
you two examples. One of them should work on BSD the other on SysVish
Unices.

I believe this one should work on BSD systems:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>

#define BUFFSIZE 256

main()
{
  struct termios term1;
  char buff[BUFFSIZE];

  bzero((char *)buff,BUFFSIZE);

  printf("Enter password:");

  /* Get terminal attributes */

  tcgetattr(0,&term1);

  /* Remove the echo flag so we wont echo back to the screen */

  term1.c_lflag &= ~ECHO;

  /* Set the terminal attributes */

  tcsetattr(0,0,&term1);

  fgets(buff,BUFFSIZE,stdin);

  /* Reset the terminal attributes to include the echo flag */

  term1.c_lflag |= ECHO;

  tcsetattr(0,0,&term1);

  printf("\n%s",buff);

  exit(0);

Quote:}

This one should work on SystemVish systems:

#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <strings.h>

#define BUFFSIZE 256

main()
{
  char buff[BUFFSIZE];
  struct termios term1;

  memset(buff,0,BUFFSIZE);

  printf("Enter password:");

  /* Get term attributes */

  ioctl(0,TCGETA,&term1);

  term1.c_lflag &= ~ECHO;

  ioctl(0,TCSETA,&term1);

  fgets(buff,BUFFSIZE,stdin);

  term1.c_lflag |= ECHO;

  ioctl(0,TCSETA,&term1);

  printf("%s",buff);

Quote:}

--
Thamer Al-Herbish

"*age angst paid off well.. now I'm bored and old."- Kurt Cobain.
-------------------------------------------------------------------------------
 
 
 

Reading Input without echoing chars

Post by Roger Espel Lli » Fri, 05 Jul 1996 04:00:00





>>I'm looking for code or suggesting on how to read a string without having
>>the chars that are being typed echoed to the screen.  Just like what
>>most UNIX's do when you type your passwd during login.

Check out the library function getpass(), which does just that for you.

Quote:>It deals mostly with setting the tty attributes to remove the ECHO flag
>on the tty. There's a number of ways to do this. Allow me to give
>you two examples. One of them should work on BSD the other on SysVish
>Unices.
>I believe this one should work on BSD systems:

Actually this is the POSIX way of doing it, and should work on all
modern systems.

[ code using tcgetattr(), tcsetattr() ]

Quote:>This one should work on SystemVish systems:

Not recommended... but some old SysV's only recognize this.

[ code using ioctl(0, TC{G,S}ETA...) ]

And then there's also the traditional BSD way to do this... it's
the only way that works on old BSD systems, including the NeXT:

#include <sgtty.h>
[ ... ]
struct sgttyb term, term0;

[ ... ]

ioctl(ttyfd, TIOCGETP, &term);
ioctl(ttyfd, TIOCGETP, &term0);
term.sg_flags &= ~ECHO;
ioctl(ttyfd, TIOCSETP, &term);
[ read pass ]
ioctl(ttyfd, TIOCSETP, &term0);
[ ... ]

        -Roger
--

WWW & PGP key: http://www.eleves.ens.fr:8080/home/espel/index.html