[comp.lang.c] Making chars disappear

james@castle.ed.ac.uk (J Gillespie) (02/18/91)

Does anyone know of a way to prevent characters being echoed as they
are typed in?  Like when you log in, your password doesn't get echoed.
I have a nasty feeling this may involve sending control codes to the
terminal.

-- 
  James Gillespie,     /~~~~~~~~\
 Edinburgh University /   @  @   \ "Oh wow.  What a bummer."
   james@ed.ac.uk    /     <      \		-- Truman Sparks, _Fandango_
____________________/  \________/  \__________________________________________

mike (02/18/91)

In an article, castle.ed.ac.uk!james (J Gillespie) writes:
>Does anyone know of a way to prevent characters being echoed as they
>are typed in?  Like when you log in, your password doesn't get echoed.
>I have a nasty feeling this may involve sending control codes to the
>terminal.

This is a function of the operating system, and not C.  Since it comes
up every so often, I'll post an answer to comp.unix.progammer.

BTW, no operating system that I know of accompishes supressing echo
by using control characters; it is usually a bit flag in a status word
for the port, or something along those lines.


-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?

jwm712@unhd.unh.edu (Jonathan W Miner) (02/18/91)

In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
>
>Does anyone know of a way to prevent characters being echoed as they
>are typed in?  Like when you log in, your password doesn't get echoed.
>I have a nasty feeling this may involve sending control codes to the
>terminal.
You will have to get the terminal driver to shift into RAW mode.  This takes
away all off the 'nice' features of line-oriented input.  Look into the
ioctl() system call and <sgtty.h>.  (I just don't have the time.)

-- 
Jonathan Miner        | I don't speak for UNH, and UNH does not 
jwm712@unhd.unh.edu   | speak for me! 
(603)868-3416         | Hacking to survive......

tchrist@convex.COM (Tom Christiansen) (02/18/91)

From the keyboard of jwm712@unhd.unh.edu (Jonathan W Miner):
:In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
:>
:>Does anyone know of a way to prevent characters being echoed as they
:>are typed in?  Like when you log in, your password doesn't get echoed.
:>I have a nasty feeling this may involve sending control codes to the
:>terminal.
:You will have to get the terminal driver to shift into RAW mode.  This takes
:away all off the 'nice' features of line-oriented input.  Look into the
:ioctl() system call and <sgtty.h>.  (I just don't have the time.)

NO! Raw mode doesn't automatically get you no echo.  I guess now we'll
have 3 wrong answer and 6 semi-right ones.

This is *not* a C-related question.  It's an OS-related question, and
cannot be answered portably.  It's very closely related to "how do I get
one character from the keyboard," which doesn't belong here either.

See the answer to the latter question in the FAQ for comp.unix.questions
and interpolate, which you would have known had you read the FAQ for this
group.  I'm sorry if this sounds testy.  The net is not really supposed to
be a substitute for RTFM or asking around at your own site.  And even when
you've no other recourse, reading the FFAQs first would often answer the
simpler questions.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

dave@cs.arizona.edu (Dave P. Schaumann) (02/18/91)

In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
>
>Does anyone know of a way to prevent characters being echoed as they
>are typed in?  Like when you log in, your password doesn't get echoed.
>I have a nasty feeling this may involve sending control codes to the
>terminal.

I suspect that this is best done with an un-buffered read of the keyboard.
Unfortunately, there is no standard way to do this.

Even more so for any control code you might send, since that would be
terminal dependant.

-- 
Dave Schaumann      | Is this question undecidable?
dave@cs.arizona.edu |

james@castle.ed.ac.uk (J Gillespie) (02/18/91)

I'm sorry I asked this question in this group.  Thanks for all your
answers and for pointing out that this question was OS-related, not
-- 
  James Gillespie,     /~~~~~~~~\
 Edinburgh University /   @  @   \ "Oh wow.  What a bummer."
   james@ed.ac.uk    /     <      \		-- Truman Sparks, _Fandango_
____________________/  \________/  \__________________________________________

frank@grep.co.uk (Frank Wales) (02/19/91)

In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
>Does anyone know of a way to prevent characters being echoed as they
>are typed in?

See the manual pages for stty, ioctl and either termio or tty dependent
on your system flavour -- note that some entries occur in more than one manual
section.  Also, this isn't a C question, so I've sent follow-ups elsewhere.
--
Frank Wales, Grep Limited,             [frank@grep.co.uk<->uunet!grep!frank]
Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (02/19/91)

In article <22233@hydra.gatech.EDU>, ccastdf@prism.gatech.EDU (Dave) writes:
> In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
> >Does anyone know of a way to prevent characters being echoed as they
> >are typed in?

> In unix, you need to set the mode to raw.  This can be done by invoking
> an ioctl command, or by isuing an stty system call.

(a) This is a system-specific question, and should be asked in an
    appropriate system-specific newsgroup.
(b) Check "getpass" in section 3 of the UNIX manual
(c) The claim that you have to go into "raw" mode in UNIX is *BOGUS*.
    It simply is not true.  It's ever so simple:
	system("stty -echo");	/* disables echoing */
	system("stty echo");	/* enables it again */
    other aspects, such as editing characters, are NOT affected by this.
    The *only* thing that is switched off is echoing.  (Do be careful
    to switch it back on even if you get a "drop dead" signal.)

-- 
Professional programming is paranoid programming

larry@st-andy.uucp (Larry Martell) (02/25/91)

In article <8531@castle.ed.ac.uk> james@castle.ed.ac.uk (J Gillespie) writes:
>
>Does anyone know of a way to prevent characters being echoed as they
>are typed in?  Like when you log in, your password doesn't get echoed.
>I have a nasty feeling this may involve sending control codes to the
>terminal.
>
>-- 

This is how I do it on a Sun running 4.1:

#include <fcntl.h>
#include <sys/termios.h>

main()

{

int fd;
struct termios  termios;

        if ((fd = open("/dev/tty",O_RDWR)) < 0) {
                perror("open failed\n");
                exit(1);
        }

        if (ioctl(fd,TCGETS,&termios) < 0) {
                perror("ioctl failed\n");
                exit(1);
        }

        termios.c_lflag &= ~ECHO;

        if (ioctl(fd,TCSETS,&termios) < 0) {
                perror("ioctl failed\n");
                exit(1);
        }
}

See termio(4) for more info.
-- 
Larry Martell
uunet!st-andy!larry
212-668-9478