diff options
author | Yonghee Han <onstudy@samsung.com> | 2016-07-27 16:40:17 +0900 |
---|---|---|
committer | Yonghee Han <onstudy@samsung.com> | 2016-07-27 00:53:56 -0700 |
commit | 3158f4a51894e46ecb593bffbfd12824e1d6534a (patch) | |
tree | 2bef7f0238e687c5de65f48b5995ee124a95d157 /util/oslib-posix.c | |
parent | a3b133b0ea0696e42fd876b9a803e28bc6ef5299 (diff) | |
download | qemu-upstream/2.4.1.tar.gz qemu-upstream/2.4.1.tar.bz2 qemu-upstream/2.4.1.zip |
Imported Upstream version 2.4.1upstream/2.4.1
Change-Id: I0b584f569cb0e0f4eac13cdb79e110c2dbc34bfc
Diffstat (limited to 'util/oslib-posix.c')
-rw-r--r-- | util/oslib-posix.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 37ffd9624..3ae4987b6 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -50,6 +50,7 @@ extern int daemon(int, int); #include <termios.h> #include <unistd.h> +#include <termios.h> #include <glib/gprintf.h> @@ -415,3 +416,69 @@ void os_mem_prealloc(int fd, char *area, size_t memory) pthread_sigmask(SIG_SETMASK, &oldset, NULL); } } + + +static struct termios oldtty; + +static void term_exit(void) +{ + tcsetattr(0, TCSANOW, &oldtty); +} + +static void term_init(void) +{ + struct termios tty; + + tcgetattr(0, &tty); + oldtty = tty; + + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP + |INLCR|IGNCR|ICRNL|IXON); + tty.c_oflag |= OPOST; + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); + tty.c_cflag &= ~(CSIZE|PARENB); + tty.c_cflag |= CS8; + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 0; + + tcsetattr(0, TCSANOW, &tty); + + atexit(term_exit); +} + +int qemu_read_password(char *buf, int buf_size) +{ + uint8_t ch; + int i, ret; + + printf("password: "); + fflush(stdout); + term_init(); + i = 0; + for (;;) { + ret = read(0, &ch, 1); + if (ret == -1) { + if (errno == EAGAIN || errno == EINTR) { + continue; + } else { + break; + } + } else if (ret == 0) { + ret = -1; + break; + } else { + if (ch == '\r' || + ch == '\n') { + ret = 0; + break; + } + if (i < (buf_size - 1)) { + buf[i++] = ch; + } + } + } + term_exit(); + buf[i] = '\0'; + printf("\n"); + return ret; +} |