-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcharinput.c
89 lines (77 loc) · 1.92 KB
/
tcharinput.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <sys/cdefs.h>
#ifdef __linux__
# define _BSD_SOURCE /* for ECHOKE */
#endif
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
int main(void);
#ifndef TCSASOFT
# define TCSASOFT 0 /* for some non-BSD */
#endif
#ifndef CIGNORE
# define CIGNORE 0 /* for some BSDs */
#endif
int
main()
{
ssize_t ret;
unsigned char ch;
struct termios tio;
struct termios otio;
if (tcgetattr(STDIN_FILENO, &tio) < 0) {
err(1, "tcgetattr(STDIN_FILENO)");
}
otio = tio;
/* turn off canonical processing and all echoing */
tio.c_lflag &= ~(ICANON | ECHOKE | ECHOE | ECHO | ECHONL | CIGNORE);
/* wait for one character to be available */
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT, &tio) < 0) {
/* hope for the best.... */
(void) tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT, &otio);
err(1, "tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT...)");
}
setbuf(stdout, (char *) NULL);
printf("Type a single character, and see its value: ");
switch ((ret = read(STDIN_FILENO, &ch, 1))) {
case -1:
warn("read(STDIN_FILENO, &ch, 1) returned an error");
ch = (unsigned char) EOF;
break;
case 0:
warnx("read(STDIN_FILENO, &ch, 1) returned nothing");
ch = 0;
break;
case 1:
#if 0
switch ((ret = write(STDOUT_FILENO, &ch, 1))) {
case -1:
warn("write(STDOUT_FILENO, &ch, 1) returned an error");
break;
case 0:
warnx("write(STDOUT_FILENO, &ch, 1) returned zero");
break;
case 1:
/* all is well! */
break;
default:
warnx("write(STDOUT_FILENO, &ch, 1) returned too much! (%d)", ret);
break;
}
#else
printf("0x%02X, aka 0%o\n", (unsigned int) ch, (unsigned int) ch);
#endif
break;
default:
warnx("read(STDIN_FILENO, &ch, 1) returned too much! (%d)", ret);
break;
}
if (tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT, &otio) < 0) {
err(1, "tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT, &otio)");
}
exit(0);
}