Newbie Turn off Echo in C

Discussion in 'C Programming' started by Chris, Oct 13, 2003.

  1. Chris

    Chris Guest

    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.....
     
    Chris, Oct 13, 2003
    #1
    1. Advertising

  2. Chris <> scribbled the following:
    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I wish someone we knew would die so we could leave them flowers."
    - A 6-year-old girl, upon seeing flowers in a cemetery
     
    Joona I Palaste, Oct 13, 2003
    #2
    1. Advertising

  3. Chris

    Chris Guest

    Joona I Palaste <> wrote in message news:<bmejpt$2dg$>...
    > Chris <> scribbled the following:
    > > 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.



    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;
    }
     
    Chris, Oct 14, 2003
    #3
  4. Chris

    Richard Bos Guest

    (Chris) wrote:

    > Joona I Palaste <> wrote in message news:<bmejpt$2dg$>...
    > > 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;
    > }


    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
     
    Richard Bos, Oct 14, 2003
    #4
  5. On 14 Oct 2003 08:23:11 -0700, in comp.lang.c ,
    (Chris) wrote:

    >>
    >> Short answer:
    >> You can't.
    >>

    > 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

    >return;


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


    ok, this line is C.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #5
  6. Chris

    Dan Pop Guest

    In <> (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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 14, 2003
    #6
  7. Chris

    Richard Bos Guest

    Mark McIntyre <> wrote:

    > On 14 Oct 2003 08:23:11 -0700, in comp.lang.c ,
    > (Chris) wrote:
    >
    > >void echo_off(void)


    > >return;

    >
    > 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
     
    Richard Bos, Oct 14, 2003
    #7
  8. On Tue, 14 Oct 2003 15:59:39 GMT, in comp.lang.c ,
    (Richard Bos) wrote:

    >Mark McIntyre <> wrote:
    >
    >> On 14 Oct 2003 08:23:11 -0700, in comp.lang.c ,
    >> (Chris) wrote:
    >>
    >> >void echo_off(void)

    >
    >> >return;

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


    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.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #8
  9. Chris

    Richard Bos Guest

    Mark McIntyre <> wrote:

    > On Tue, 14 Oct 2003 15:59:39 GMT, in comp.lang.c ,
    > (Richard Bos) wrote:
    >
    > >Mark McIntyre <> wrote:
    > >
    > >> On 14 Oct 2003 08:23:11 -0700, in comp.lang.c ,
    > >> (Chris) wrote:
    > >>
    > >> >void echo_off(void)

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

    >
    > Thats what I meant. The return is useless in this example.


    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.

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


    Vade retro, Niklaus Wirth!

    Richard
     
    Richard Bos, Oct 15, 2003
    #9
  10. Chris

    Chris Guest

    (Dan Pop) wrote in message news:<bmh6b5$srg$>...
    > In <> (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.
     
    Chris, Oct 15, 2003
    #10
  11. Chris <> scribbled the following:
    > (Dan Pop) wrote in message news:<bmh6b5$srg$>...
    >> In <> (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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
    - Dr. Michael Hfuhruhurr
     
    Joona I Palaste, Oct 15, 2003
    #11
  12. 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
     
    Arthur J. O'Dwyer, Oct 15, 2003
    #12
  13. Chris

    Richard Bos Guest

    (Chris) wrote:

    > > In <> (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
     
    Richard Bos, Oct 15, 2003
    #13
  14. Chris <> 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.
     
    Christopher Benson-Manica, Oct 15, 2003
    #14
  15. Chris

    Dan Pop Guest

    In <bmjobn$991$> Christopher Benson-Manica <> writes:

    >Chris <> 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:
     
    Dan Pop, Oct 15, 2003
    #15
  16. Dan Pop <> 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.
     
    Christopher Benson-Manica, Oct 15, 2003
    #16
  17. Chris

    Richard Bos Guest

    Christopher Benson-Manica <> wrote:

    > Chris <> 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
     
    Richard Bos, Oct 16, 2003
    #17
  18. Chris

    Chris Guest

    (Richard Bos) wrote in message news:<>...
    > Christopher Benson-Manica <> wrote:
    >
    > > Chris <> 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.....
     
    Chris, Oct 16, 2003
    #18
  19. Chris

    Ed Morton Guest

    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.....
     
    Ed Morton, Oct 16, 2003
    #19
  20. Chris wrote:
    >
    > (Richard Bos) wrote in message news:<>...
    > > Christopher Benson-Manica <> wrote:
    > >
    > > > Chris <> 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
     
    Wolfgang Riedel, Oct 16, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    458
    Cheung, Jeffrey Jing-Yen
    Jul 8, 2003
  2. Replies:
    8
    Views:
    6,808
    Gordon Beaton
    Jul 13, 2005
  3. Replies:
    4
    Views:
    310
    osmium
    Aug 15, 2006
  4. robert112
    Replies:
    1
    Views:
    443
    Juan T. Llibre
    Apr 26, 2007
  5. Jacklty Lam

    How to turn off input echo in irb?

    Jacklty Lam, Aug 18, 2006, in forum: Ruby
    Replies:
    4
    Views:
    199
    Thaddeus L Olczyk
    Aug 19, 2006
Loading...

Share This Page