diff options
Diffstat (limited to '')
-rw-r--r-- | common/cli.c | 58 | ||||
-rw-r--r-- | common/cli.h | 9 |
2 files changed, 67 insertions, 0 deletions
diff --git a/common/cli.c b/common/cli.c new file mode 100644 index 0000000..fb4ac1f --- /dev/null +++ b/common/cli.c @@ -0,0 +1,58 @@ +#include <stdlib.h> +#include <stdio.h> +#include <termios.h> + +#include "cli.h" + +char * login = NULL; +char * password = NULL; + +void common_cli_free(void) +{ + free(login); + login = NULL; + free(password); + password = NULL; +} + +const char* common_cli_get_login(void) +{ + while (1) { + size_t len = 0; + printf("Login: "); + ssize_t read = getline(&login, &len, stdin); + if (read > 1) { + login[read-1] = 0; + return login; + } + } +} + +const char* common_cli_get_password(void) +{ + struct termios oflags, nflags; + tcgetattr(fileno(stdin), &oflags); + nflags = oflags; + nflags.c_lflag &= ~ECHO; + nflags.c_lflag |= ECHONL; + + if (tcsetattr(fileno(stdin), TCSADRAIN, &nflags) != 0) { + perror("tcsetattr"); + exit(998); + } + + while (1) { + size_t len = 0; + printf("Password: "); + size_t read = getline(&password, &len, stdin); + if (read > 1) { + password[read-1] = 0; + break; + } + } + if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { + perror("tcsetattr 2"); + exit(998); + } + return password; +} diff --git a/common/cli.h b/common/cli.h new file mode 100644 index 0000000..51f014b --- /dev/null +++ b/common/cli.h @@ -0,0 +1,9 @@ +#ifndef COMMON_CLI_H_ +#define COMMON_CLI_H_ + +void common_cli_free(void); +const char* common_cli_get_login(void); +const char* common_cli_get_password(void); + +#endif + |