在用户输入密码时回显号

来源:互联网 发布:lantern for mac 2016 编辑:程序博客网 时间:2024/05/19 03:45

在用户输入密码时回显号,主要用到了linux的终端控制技术。

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
      
/* Use this variable to remember original terminal attributes. */
      
struct termios saved_attributes;
      
    void
reset_input_mode (void)
{
    tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}
      
    void
set_input_mode (void)
{
    struct termios tattr;
    char *name;
      
    /* Make sure stdin is a terminal. */
    if (!isatty (STDIN_FILENO))
    {
        fprintf (stderr, "Not a terminal./n");
        exit (EXIT_FAILURE);
    }
      
    /* Save the terminal attributes so we can restore them later. */
    tcgetattr (STDIN_FILENO, &saved_attributes);
    atexit (reset_input_mode);
      
    /* Set the funny terminal modes. */
    tcgetattr (STDIN_FILENO, &tattr);
    tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
    tattr.c_cc[VMIN] = 1;
    tattr.c_cc[VTIME] = 0;
    tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}
      
    int
main (void)
{
    char c;
      
    set_input_mode ();
      
    while (1)
    {
        read (STDIN_FILENO, &c, 1);
        if (c == EOF || c == '/n')          /* C-d */
            break;
        else
        {
            putchar('*');
            fflush(NULL);
        }
    }
      
    return EXIT_SUCCESS;
}