Newbie Turn off Echo in C

C

Chris

Hey all I know how to turn off echoing in a shell but I want to know
how I can do that in a C program when using getchar or scanf . Thanks
for your help.....
 
J

Joona I Palaste

Chris said:
Hey all I know how to turn off echoing in a shell but I want to know
how I can do that in a C program when using getchar or scanf . Thanks
for your help.....

Short answer:
You can't.

Long answer:
When you write text from your keyboard, it doesn't go to C's stdin. It
goes to your console, which does two things:
(1) write it to your screen, and
(2) send it to C's stdin.
As by the time C ever sees your text it has already been written to your
screen, C has a snowball's chance of erasing it from your screen.
Therefore you have to prevent your console from writing it there in the
first place. How you do this is very much OS-specific and can't be done
in standard C.
 
C

Chris

Joona I Palaste said:
Short answer:
You can't.

Long answer:
When you write text from your keyboard, it doesn't go to C's stdin. It
goes to your console, which does two things:
(1) write it to your screen, and
(2) send it to C's stdin.
As by the time C ever sees your text it has already been written to your
screen, C has a snowball's chance of erasing it from your screen.
Therefore you have to prevent your console from writing it there in the
first place. How you do this is very much OS-specific and can't be done
in standard C.


Well I would totally have to disagree. After searching around I found
snippet of code that can stop text from going to the console. Take a
look below. This is on a Linux Box. Turning off echoing in a root
shell with stty is easy but this worked using C.

static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}
 
R

Richard Bos

^^^^^^^^^^
Well I would totally have to disagree. After searching around I found
snippet of code that can stop text from going to the console. Take a
look below. This is on a Linux Box. Turning off echoing in a root
shell with stty is easy but this worked using C.

static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}

And how is this Standard C? This is not ISO, and therefore off-topic here -
but probably on-topic on a Linux programming group. Which is exactly what
Joona wrote.

Richard
 
M

Mark McIntyre

Well I would totally have to disagree. After searching around I found
snippet of code that can stop text from going to the console. Take a
look below. This is on a Linux Box. Turning off echoing in a root
shell with stty is easy but this worked using C.

I hate to point this out to you, but this code is not C.
static struct termios stored_settings;

implementation specific extension
void echo_off(void)
{
struct termios new_settings;

implementation specific extension
tcgetattr(0,&stored_settings);

implementation specific extension
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);

implementation specific extension

Style point: you can't return from a void function.

ok, this line is C.
 
D

Dan Pop

In said:
Well I would totally have to disagree. After searching around I found
snippet of code that can stop text from going to the console. Take a
look below. This is on a Linux Box. Turning off echoing in a root
shell with stty is easy but this worked using C.

Well, you can always use stty in a C program (think system()) to avoid
getting your hands dirty.
static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}

Dan
 
R

Richard Bos

Mark McIntyre said:
Style point: you can't return from a void function.

Wrong. You can't return _a value_ from a void function. You can
certainly return from it. After all, how else would you return from it
prematurely? C is not Pascal, after all.

Richard
 
M

Mark McIntyre

Style point: you can't return from a void function.

Wrong. You can't return _a value_ from a void function. You can
certainly return from it. [/QUOTE]

Thats what I meant. The return is useless in this example.
After all, how else would you return from it
prematurely?

By using the One True Control Style. To misquote highlander: "there
can /be/ only one return point".
C is not Pascal, after all.

and here's me having 1-based arrays as recently as y'day. Shoot me.
 
R

Richard Bos

Mark McIntyre said:
Wrong. You can't return _a value_ from a void function. You can
certainly return from it.

Thats what I meant. The return is useless in this example.[/QUOTE]

Not quite; it has no effect on the compiled code, but it does have an
effect on the maintainer. It is, in any case, far from incorrect, and I
doubt many people would call it bad style. In fact, I'd call not having
it, well, not _bad_ style, but slightly off, certainly.
By using the One True Control Style. To misquote highlander: "there
can /be/ only one return point".

Vade retro, Niklaus Wirth!

Richard
 
C

Chris

Well, you can always use stty in a C program (think system()) to avoid
getting your hands dirty.


Dan

So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.
 
J

Joona I Palaste

Chris said:
So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.

Whether it's C is a matter of terminology. It's certainly not
*standard* C, and very much dependent on the OS API you are using.
Most people classify it as "C with non-standard extensions". I count
myself amongst them.
 
A

Arthur J. O'Dwyer

Dan Pop wrote...

So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.

% cat test.c
static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}

% gcc -c test.c
test.c: In function `echo_off':
test.c:4: storage size of `new_settings' isn't known
test.c:7: `ECHO' undeclared (first use in this function)
test.c:7: (Each undeclared identifier is reported only once
test.c:7: for each function it appears in.)
test.c:8: `TCSANOW' undeclared (first use in this function)
test.c: At top level:
test.c:1: storage size of `stored_settings' isn't known
%


It doesn't matter that it compiles as a .c program; as long as
it *doesn't* compile as a C program, it's not, strictly speaking,
written in C.

Technically, what you've got there is something which *could*
be a C program, if you added some supporting code (e.g., definitions
for structures and a few enumerated constants). Then you'd have
a C program that produced undefined behavior when run.

Standard C doesn't support colors; thus, if you're claiming the
above code supports colors, then either you're mistaken, or the
above code is not standard C (which is the only kind of C we
discuss here).

HTH,
-Arthur
 
R

Richard Bos

So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.

Not on my implementation, it doesn't. It's C plus something
system-specific.

Richard
 
C

Christopher Benson-Manica

Chris said:
So what everyone is saying is that this is not C ? Then what is it ?
It compiles as a .c program.

In an attempt to clarify what others have said:

1) ANSI C is a standard specification by which ALL C implementations must
abide, regardless of what system they run on.
2) "C" in the context of this newsgroup is synonymous with "ANSI C."
3) The code you posted contains extensions to ANSI C that are specific to a
particular system. A Windows programmer writing ANSI C could not expect
to be able to compile your code, for example. Thus, your code is not C.
 
D

Dan Pop

In said:
In an attempt to clarify what others have said:

1) ANSI C is a standard specification by which ALL C implementations must
abide, regardless of what system they run on.
2) "C" in the context of this newsgroup is synonymous with "ANSI C."
3) The code you posted contains extensions to ANSI C that are specific to a
particular system. A Windows programmer writing ANSI C could not expect
to be able to compile your code, for example. Thus, your code is not C.

With the inclusion of the appropriate headers, this code satisfies the
definition of "conforming C program" as provided by the C standard.

It is downright idiotic to claim that C code using platform-specific
extensions is not C. By this logic, there are extremely few real world
programs written in C and most C programmers are not writing C code most
of the time.

There is nothing wrong with telling that a certain C program is not
portable, but it is bullshit to say that such a program is not a C
program. If it compiles with a C compiler, *when invoked as a C compiler*
*without generating any diagnostic required by the standard*, then it
qualifies as a C program. According to the standard itself.

Dan
 
C

Christopher Benson-Manica

Dan Pop said:
It is downright idiotic to claim that C code using platform-specific
extensions is not C. By this logic, there are extremely few real world
programs written in C and most C programmers are not writing C code most
of the time.

Okay :( I will try not to say any more stupid things. I'm sorry.
 
C

Chris

s/ANSI/ISO/, since many of us are not USAnian, but yes.

Richard

OK here is all of the code. This was a attempt to hide the users
input.

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

static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}

void echo_on(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
main()
{
int ch;
int ph = "*";
void echo_off();
while((ch = getchar()) != EOF)
putchar(ph);
void echo_on();

}


So what every one is saying is that this is a OS specific funtion on a
OS platform that itself is mainly written in C so the function is a C
funtion dependent on the OS. WOW... This is not standard C but a
custom C funtion that works for this OS. Fair Enough ..... Thanks
all.....
 
W

Wolfgang Riedel

Chris said:
OK here is all of the code. This was a attempt to hide the users
input.

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

static struct termios stored_settings;
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}

void echo_on(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
main()
{
int ch;
int ph = "*";
void echo_off();
while((ch = getchar()) != EOF)
putchar(ph);
void echo_on();

}

So what every one is saying is that this is a OS specific funtion on a
OS platform that itself is mainly written in C so the function is a C
funtion dependent on the OS. WOW... This is not standard C but a
custom C funtion that works for this OS. Fair Enough ..... Thanks
all.....

btw. this should be:

main()
{
int ch;
int ph = "*";
void echo_off();
while((ch = getchar()) != EOF)
putchar(ph);
void echo_on();

}

or better

float* /* maybe somebody whishes to know the outcome, so give them a clue
/* har, har... */ /* ignore the warning! */ */
main()
{
int ch;
nt ph = "*";
id echo_off();
e((ch = getchar()) != EOF)
ar(ph);
cho_on();
/* documentation here later on!! */
}

g
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top