- 论坛徽章:
- 0
|
本帖最后由 sernix 于 2012-04-01 17:35 编辑
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <termios.h>
- #include <errno.h>
- static int __getch(int *_ch)
- {
- int ret, ch;
- struct termios old, new;
- ret = tcgetattr(STDIN_FILENO, &old);
- if (ret < 0) {
- ret = errno;
- goto err_ret;
- }
- memcpy(&new, &old, sizeof(struct termios));
- new.c_lflag &= ~(ICANON);
- ret = tcsetattr(STDIN_FILENO, TCSANOW, &new);
- if (ret < 0) {
- ret = errno;
- goto err_ret;
- }
- ch = getchar();
- ret = tcsetattr(STDIN_FILENO, TCSANOW, &old);
- if (ret < 0) {
- ret = errno;
- goto err_ret;
- }
- *_ch = ch;
- return 0;
- err_ret:
- return ret;
- }
- int main(int argc, char *argv[])
- {
- int ret, ch;
- do {
- (void) __getch(&ch);
- putchar(ch);
- } while (ch != '\n');
- return 0;
- }
复制代码 |
|