diff options
author | Rob Landley <rob@landley.net> | 2020-09-23 09:47:56 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2020-09-23 09:47:56 -0500 |
commit | 8079673ef5d0691f7ccd4268cfd8fd3d8de44471 (patch) | |
tree | 78dad7176479cbdf47bfec64088b045ca7597ba7 /lib | |
parent | 9cce74afd2561663bceab9a5ce609109b18aca57 (diff) | |
download | toybox-8079673ef5d0691f7ccd4268cfd8fd3d8de44471.tar.gz |
Have read_password() read from tty device, handle ctrl-c and ctrl-d.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/password.c | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/lib/password.c b/lib/password.c index ab240aed..3a39d9e6 100644 --- a/lib/password.c +++ b/lib/password.c @@ -51,7 +51,7 @@ int read_password(char *buf, int buflen, char *mesg) { struct termios oldtermio; struct sigaction sa, oldsa; - int i, ret = 1; + int i, tty = tty_fd(), ret = 1; // NOP signal handler to return from the read. Use sigaction() instead // of xsignal() because we want to restore the old handler afterwards. @@ -59,13 +59,12 @@ int read_password(char *buf, int buflen, char *mesg) sa.sa_handler = generic_signal; sigaction(SIGINT, &sa, &oldsa); - tcflush(0, TCIFLUSH); - xset_terminal(0, 1, 0, &oldtermio); + tcflush(tty, TCIFLUSH); + xset_terminal(tty, 1, 0, &oldtermio); + dprintf(tty, "%s", mesg); - dprintf(1, "%s", mesg); - - for (i=0; i < buflen-1; i++) { - if ((ret = read(0, buf+i, 1)) < 0 || (!ret && !i)) { + for (i = 0; i<buflen-1; i++) { + if ((ret = read(tty, buf+i, 1))<0 || (!ret&&!i) || *buf==4 || buf[i]==3) { i = 0; ret = 1; @@ -74,11 +73,11 @@ int read_password(char *buf, int buflen, char *mesg) ret = 0; break; - } else if (buf[i] == 8 || buf[i] == 127) i -= i ? 2 : 1; + } else if (buf[i] == 8 || buf[i] == 127) i -= 2-!i; } // Restore terminal/signal state, terminate string - sigaction(SIGINT, &oldsa, NULL); + sigaction(SIGINT, &oldsa, 0); tcsetattr(0, TCSANOW, &oldtermio); buf[i] = 0; xputc('\n'); |