forked from xiajian/apue3e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_raw.c
49 lines (44 loc) · 1.08 KB
/
t_raw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "apue.h"
static void
sig_catch(int signo)
{
printf("signal caught\n");
tty_reset(STDIN_FILENO);
exit(0);
}
int
main(void)
{
int i;
char c;
if (signal(SIGINT, sig_catch) == SIG_ERR) /* catch signals */
err_sys("signal(SIGINT) error");
if (signal(SIGQUIT, sig_catch) == SIG_ERR)
err_sys("signal(SIGQUIT) error");
if (signal(SIGTERM, sig_catch) == SIG_ERR)
err_sys("signal(SIGTERM) error");
if (tty_raw(STDIN_FILENO) < 0)
err_sys("tty_raw error");
printf("Enter raw mode characters, terminate with DELETE\n");
while ((i = read(STDIN_FILENO, &c, 1)) == 1) {
if ((c &= 255) == 0177) /* 0177 = ASCII DELETE */
break;
printf("%o\n", c);
}
if (tty_reset(STDIN_FILENO) < 0)
err_sys("tty_reset error");
if (i <= 0)
err_sys("read error");
if (tty_cbreak(STDIN_FILENO) < 0)
err_sys("tty_cbreak error");
printf("\nEnter cbreak mode characters, terminate with SIGINT\n");
while ((i = read(STDIN_FILENO, &c, 1)) == 1) {
c &= 255;
printf("%o\n", c);
}
if (tty_reset(STDIN_FILENO) < 0)
err_sys("tty_reset error");
if (i <= 0)
err_sys("read error");
exit(0);
}