Simple login program password does not echo under Linux terminal

Source: Internet
Author: User

Login on Linux is the input password will not be echoed, so I also wrote a simple login program, so that when entering the password is no longer echo.

#include <stdio.h>#include<stdlib.h>#include<termios.h>#include<unistd.h>#include<errno.h>#include<string.h>#include<stdbool.h>#defineUser_name "username"#defineUser_pass "Userpass"#defineMAX 20#defineEchoflags (ECHO | Echoe | Echok)intSet_disp_mode (intFdintoption) {    interr; structTermios term; if(Tcgetattr (fd, &term) = =-1) {fprintf (stderr,"can\ ' t get the attribution of the terminal!\n"); Exit (1); }    if(option) {Term.c_lflag|=Echoflags; }    Else{Term.c_lflag&= ~Echoflags; } Err= Tcsetattr (FD, Tcsaflush, &term ); if(Err = =-1&& Err = =eintr) {fprintf (stderr,"can\ ' t set the attribution of the terminal!\n"); Exit (1); }    return  0;}intGET_PASSWD (Char* passwd,intsize) {    intC; intn =0; fprintf (stderr,"Please input password:");  Do{C=GetChar (); if(c! ='\ n'&& C! ='\ r') {Passwd[n++] =C; }    }  while(c! ='\ n'&& C! ='\ r'&& N < (size-1)); Passwd[n]=' /'; returnN;}BOOLCheck_login (Const Char* Name,Const Char*passwd) {    intNlen =strlen (user_name); intPlen =strlen (User_pass); return(STRNCMP (name, user_name, nlen) = =0) &&(strncmp (passwd, User_pass, Plen)==0);}intMainvoid) {    CharName[max]; CharPasswd[max]; login:fprintf (stderr,"Login User:");    Fgets (name, MAX, stdin); Name[strlen (name)-1] =' /'; Set_disp_mode (Stdin_fileno,0);    GET_PASSWD (passwd, MAX); if(Check_login (name, passwd)) {fprintf (stderr,"\nhello%s, welcome!\n", name); }    Else{fprintf (stderr,"\ n"); Set_disp_mode (Stdin_fileno,1); GotoLOGIN; } set_disp_mode (Stdin_fileno,1); return  0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.