Termios is freezing

H

hem

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.

Any pointers/help would be greatly appreciated.

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

int main(int argc, char *argv[])
{
struct termios oldt,
newt;
char ch;
char userpasswd[50];

printf("enter password:");
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
gets(userpasswd);
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
printf("\nPassword enetered %s\n", userpasswd);
return 0;
}


Thanks,
Reddy
 
P

Phlip

hem said:
#include <termios.h>

Please use Google Groups to find a newsgroup that discusses termios. You
will get a much better answer there than on a generic newsgroup about C++.

(Also, your code is C, so learn the difference!)
 
S

Simon Biber

hem said:
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.

Any pointers/help would be greatly appreciated.

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

<stdio.h> and <string.h> are OK but should be replaced by <cstdio> and
<cstring> if your C++ compiler supports it.

int main(int argc, char *argv[])

The argc and argv variables are not used. Consider using the other form
of main, with no arguments.
{
struct termios oldt,
newt;
char ch;

The ch variable is not used.
char userpasswd[50];

printf("enter password:");

There is no newline here, and no flush, so it may not be output on your
screen before you wait for user input. Try fflush(stdout);
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
gets(userpasswd);

The gets function is dangerous. What if the user enters more than 49
characters? The solution is to use an input function that limits the
length of input. A reasonable replacement is fgets. But then you have to
manually remove the newline character.
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
printf("\nPassword enetered %s\n", userpasswd);
return 0;
}

Your actual problem is likely to be caused by the system-specific stuff,
which I have ignored. Post to a unix newsgroup for a more useful
response if necessary. I suggest comp.unix.programmer

Simon.
 
S

Simon Elliott

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.

Any pointers/help would be greatly appreciated.

comp.unix.programmer might be a better newsgroup for this question.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include<string.h>

int main(int argc, char *argv[])
{
struct termios oldt,
newt;
char ch;
char userpasswd[50];

printf("enter password:");
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );

I'm not sure what gets() does when canonical mode is disabled. As I
mentioned, the guys over at comp.unix.programmer will know.
 
S

Simon Biber

Phlip said:
hem wrote:




Please use Google Groups to find a newsgroup that discusses termios. You
will get a much better answer there than on a generic newsgroup about C++.

I agree. As I and the other Simon suggested, comp.unix.programmer is a
good choice.
(Also, your code is C, so learn the difference!)

The code provided appears to be valid as both C and C++, apart from the
system-specific parts. I don't see how you can conclude that the OP does
not know the difference between C and C++.

Simon.
 
P

Phlip

Simon said:
The code provided appears to be valid as both C and C++, apart from the
system-specific parts. I don't see how you can conclude that the OP does
not know the difference between C and C++.

There's a spectrum of code styles, from C-style to C++-style. The given code
used stuff that C++ should not use, such as <stdio.h>.

However you are correct that I should have stated the observation as a
question.
 
H

hem

Hi,

Thanks for your valuable responses. I will try it out in
comp.unix.programmer.

Regards,
Reddy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top