473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Termios is freezing

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
6 2794
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
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
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
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
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
hem
Hi,

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

Regards,
Reddy

Apr 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Hodges | last post by:
Posted with an Unregistered Version of NewsHunter - The Newsgroup Utility for OS X. Get your copy today at: http://www.parkersoftware.com/products/newshunter/ Can anyone provide details on how...
3
by: henry xie | last post by:
Hi, All: I am a newbie in Python. I am trying to use pexpect package in the project. But when I installed the package, it couldn't be used as termios.py was missing. I checked the LIB...
5
by: Petr Jakes | last post by:
On my box (Fedora Core4, Python 2.4.1) I am getting following error: >>> import termios, sys >>> fd = sys.stdin.fileno() >>> oldSettings = termios.tcgetattr(fd) Traceback (innermost last): File...
7
by: bearophileHUGS | last post by:
Most of my ideas seem usless or stupid, but I think expressing them here doesn't harm much. This is an idea for Py 3.0, because it's not backward compatible. Dicts and sets require immutable...
0
by: Derek Peschel | last post by:
Should I add an RFE to SourceForge too? I'd like a wide audience in case someone has enough experience to comment or is solving the same problem. I'm using the urwid library which uses curses. ...
0
by: Laszlo Nagy | last post by:
Hi All, I have a python program that downloads database backups from a remote server, and tries to replace the local database with the downloaded backup. The database is a PostgreSQL server and my...
2
by: Brice Rebsamen | last post by:
Hi I wrote the following program that reads the keyboard in medium raw mode (keycode mode). Here is the initialization code, the full code is at the end. fd = open("/dev/tty0", O_RDONLY);...
3
by: goblin | last post by:
Hi all I'm struggeling to get my serial port settings changed using termios. I can change the baud rate fine, but none of the control flags respond to my changes. As an example: #include...
1
by: jorba101 | last post by:
I'm programming an ARM's UART that comes with a library implementing the standard unix termios interface. Regarding termios, I have following trouble: When I do following syscall: write(...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.