-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtty.c
113 lines (93 loc) · 2.01 KB
/
tty.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-99,2003,2010 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#ifndef __DJGPP__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#else
#include <bios.h>
#endif
#if !defined(O_NONBLOCK) && defined(O_NDELAY)
#define O_NONBLOCK O_NDELAY
#endif
#ifdef __CYGWIN__
#include <string.h>
#include <sys/socket.h>
#endif
#include "tty.h"
#ifndef __DJGPP__
static int tty_fd = -1;
static struct termios saved_ti;
#endif
void tty_init(int stdin_mode)
{
#ifndef __DJGPP__
int fd;
struct termios ti;
if (tty_fd >= 0) return;
/*
* If we're in "--stdin" mode (reading candidate passwords from stdin), then
* only initialize the tty if stdin is not a tty. Otherwise it could be the
* same tty, in which case we'd interfere with the user's ability to type
* candidate passwords directly to John.
*/
if (stdin_mode && !tcgetattr(0, &ti))
return;
if ((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
if (tcgetpgrp(fd) != getpid()) {
close(fd); return;
}
tcgetattr(fd, &ti);
saved_ti = ti;
ti.c_lflag &= ~(ICANON | ECHO);
ti.c_cc[VMIN] = 1;
ti.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &ti);
tty_fd = fd;
atexit(tty_done);
#endif
}
int tty_getchar(void)
{
#ifndef __DJGPP__
int c;
#ifdef __CYGWIN__
fd_set set;
struct timeval tv;
#endif
if (tty_fd >= 0) {
#ifdef __CYGWIN__
FD_ZERO(&set); FD_SET(tty_fd, &set);
tv.tv_sec = 0; tv.tv_usec = 0;
if (select(tty_fd + 1, &set, NULL, NULL, &tv) <= 0)
return -1;
#endif
c = 0;
if (read(tty_fd, &c, 1) > 0) return c;
}
#else
if (_bios_keybrd(_KEYBRD_READY))
return _bios_keybrd(_KEYBRD_READ);
#endif
return -1;
}
void tty_done(void)
{
#ifndef __DJGPP__
int fd;
if (tty_fd < 0) return;
fd = tty_fd; tty_fd = -1;
tcsetattr(fd, TCSANOW, &saved_ti);
close(fd);
#endif
}