如何使用 getpass() 和 poll() 来设置时间限制?
How to use getpass() with poll() to set time limit?
我正在使用 poll()
和 getpass()
来在有限的时间内获得用户的意见。它可以工作,但它不会显示 getpass()
中给出的 message
,而是在按下 enter key
之前不会显示 message
。怎么把这两个函数一起使用,让getpass()
中给出的message
不需要输入enter key
就可以显示出来,而且输入密码的时间会被限制?
我试图通过清除 stdin
和 stdout
来解决这个问题,但没有成功。
#include <poll.h>
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
if( poll(&mypoll, 1, 20000) )
{
char *pass = getpass("\nPlease enter password:");
}
getpass 函数已过时。不要使用它。
这是工作示例。程序等待 20 秒。如果用户在 20 秒内输入密码,则程序会读取密码信息,否则会通知用户输入超时 password.The 以下示例不会关闭回显。
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
int main()
{
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
char password[100];
printf("Please enter password\n");
if( poll(&mypoll, 1, 20000) )
{
scanf("%99s", password);
printf("password - %s\n", password);
}
else
{
puts("Time Up");
}
return 0;
}
以下示例将关闭回显。与 getpass 一样工作。这适用于 linux/macosx,windows 版本应使用 Get/Set ConsoleMode
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
int main()
{
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
char password[100];
struct termios oflags, nflags;
/* disabling echo */
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
printf("Please enter password\n");
if( poll(&mypoll, 1, 20000) )
{
scanf("%s", password);
printf("password - %s\n", password);
}
else
{
puts("Time Up");
}
/* restore terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
return 0;
}
我正在使用 poll()
和 getpass()
来在有限的时间内获得用户的意见。它可以工作,但它不会显示 getpass()
中给出的 message
,而是在按下 enter key
之前不会显示 message
。怎么把这两个函数一起使用,让getpass()
中给出的message
不需要输入enter key
就可以显示出来,而且输入密码的时间会被限制?
我试图通过清除 stdin
和 stdout
来解决这个问题,但没有成功。
#include <poll.h>
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
if( poll(&mypoll, 1, 20000) )
{
char *pass = getpass("\nPlease enter password:");
}
getpass 函数已过时。不要使用它。
这是工作示例。程序等待 20 秒。如果用户在 20 秒内输入密码,则程序会读取密码信息,否则会通知用户输入超时 password.The 以下示例不会关闭回显。
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
int main()
{
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
char password[100];
printf("Please enter password\n");
if( poll(&mypoll, 1, 20000) )
{
scanf("%99s", password);
printf("password - %s\n", password);
}
else
{
puts("Time Up");
}
return 0;
}
以下示例将关闭回显。与 getpass 一样工作。这适用于 linux/macosx,windows 版本应使用 Get/Set ConsoleMode
#include <unistd.h>
#include <poll.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
int main()
{
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
char password[100];
struct termios oflags, nflags;
/* disabling echo */
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
printf("Please enter password\n");
if( poll(&mypoll, 1, 20000) )
{
scanf("%s", password);
printf("password - %s\n", password);
}
else
{
puts("Time Up");
}
/* restore terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
return 0;
}