termux-packages/ndk-patches/unistd.h.patch

78 lines
2.2 KiB
Diff

--- ./usr/include/unistd.h.orig 2021-08-16 11:52:08.095542840 +0200
+++ ./usr/include/unistd.h 2021-08-16 11:52:08.565542673 +0200
@@ -375,6 +375,74 @@
#undef _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
#include <android/legacy_unistd_inlines.h>