Browse Source

ndk-patches: implement getpass()

android-5
Leonid Plyushch 6 years ago
committed by Fredrik Fornwall
parent
commit
5a8b31b3a6
  1. 77
      ndk-patches/unistd.h.patch

77
ndk-patches/unistd.h.patch

@ -0,0 +1,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
Loading…
Cancel
Save