C program to achieve password secret input instances of linux system can be executed

  • 2020-06-01 10:24:34
  • OfStack

Read and write user input, screen does not echo

char *getpass( const char *prompt);

getpass is used to read user input from the keyboard, but the screen does not echo.

The parameter prompt is the screen prompt character.

The function returns a string entered by the user's keyboard.

Screen non-echo refers to the content entered by the user without any prompt message being displayed, which means it is not practical to enter the password when switching users in Linux.

The procedure is as follows:

#include <stdio.h> 
 #include <unistd.h> 
 int main(int argc, char *args[]) 
  //  call getpass function  
  //  The argument to the function is the prompt  
  //  The return value of the function is what the user entered  
  char *password = getpass("Input your password : "); 
  //  Output the information entered by the user  
  printf("password = %s\n", password); 
  return 0; 

Compile and execute the program:

[negivup@negivup mycode]$ gcc -o main main.c 
[negivup@negivup mycode]$ ./main 
Input your password :   ------------ The input will not echo back here  
password = 123456 

Related articles: