關閉回顯,並且中斷訊號關閉,輸入密碼
阿新 • • 發佈:2019-02-07
sigprocmask只能用在單執行緒程序
- #include <stdio.h>
- #include <unistd.h>
- #include <termios.h>
- #include <error.h>
- #include "myerror.h"
- #include "file.h"
- #include <signal.h>
- #define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
- int DelayInt(sigset_t * sigold){
- sigset_t signew;
- if( (sigemptyset(&signew) == -1) ||
- (sigaddset(&signew, SIGINT) == -1) ||
- (sigaddset(&signew,SIGQUIT) == -1) ||
- (sigaddset(&signew,SIGTSTP) == -1) ){
- return -1;
- }
- if( sigprocmask(SIG_BLOCK,&signew,sigold) == -1){
- return -1;
- }
- return 0;
- }
- int ReleaseDelay(sigset_t * sigold){
- if(sigprocmask(SIG_SETMASK,sigold,NULL) == -1)
- return -1;
- return 0;
- }
- int SetMode(int fd,int option){
- struct termios term;
- if(tcgetattr(fd,&term) == -1){
- return -1;
- }
- if(option)
- term.c_lflag |= ECHOFLAGS;
- else
- term.c_lflag &= ~ECHOFLAGS;
- if(tcsetattr(fd,TCSAFLUSH,&term) == -1){
- return -1;
- }
- return 0;
- }
- int GetPassword(char * passwd,
- int retval;
- retval = ReadLine(STDIN_FILENO,passwd,size);
- if(retval < 0)
- return -1;
- return retval;
- }
- 測試執行
- #include <stdio.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <signal.h>
- #include "mystring.h"
- #include "file.h"
- #include "tools.h"
- #include "myerror.h"
- #include "restart.h"
- #include "passwd.h"
- int main(int argc,char *argv[]){
- char passwd[20] = {0};
- int turn = 1;
- int retval;
- sigset_t sigold;
- if(DelayInt(&sigold) == -1){
- fprintf(stderr,"delayint error!/n");
- return -1;
- }
- while(turn){
- printf("/nPlease Input your password:");
- fflush(stdout);
- SetMode(STDIN_FILENO,0);
- retval = GetPassword(passwd,sizeof(passwd));
- if(retval < 0)
- turn = 1;
- else
- turn = 0;
- SetMode(STDIN_FILENO,1);
- }
- while(ReleaseDelay(&sigold) == -1)
- ;
- printf("/nYour password is %s/n",passwd);
- return 0;
- }