By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,661 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,661 IT Pros & Developers. It's quick & easy.

Termios is freezing

P: n/a
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

Apr 22 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
hem wrote:
#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!)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 22 '06 #2

P: n/a
hem 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.

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.

<termios.h> and <unistd.h> are system headers, and are not defined by
the C++ standard, so we can't help you with them here.

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.
Apr 22 '06 #3

P: n/a
On 22/04/2006, hem wrote:
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.
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
gets(userpasswd);
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
printf("\nPassword enetered %s\n", userpasswd);
return 0;
}


--
Simon Elliott http://www.ctsn.co.uk
Apr 22 '06 #4

P: n/a
Phlip wrote:
hem wrote:

#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++.


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.
Apr 22 '06 #5

P: n/a
Simon Biber wrote:
(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++.


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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 23 '06 #6

P: n/a
hem
Hi,

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

Regards,
Reddy

Apr 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.