blob: 22ff95eb98edf714f4859da31f61d9cc14cccff4 [file] [log] [blame]
From 4999ea8dc7d6c967bdb01484f1c9c91b1dfe09dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Degros?= <fdegros@chromium.org>
Date: Thu, 23 Jul 2020 18:15:29 +1000
Subject: [PATCH] Read password from stdin
---
src/rar2fs.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/src/rar2fs.c b/src/rar2fs.c
index 1378e41..30fea04 100644
--- a/src/rar2fs.c
+++ b/src/rar2fs.c
@@ -463,6 +463,33 @@ static wchar_t *get_password(const char *file, wchar_t *buf, size_t len)
static char *get_password(const char *file, char *buf, size_t len)
#endif
{
+#define GPASSWORD_SIZE 256
+
+ static char gpassword[GPASSWORD_SIZE];
+
+ if (!gpassword[0]) {
+ /* Read password from stdin. */
+ printf("Password?\n");
+ if (!fgets(gpassword, GPASSWORD_SIZE, stdin))
+ return NULL;
+
+ /* Remove newline at the end of password. */
+ const size_t n = strlen(gpassword);
+ if (n > 0) {
+ char *const last = &gpassword[n - 1];
+ if (*last == '\n')
+ *last = '\0';
+ }
+ }
+
+ if (!gpassword[0])
+ return NULL;
+
+ if (mbstowcs(buf, gpassword, len) < len)
+ return buf;
+
+ return NULL;
+
char *f[2] = {NULL, NULL};
int l[2] = {0, 0};
int i;
--
2.29.2.222.g5d2a92d10f8-goog