Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 103 lines (85 sloc) 1.793 kb
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
1 #define _POSIX_C_SOURCE 200112L
2
b3e8467 @majnemer [answerback] further cleanups
authored
3 #include <signal.h> /* for signal handling */
4 #include <stdio.h> /* fopen(), et al. */
26a9ecd @majnemer [answerback] small cleanup, no need to rebuild
authored
5 #include <fcntl.h> /* for open() */
6 #include <unistd.h> /* for ssize_t, read(), write() */
b3e8467 @majnemer [answerback] further cleanups
authored
7 #include <stdlib.h> /* for EXIT_SUCCESS, EXIT_FAILURE */
26a9ecd @majnemer [answerback] small cleanup, no need to rebuild
authored
8 #include <termios.h> /* ctermid(), et al. */
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
9
26e4c1a @majnemer [answerback] cleanup
authored
10 #define ANSWERBACK_LEN 16
11 #define ANSWERBACK_CODE 5
12
b3e8467 @majnemer [answerback] further cleanups
authored
13 static struct termios old_term;
14 static int fd;
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
15
b3e8467 @majnemer [answerback] further cleanups
authored
16 static void
17 tty_reset(void)
18 {
19 if (tcsetattr(fd, TCSAFLUSH, &old_term) == -1)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
20 {
b3e8467 @majnemer [answerback] further cleanups
authored
21 perror("tcsetattr");
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
22 }
23
b3e8467 @majnemer [answerback] further cleanups
authored
24 if (close(fd) == -1)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
25 {
b3e8467 @majnemer [answerback] further cleanups
authored
26 perror("close");
26e4c1a @majnemer [answerback] cleanup
authored
27 }
b3e8467 @majnemer [answerback] further cleanups
authored
28 }
26e4c1a @majnemer [answerback] cleanup
authored
29
b3e8467 @majnemer [answerback] further cleanups
authored
30 int main()
31 {
0ab12b7 @majnemer [answerback] align implementation with POSIX-2008
authored
32 char term[L_ctermid];
33 const char *cterm = ctermid(term);
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
34
0ab12b7 @majnemer [answerback] align implementation with POSIX-2008
authored
35 if (cterm[0] == '\0')
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
36 {
26a9ecd @majnemer [answerback] small cleanup, no need to rebuild
authored
37 (void)fputs("Cannot get the path to the console", stderr);
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
38 return EXIT_FAILURE;
39 }
40
b3e8467 @majnemer [answerback] further cleanups
authored
41 if ((fd = open(cterm, O_RDWR)) == -1)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
42 {
b3e8467 @majnemer [answerback] further cleanups
authored
43 perror("open");
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
44 return EXIT_FAILURE;
45 }
b3e8467 @majnemer [answerback] further cleanups
authored
46
47 if (tcgetattr(fd, &old_term) == -1)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
48 {
b3e8467 @majnemer [answerback] further cleanups
authored
49 perror("tcgetattr");
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
50 return EXIT_FAILURE;
51 }
52
b3e8467 @majnemer [answerback] further cleanups
authored
53 if (atexit(tty_reset) != 0)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
54 {
26a9ecd @majnemer [answerback] small cleanup, no need to rebuild
authored
55 (void)fputs("Cannot set the exit function", stderr);
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
56 return EXIT_FAILURE;
57 }
58
59 struct termios new_term = old_term;
b3e8467 @majnemer [answerback] further cleanups
authored
60 new_term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON | ISIG | IEXTEN);
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
61 new_term.c_cc[VMIN] = 0;
16e96b6 @majnemer [answerback] decrease wait time
authored
62 new_term.c_cc[VTIME] = 1;
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
63
64 if (tcsetattr(fd, TCSAFLUSH, &new_term) == -1)
65 {
66 perror("tcsetattr");
67 return EXIT_FAILURE;
68 }
69
26e4c1a @majnemer [answerback] cleanup
authored
70 char code = ANSWERBACK_CODE;
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
71 for (;;)
72 {
26e4c1a @majnemer [answerback] cleanup
authored
73 ssize_t ret = write(fd, &code, sizeof(code));
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
74 if (ret == -1)
75 {
76 perror("write");
77 return EXIT_FAILURE;
78 }
79 else if (ret > 0)
80 {
81 break;
82 }
83 }
84
26e4c1a @majnemer [answerback] cleanup
authored
85 char buffer[ANSWERBACK_LEN] = { 0 };
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
86 ssize_t ret = read(fd, buffer, sizeof(buffer) - 1);
87 if (ret == -1)
88 {
89 perror("read");
90 return EXIT_FAILURE;
91 }
92
b3e8467 @majnemer [answerback] further cleanups
authored
93 buffer[ret] = '\0';
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
94
26e4c1a @majnemer [answerback] cleanup
authored
95 if (ret == 0)
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
96 {
26e4c1a @majnemer [answerback] cleanup
authored
97 return EXIT_FAILURE;
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
98 }
99
26e4c1a @majnemer [answerback] cleanup
authored
100 puts(buffer);
101 return EXIT_SUCCESS;
c952807 @majnemer add answerback.c, tells us if we are on putty
authored
102 }
Something went wrong with that request. Please try again.