04-27-06 12:56 PM
Reddy wrote:
> Hi,
>
> I have the following small program which read password from user after
> echoing off. But the problem is, it is freezing for some time (not sure
> about the duration) before going to the next statement and I have to
> press "enter" multiple times (maximum 4, it is not consistent though).
> I am trying it on a hp-ux machine with aCC compiler.
>
> int main()
> {
> struct termios oldt, newt;
> char userpasswd[50];
>
> printf("enter password:");
>
> tcgetattr( STDIN_FILENO, &oldt );
> newt = oldt;
> newt.c_lflag &= ~( ICANON | ECHO );
Common problem. If you're going to turn off ICANON,
you have to set VMIN and VTIME. Read the termios man
page more carefully. On many machines, c_cc[VMIN] is
the same value as c_cc[VEOF]. VMIN is for non-canonical\
processing and VEOF is for canonical processing. VEOF is
ususally set to Ctrl-D, which is 4, so if you don't change
VMIN when turning off ICANON, it'll wait for 4 chars to be
input.
> tcsetattr( STDIN_FILENO, TCSANOW, &newt );
> gets(userpasswd);
> tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
> printf("\nPassword enetered %s\n", userpasswd);
> return 0;
>
> }
If all you're doing is getting a password, why not just use
the getpass(2) routine? It does the same thing.
--
Brian Bebeau
SecurePipe, Inc.
http://www.securepipe.com
[ Post a follow-up to this message ]
|