Linux API/network2009. 8. 27. 14:47
Try It Out − A Password Program with termios

1. Our password program, password.c, begins with the following definitions:
#include <termios.h>
#include <stdio.h>
#define PASSWORD_LEN 8
int main()
{
    struct termios initialrsettings, newrsettings;
    char password[PASSWORD_LEN + 1];

2. Next, add in a line to get the current settings from the standard input and copy them into the termios
structure that we created above.
    tcgetattr(fileno(stdin), &initialrsettings);

3. Make a copy of the original settings to replace them at the end. Turn off the ECHO flag on the
newrsettings and ask the user for their password:
    newrsettings = initialrsettings;
    newrsettings.c_lflag &= ~ECHO;
    printf("Enter password: ");

4. Next, set the terminal attributes to newrsettings and read in the password. Lastly, reset the terminal
attributes to their original setting and print the password to render all the previous effort useless.
    if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) {
        fprintf(stderr,"Could not set attributes\n");
    }
    else {
        fgets(password, PASSWORD_LEN, stdin);
        tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
        fprintf(stdout, "\nYou entered %s\n", password);
    }
    exit(0);
}

[링크 : http://people.freedesktop.org/~nagappan/beginning-linux-programming.pdf]

[링크 : http://ttongfly.net/zbxe/?document_srl=45222]
ttongfly.net 의 내용은 아마도 위의 beginning linux programming의 내용을 번역한 것으로 보인다.

위와 같은 작동은 간단하게 쉘에서
"stty -echo" << echo 끔
"stty echo" << echo 켬
로 가능하다.

하지만, 프로그램에서 쉘 호출 해봤자, 자기 자신에게 적용이 안되므로
termios를 이용해서 직접 제어를 해야 하는데, termios는 말그대로 TERMinal IO Stucture 이다.
소스에서 include 할녀석은 <termios.h> 이고 이녀석은 <bits/termios.h> 을 물어온다.

$ cat /usr/include/bits/termios.h
...
struct termios
  {
    tcflag_t c_iflag;           /* input mode flags */
    tcflag_t c_oflag;           /* output mode flags */
    tcflag_t c_cflag;           /* control mode flags */
    tcflag_t c_lflag;           /* local mode flags */
    cc_t c_line;                        /* line discipline */
    cc_t c_cc[NCCS];            /* control characters */
    speed_t c_ispeed;           /* input speed */
    speed_t c_ospeed;           /* output speed */
#define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
#define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
  };
...
/* c_lflag bits */
#define ISIG    0000001
#define ICANON  0000002
#if defined __USE_MISC || defined __USE_XOPEN
    # define XCASE  0000004
#endif
#define ECHO    0000010
#define ECHOE   0000020
#define ECHOK   0000040
#define ECHONL  0000100
#define NOFLSH  0000200
#define TOSTOP  0000400
#ifdef __USE_MISC
    # define ECHOCTL 0001000
    # define ECHOPRT 0002000
    # define ECHOKE  0004000
    # define FLUSHO  0010000
    # define PENDIN  0040000
#endif
#define IEXTEN  0100000

'Linux API > network' 카테고리의 다른 글

netstat 에서 0.0.0.0의 의미  (2) 2009.12.07
ioctl을 이용한 정보수집  (0) 2009.11.30
canonical / non-canonical  (0) 2009.08.27
struct in_addr  (0) 2009.08.18
ifup/ifdown  (0) 2009.08.18
Posted by 구차니