summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
index 59e37a5..c7064d9 100644
--- a/src/common.c
+++ b/src/common.c
@@ -34,9 +34,11 @@
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <ctype.h>
#ifdef WIN32
#include <windows.h>
+#include <conio.h>
#ifndef _O_EXCL
#define _O_EXCL 0x0400
#endif
@@ -46,6 +48,7 @@
#else
#include <sys/time.h>
#include <pthread.h>
+#include <termios.h>
#endif
#include "common.h"
@@ -481,3 +484,45 @@ char* strsep(char** strp, const char* delim)
return s;
}
#endif
+
+#ifdef WIN32
+#define BS_CC '\b'
+#define my_getch getch
+#else
+#define BS_CC 0x7f
+static int my_getch(void)
+{
+ struct termios oldt, newt;
+ int ch;
+ tcgetattr(STDIN_FILENO, &oldt);
+ newt = oldt;
+ newt.c_lflag &= ~(ICANON | ECHO);
+ tcsetattr(STDIN_FILENO, TCSANOW, &newt);
+ ch = getchar();
+ tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
+ return ch;
+}
+#endif
+
+void get_user_input(char *buf, int maxlen, int secure)
+{
+ int len = 0;
+ int c;
+
+ while ((c = my_getch())) {
+ if ((c == '\r') || (c == '\n')) {
+ break;
+ } else if (isprint(c)) {
+ if (len < maxlen-1)
+ buf[len++] = c;
+ fputc((secure) ? '*' : c, stdout);
+ } else if (c == BS_CC) {
+ if (len > 0) {
+ fputs("\b \b", stdout);
+ len--;
+ }
+ }
+ }
+ fputs("\n", stdout);
+ buf[len] = 0;
+}