blob: 2637f1d6001d7483c6e043bf07956c4bcb4a5620 [file] [log] [blame]
From 498e195ca77cd43d2dc281aae6219ff2b8e8b35f 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 1e6f23b..f28493d 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.30.0.284.gd98b1dd5eaa7-goog