- #include <stdio.h>
- #include <term.h>
- #include <termios.h>
- #include <unistd.h>
- int getch(void)
- {
- int ch;
- struct termios buf;
- struct termios save;
- tcgetattr(0, &save);
- buf = save;
- buf.c_lflag &= ~(ICANON|ECHO);
- buf.c_cc[VMIN] = 1;
- buf.c_cc[VTIME] = 0;
- tcsetattr(0, TCSAFLUSH, &buf);
- ch = getchar();
- tcsetattr(0, TCSAFLUSH, &save);
- return ch;
- }
- int main(void)
- {
- int ch;
- for(; !(ch=='\n');){
- ch = getch();
- printf("%d \n", ch);
- }
- return 0;
- }
터미널로부터 리턴키가 입력될때까지 자료를 입력받는 예제
위의 예제를 보다 발전(?)시키기 위해서 좀더 많은 자료가 필요하다면, 아래의 링크를 참조하기 바란다. 터미널을 제어하는 방법에 대한 내용이 자세하게 정리되어 있는 페이지이다.
링크 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/termios
◆ 추가로 터미널 입력시 특수문자를 요약한 Table
Character | Description | c_ccsubscript | Enabled by | Typical value | POSIX.1 | FreeBSD 5.2.1 | Linux 2.4.22 | Mac OS X 10.3 | Solaris 9 | |
---|---|---|---|---|---|---|---|---|---|---|
field | flag | |||||||||
CR | carriage return | (can't change) | c_lflag | ICANON | \r | • | • | • | • | • |
DISCARD | discard output | VDISCARD | c_lflag | IEXTEN | ^O | • | • | • | • | |
DSUSP | delayed suspend (SIGTSTP) | VDSUSP | c_lflag | ISIG | ^Y | • | • | • | ||
EOF | end of file | VEOF | c_lflag | ICANON | ^D | • | • | • | • | • |
EOL | end of line | VEOL | c_lflag | ICANON | • | • | • | • | • | |
EOL2 | alternate end of line | VEOL2 | c_lflag | ICANON | • | • | • | • | ||
ERASE | backspace one character | VERASE | c_lflag | ICANON | ^H, ^? | • | • | • | • | • |
ERASE2 | alternate backspace character | VERASE2 | c_lflag | ICANON | ^H, ^? | • | ||||
INTR | interrupt signal (SIGINT) | VINTR | c_lflag | ISIG | ^?, ^C | • | • | • | • | • |
KILL | erase line | VKILL | c_lflag | ICANON | ^U | • | • | • | • | • |
LNEXT | literal next | VLNEXT | c_lflag | IEXTEN | ^V | • | • | • | • | |
NL | line feed (newline) | (can't change) | c_lflag | ICANON | \n | • | • | • | • | • |
QUIT | quit signal (SIGQUIT) | VQUIT | c_lflag | ISIG | ^\ | • | • | • | • | • |
REPRINT | reprint all input | VREPRINT | c_lflag | ICANON | ^R | • | • | • | • | |
START | resume output | VSTART | c_iflag | IXON/IXOFF | ^Q | • | • | • | • | • |
STATUS | status request | VSTATUS | c_lflag | ICANON | ^T | • | • | |||
STOP | stop output | VSTOP | c_iflag | IXON/IXOFF | ^S | • | • | • | • | • |
SUSP | suspend signal (SIGTSTP) | VSUSP | c_lflag | ISIG | ^Z | • | • | • | • | • |
WERASE | backspace one word | VWERASE | c_lflag | ICANON | ^W | • | • | • | • |