summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/src/tty.c
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/b-scan/tmp/src/tty.c
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/b-scan/tmp/src/tty.c')
-rw-r--r--other/b-scan/tmp/src/tty.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/other/b-scan/tmp/src/tty.c b/other/b-scan/tmp/src/tty.c
new file mode 100644
index 0000000..5c99b7f
--- /dev/null
+++ b/other/b-scan/tmp/src/tty.c
@@ -0,0 +1,116 @@
1/*
2 * most of this stuff is ripped from solar's excelent john-1.6 source
3 */
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7
8#include <unistd.h>
9#include <stdlib.h>
10#include <termios.h>
11
12static int tty_fd = 0;
13static int tty_buf = -1;
14static struct termios saved_ti;
15
16
17/*
18 * Reads a character, returns -1 if no data available or on error.
19 */
20int
21tty_getchar ()
22{
23 int c;
24
25 /*
26 * process buffer first
27 */
28 if (tty_buf != -1)
29 {
30 c = tty_buf;
31 tty_buf = -1;
32 return (c);
33 }
34
35 if (tty_fd)
36 {
37 c = 0;
38 if (read (tty_fd, &c, 1) > 0)
39 return c;
40 }
41
42 return (-1);
43}
44
45/*
46 * check if someone pressed a key
47 * Actually we do a read on the fd and store the result in a buffer
48 * todo: check with ioctl if data is pending
49 * return 1 is data is pending, 0 if not
50 */
51int
52tty_ischar ()
53{
54 if (tty_buf != -1)
55 return (1);
56
57 if ((tty_buf = tty_getchar ()) != -1)
58 return (1);
59
60 return (0);
61}
62
63
64/*
65 * Restores the terminal parameters and closes the file descriptor.
66 */
67void
68tty_done ()
69{
70 int fd;
71
72 if (!tty_fd)
73 return;
74
75 fd = tty_fd;
76 tty_fd = 0;
77 tcsetattr (fd, TCSANOW, &saved_ti);
78
79 close (fd);
80}
81
82
83/*
84 * Initializes the terminal for unbuffered non-blocking input. Also registers
85 * tty_done() via atexit().
86 */
87void
88tty_init ()
89{
90 int fd;
91 struct termios ti;
92
93 if (tty_fd)
94 return;
95
96 if ((fd = open ("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0)
97 return;
98
99 if (tcgetpgrp (fd) != getpid ())
100 {
101 close (fd);
102 return;
103 }
104
105 tcgetattr (fd, &ti);
106 saved_ti = ti;
107 ti.c_lflag &= ~(ICANON | ECHO);
108 ti.c_cc[VINTR] = 3; /* CTRL-C is INTR */
109 ti.c_cc[VMIN] = 1;
110 ti.c_cc[VTIME] = 0;
111 tcsetattr (fd, TCSANOW, &ti);
112
113 tty_fd = fd;
114
115 atexit (tty_done);
116}