Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Turn off Echo in C

Reply
Thread Tools

Newbie Turn off Echo in C

 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-15-2003
Chris <(E-Mail Removed)> scribbled the following:
> http://www.velocityreviews.com/forums/(E-Mail Removed) (Dan Pop) wrote in message news:<bmh6b5$srg$(E-Mail Removed)>...
>> In <(E-Mail Removed) > (E-Mail Removed) (Chris) writes:
>>
>> > 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


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

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      10-15-2003

On Wed, 15 Oct 2003, Chris wrote:
>
> Dan Pop wrote...
> > Chris writes:
> > > 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;
> > >}

>
> 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

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      10-15-2003
(E-Mail Removed) (Chris) wrote:

> > In <(E-Mail Removed) > (E-Mail Removed) (Chris) writes:
> >
> > >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;
> > >}

>
> 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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-15-2003
Chris <(E-Mail Removed)> spoke thus:

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

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-15-2003
In <bmjobn$991$(E-Mail Removed)> Christopher Benson-Manica <(E-Mail Removed)> writes:

>Chris <(E-Mail Removed)> spoke thus:
>
>> 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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-15-2003
Dan Pop <(E-Mail Removed)> spoke thus:

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

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-16-2003
Christopher Benson-Manica <(E-Mail Removed)> wrote:

> Chris <(E-Mail Removed)> spoke thus:
>
> > 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


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

Richard
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      10-16-2003
(E-Mail Removed) (Richard Bos) wrote in message news:<(E-Mail Removed)>...
> Christopher Benson-Manica <(E-Mail Removed)> wrote:
>
> > Chris <(E-Mail Removed)> spoke thus:
> >
> > > 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

>
> 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.....
 
Reply With Quote
 
Ed Morton
Guest
Posts: n/a
 
      10-16-2003


On 10/16/2003 11:32 AM, Chris wrote:
<snip>


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



<snip>


> main()


add an "int" return type before main().

> {
> int ch;
> int ph = "*";


ph is an int, "*" is a string of '*' followed by '\0'.

> void echo_off();


get rid of the void.

> while((ch = getchar()) != EOF)
> putchar(ph);
> void echo_on();


get rid of the void.

add a return value from main.

Regards,

Ed.

> }
>
>
> 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.....


 
Reply With Quote
 
Wolfgang Riedel
Guest
Posts: n/a
 
      10-16-2003
Chris wrote:
>
> (E-Mail Removed) (Richard Bos) wrote in message news:<(E-Mail Removed)>...
> > Christopher Benson-Manica <(E-Mail Removed)> wrote:
> >
> > > Chris <(E-Mail Removed)> spoke thus:
> > >
> > > > 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

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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Viewstate wont turn off even after setting it to off ! robert112 ASP .Net 1 04-26-2007 01:51 AM
How to turn off input echo in irb? Jacklty Lam Ruby 4 08-19-2006 10:24 AM
how to switch off an echo from cin? andwing@gmail.com C++ 4 08-15-2006 01:48 PM
Turning echo off in Java ctippur@gmail.com Java 8 07-13-2005 11:28 AM
echo echo echo craig judd Computer Support 1 09-23-2003 08:53 PM



Advertisments