util: move read_password method out of qemu-img into osdep/oslib

The qemu-img.c file has a read_password() method impl that is
used to prompt for passwords on the console, with impls for
POSIX and Windows. This will be needed by qemu-io.c too, so
move it into the QEMU osdep/oslib files where it can be shared
without code duplication

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Daniel P. Berrange 2015-05-12 17:09:19 +01:00 committed by Kevin Wolf
parent 8336aafae1
commit d57e4e482e
4 changed files with 93 additions and 92 deletions

View file

@ -470,3 +470,27 @@ void os_mem_prealloc(int fd, char *area, size_t memory)
memset(area + pagesize * i, 0, 1);
}
}
/* XXX: put correct support for win32 */
int qemu_read_password(char *buf, int buf_size)
{
int c, i;
printf("Password: ");
fflush(stdout);
i = 0;
for (;;) {
c = getchar();
if (c < 0) {
buf[i] = '\0';
return -1;
} else if (c == '\n') {
break;
} else if (i < (buf_size - 1)) {
buf[i++] = c;
}
}
buf[i] = '\0';
return 0;
}