diff options
Diffstat (limited to 'other/dirthy')
| -rw-r--r-- | other/dirthy/dirthy.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/other/dirthy/dirthy.c b/other/dirthy/dirthy.c new file mode 100644 index 0000000..05b8caa --- /dev/null +++ b/other/dirthy/dirthy.c | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | /* writeonly redo if dirtyh.c.. hmm, still ugly, but cbreak mode - typo/teso */ | ||
| 2 | |||
| 3 | #include <sys/ioctl.h> | ||
| 4 | #include <sys/types.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdarg.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | #include <signal.h> | ||
| 10 | #include <fcntl.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <unistd.h> | ||
| 13 | #include <termios.h> | ||
| 14 | #include <getopt.h> | ||
| 15 | |||
| 16 | #define EXTERN extern | ||
| 17 | #define PRIVATE static | ||
| 18 | #define PUBLIC | ||
| 19 | |||
| 20 | PRIVATE struct termios save_termios; | ||
| 21 | PRIVATE int ttysavefd = -1; | ||
| 22 | PRIVATE enum { RESET, DIRTY } ttystate = RESET; | ||
| 23 | |||
| 24 | int tty_dirtyterm (int fd) { | ||
| 25 | struct termios buf; | ||
| 26 | |||
| 27 | if (tcgetattr(fd, &save_termios) < 0) | ||
| 28 | return(-1); | ||
| 29 | |||
| 30 | buf = save_termios; | ||
| 31 | |||
| 32 | buf.c_lflag &= ~(ECHO | ICANON); | ||
| 33 | |||
| 34 | buf.c_cc[VMIN] = 1; | ||
| 35 | buf.c_cc[VTIME] = 0; | ||
| 36 | |||
| 37 | if (tcsetattr(fd, TCSAFLUSH, &buf) <0) | ||
| 38 | return(-1); | ||
| 39 | |||
| 40 | ttystate = DIRTY; | ||
| 41 | ttysavefd = fd; | ||
| 42 | |||
| 43 | return(0); | ||
| 44 | } | ||
| 45 | |||
| 46 | int tty_reset (int fd) { | ||
| 47 | if (ttystate == RESET) | ||
| 48 | return(0); | ||
| 49 | |||
| 50 | if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) | ||
| 51 | return(-1); | ||
| 52 | |||
| 53 | ttystate = RESET; | ||
| 54 | return(0); | ||
| 55 | } | ||
| 56 | |||
| 57 | void tty_cleanup (void) { | ||
| 58 | if (ttysavefd >= 0) { | ||
| 59 | fflush(stdout); | ||
| 60 | tty_reset(ttysavefd); | ||
| 61 | } | ||
| 62 | exit(EXIT_SUCCESS); | ||
| 63 | } | ||
| 64 | |||
| 65 | void err (const int syserr, const char *msg, ...) { | ||
| 66 | va_list ap; | ||
| 67 | |||
| 68 | printf("err: "); | ||
| 69 | |||
| 70 | va_start (ap, msg); | ||
| 71 | vprintf (msg, ap); | ||
| 72 | va_end (ap); | ||
| 73 | |||
| 74 | if (syserr) | ||
| 75 | printf(": %s\n", sys_errlist[errno]); | ||
| 76 | else | ||
| 77 | printf("\n"); | ||
| 78 | |||
| 79 | tty_cleanup(); | ||
| 80 | exit(EXIT_FAILURE); | ||
| 81 | } | ||
| 82 | |||
| 83 | void sig_catch (int signo) { | ||
| 84 | printf("%s\n", sys_siglist[signo]); | ||
| 85 | tty_cleanup(); | ||
| 86 | } | ||
| 87 | |||
| 88 | int main (int argc, char **argv) { | ||
| 89 | int i, fd; | ||
| 90 | char c; | ||
| 91 | char moo[2]; /* char + \0 */ | ||
| 92 | |||
| 93 | if (argc < 2) | ||
| 94 | err(0, "usage: %s </dev/ttyXY>", argv[0]); | ||
| 95 | |||
| 96 | if (signal(SIGINT, sig_catch) == SIG_ERR) | ||
| 97 | err(1, "signal(SIGINT) error"); | ||
| 98 | if (signal(SIGQUIT, sig_catch) == SIG_ERR) | ||
| 99 | err(1, "signal(SIGQUIT) error"); | ||
| 100 | if (signal(SIGTERM, sig_catch) == SIG_ERR) | ||
| 101 | err(1, "signal(SIGTERM) error"); | ||
| 102 | |||
| 103 | if ( (fd = open(argv[1], O_WRONLY)) < 0) | ||
| 104 | err(1, "open() error"); | ||
| 105 | |||
| 106 | tty_dirtyterm(STDIN_FILENO); | ||
| 107 | |||
| 108 | while ( (i = read(STDIN_FILENO, &c, 1)) == 1) { | ||
| 109 | c &= 255; | ||
| 110 | sprintf(moo, "%c", c); | ||
| 111 | printf("%c", c); | ||
| 112 | if (c == 127) | ||
| 113 | printf("\b \b"); | ||
| 114 | fflush(stdout); | ||
| 115 | ioctl(fd, TIOCSTI, moo); | ||
| 116 | } | ||
| 117 | |||
| 118 | tty_reset(STDIN_FILENO); | ||
| 119 | if (i <= 0) | ||
| 120 | err(1, "read error"); | ||
| 121 | |||
| 122 | return(42); /* not reached */ | ||
| 123 | } | ||
