1836629 Members
1796 Online
110102 Solutions
New Discussion

echo off without curses.

 
SOLVED
Go to solution
jp
Frequent Advisor

echo off without curses.

Anybody BTDT please? Dont need all the capabilities of curses, just want to turn echo off programatically.

TIA,
jp
13 REPLIES 13
Mic V.
Esteemed Contributor

Re: echo off without curses.

stty -echo

should do the trick.

Mic
What kind of a name is 'Wolverine'?
Sridhar Bhaskarla
Honored Contributor

Re: echo off without curses.

Hi,


'stty -echo' and 'stty echo' are the ones you are looking for. Below is an example script.

printf "Enter your password:"
stty -echo
read passwd
stty echo
printf "\nYour password is $passwd\n"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: echo off without curses.

Man 7 termio and all will be revealed but if all you want to do is turn echo off and read a string, by far, the easist method is the getpass() function. Man getpass for details.
If it ain't broke, I can fix that.
Ravi_8
Honored Contributor

Re: echo off without curses.

Hi

"stty - echo" will turn off the echo
never give up
Elmar P. Kolkman
Honored Contributor

Re: echo off without curses.

The solutions provided so far all look right, but I think the right answer depends on the language you try to use to program. In C it is done different from how it is done in a shell script...

So more info can help in getting (possible) more precise answers...
Every problem has at least one solution. Only some solutions are harder to find.
jp
Frequent Advisor

Re: echo off without curses.

Thanks guys, looking a lot better. We need to do this from inside a program, so something in C is what I need. Termio ??? is the go? So far we are testing with stty -echo before starting the program, but preferable from inside the prog.
TIA,
jp
Jean-Louis Phelix
Honored Contributor

Re: echo off without curses.

Hi,

Also you could tell us why you want to turn echo off. It's often to read a password and in this case you could use getpass() API which do it and turns off echo while reading ...

Regards.
It works for me (© Bill McNAMARA ...)
Elmar P. Kolkman
Honored Contributor

Re: echo off without curses.

If done from with a C program, look at the getpass() (man getpass) function, termio (man termio) library or stty system call (man 2 stty).
All of these could be used to do what you want.
Every problem has at least one solution. Only some solutions are harder to find.
jp
Frequent Advisor

Re: echo off without curses.

We have LOTS of code doing Block mode HP screen reading (MPE), which means terminal does its own echoes, so you have to turn off system echo. This relieves the system of lots of load anyway. The length of the fields, and Block means getpass() wont do it. Sounds like termio or if stty is accessible from program, one of these will be the way to go.
Thanks
jp
Elmar P. Kolkman
Honored Contributor

Re: echo off without curses.

HP's blockmode supports password fields too, if I'm not mistaken. I don't have a manual of the HP terminal codes within reach, but there should be a field type available for this.
Every problem has at least one solution. Only some solutions are harder to find.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: echo off without curses.

Okay this should be a simple example of turning off echo:

#include
#include
#include
#include

#define STDIN_FDES 0

struct termios save;

int main(int argc, char *argv[])
{
int cc = 0;
char s_tmp[80],*p = NULL;
struct termios work;

cc = tcgetattr(STDIN_FDES,&save);
work = save;
work.c_lflag &= ~(ECHO);
cc = tcsetattr(STDIN_FDES,TCSANOW,&work);
(void) printf("\nEnter value: ");
(void) fflush(stdout);
p = fgets(s_tmp,sizeof(s_tmp),stdin);
if (p != NULL) (void) printf("Out -> %s\n",p);
cc = tcsetattr(STDIN_FDES,TCSANOW,&save);
return(cc);
}

NOTE: It is very important that you have signal handlers to catch SIGINT, SIGTERM, ... and reset the terminal using the original termios because the last tcsetattr() wins and this applies to the terminal device NOT simply the process. If you leave the process with echo off, it will be off in the shell as well.
If it ain't broke, I can fix that.
jp
Frequent Advisor

Re: echo off without curses.

I think we have a winner! Thanks Clay. As soon as we get this working, I will return with the news.
Thanks again everybody who responded.

jp
A. Clay Stephenson
Acclaimed Contributor

Re: echo off without curses.

Well, if you are going to use my snippet I might as well make it more robust. Note that I have added the signal handlers I mentioned earlier so that no matter what keys you press or if you kill it externally , the controlling terminal is restored to echo. The termio's function also allow you to do single character input without waiting for a LF or to timeout a read if a character is not received in a given period of time.

I copied my original posting and spent about 3 minutes making him pretty.

If it ain't broke, I can fix that.