forked from termux/termux-packages
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunistd.h.patch
77 lines (75 loc) · 2.21 KB
/
unistd.h.patch
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
--- sysroot/usr/include/unistd.h 2018-04-30 19:24:22.000000000 +0000
+++ usr/include/unistd.h 2018-08-16 10:42:51.158596753 +0000
@@ -302,6 +302,74 @@
#include <bits/fortify/unistd.h>
#endif
+#if !defined GETPASS_H && !defined getpass && !defined HAVE_GETPASS && !defined HAS_GETPASS && !defined NO_INLINE_GETPASS
+#define GETPASS_H 1
+#define HAVE_GETPASS 1
+#define HAS_GETPASS 1
+#define PASSWORDLEN 512
+
+static __inline__ char* getpass(const char* prompt) {
+ // termios struct as in asm-generic/termbits.h
+ struct _termios {
+ unsigned int c_iflag; /* input mode flags */
+ unsigned int c_oflag; /* output mode flags */
+ unsigned int c_cflag; /* control mode flags */
+ unsigned int c_lflag; /* local mode flags */
+ unsigned char c_line; /* line discipline */
+ unsigned char c_cc[19/* NCCS */]; /* control characters */
+ };
+
+ struct _termios term_old, term_new;
+ static char password[513] = { 0 }; /* 512 1-byte charactes and '0' */
+ int len = 0, tty_changed = 0;
+
+ // print prompt
+ while (*prompt) {
+ write(1, prompt, 1);
+ prompt++;
+ }
+
+ // try to disable echoing on terminal
+ if (ioctl(0, 0x5401 /* TCGETS */, &term_old) == 0) {
+ term_new = term_old;
+ term_new.c_lflag &= ~0000010;/* ~ECHO */
+
+ if (ioctl(0, 0x5402+0 /* TCSETS+TCSANOW */, &term_new) == 0) {
+ tty_changed = 1;
+ } else {
+ tty_changed = 0;
+ }
+ }
+
+ // read password
+ char chr;
+ while (read(0, &chr, sizeof(char)) > 0) {
+ if (chr == '\r' || chr == '\n' || chr == 0) {
+ break;
+ }
+
+ if (len == sizeof(password)-1) {
+ // we should consume all entered characters even
+ // if maximal input length reached
+ continue;
+ } else {
+ password[len++] = chr;
+ }
+ }
+ password[len] = 0;
+
+ // restore terminal to previous state if needed
+ if (tty_changed) {
+ ioctl(0, 0x5402+0 /* TCSETS+TCSANOW */, &term_old);
+ }
+
+ // force new line
+ write(1, "\n", 1);
+
+ return password;
+}
+#endif
+
__END_DECLS
#endif