flat world,composite life

enjoy my life

置顶随笔 #

[置顶]linux下接收用户输入密码的完美实现

一个小问题,在linux编一个接收用户输入密码小程序,不显示密码。
google和baidu了一下,竟然没有找到现成的,好吧,自己编一个。哪里想到,
竟然一波三折。哈哈,最终还是搞定了!
1) 很容易根据termios的结构屏蔽终端属性的输出。
但是,这样一来,用户的输入不显示在屏幕上。用户不知道自己输入的个数。
对输入的内容心里也没有底。非常不方便。
2)于是改为一个一个字符的处理格式。编程实现了用'*'代替用户的输入。
但是这样linux处于非授权模式,一个限制是‘退格’键不能用。用户必须保证一次
输入正确,万一错了的话,只能眼睁睁的重新运行程序,重来一次。
3)我最终在2)的基础上,实现了用'*'代替用户的输入,并且backspace key可用。

附代码:

#include <termio.h>
#include <stdio.h>

#define passLength 100 

int main(int argc, char **argv)
{
    struct termio tio, tin;
    char*password =(char*)malloc(passLength);
    char*b=password;
  
    ioctl(0, TCGETA, &tio);
    tin = tio;
    tin.c_lflag &= ~ECHO;      /* turn off ECHO   */
    tin.c_lflag &= ~ICANON;    /* turn off ICANON */
    tin.c_lflag &= ~ISIG;
    tin.c_cc[VINTR]=1;
    tin.c_cc[VMIN]=1;
    tin.c_cc[VTIME]=0;
    /*
     * Set the new modes.  Again we ignore return
     * values.
     */
    ioctl(0,TCSETA,&tin);
   
    char selected;
    int order=0;
    printf("Enter password:");
    do{
            selected =fgetc(stdin);    
            if((selected=='\b')&&(order>0))
            {
                 fputc('\b',stdout);
                 fputc(' ',stdout);
                 fputc('\b',stdout);
                 order--;
                 password--;
                 *password='\0';
             }else if((selected!='\n')&&(selected!='\r')&&(selected!='\b')){
        
                 *password++=selected;
                 order++;
                 fputc('*',stdout);
                 fflush(stdout);
             }
        }while ((selected!='\n')&&(selected!='\r')&&(order>=0)&&(order<passLength));
       
   /*
    * Reset the old tty modes.
    */
    ioctl(0, TCSETA, &tio);
    fprintf(stdout,"\nYou entered: %s\n",b);    
    free(b);
    exit(0);

}

posted @ 2007-07-09 17:09 pujing 阅读(777) | 评论 (2)编辑 收藏

2007年7月9日 #

linux下接收用户输入密码的完美实现

一个小问题,在linux编一个接收用户输入密码小程序,不显示密码。
google和baidu了一下,竟然没有找到现成的,好吧,自己编一个。哪里想到,
竟然一波三折。哈哈,最终还是搞定了!
1) 很容易根据termios的结构屏蔽终端属性的输出。
但是,这样一来,用户的输入不显示在屏幕上。用户不知道自己输入的个数。
对输入的内容心里也没有底。非常不方便。
2)于是改为一个一个字符的处理格式。编程实现了用'*'代替用户的输入。
但是这样linux处于非授权模式,一个限制是‘退格’键不能用。用户必须保证一次
输入正确,万一错了的话,只能眼睁睁的重新运行程序,重来一次。
3)我最终在2)的基础上,实现了用'*'代替用户的输入,并且backspace key可用。

附代码:

#include <termio.h>
#include <stdio.h>

#define passLength 100 

int main(int argc, char **argv)
{
    struct termio tio, tin;
    char*password =(char*)malloc(passLength);
    char*b=password;
  
    ioctl(0, TCGETA, &tio);
    tin = tio;
    tin.c_lflag &= ~ECHO;      /* turn off ECHO   */
    tin.c_lflag &= ~ICANON;    /* turn off ICANON */
    tin.c_lflag &= ~ISIG;
    tin.c_cc[VINTR]=1;
    tin.c_cc[VMIN]=1;
    tin.c_cc[VTIME]=0;
    /*
     * Set the new modes.  Again we ignore return
     * values.
     */
    ioctl(0,TCSETA,&tin);
   
    char selected;
    int order=0;
    printf("Enter password:");
    do{
            selected =fgetc(stdin);    
            if((selected=='\b')&&(order>0))
            {
                 fputc('\b',stdout);
                 fputc(' ',stdout);
                 fputc('\b',stdout);
                 order--;
                 password--;
                 *password='\0';
             }else if((selected!='\n')&&(selected!='\r')&&(selected!='\b')){
        
                 *password++=selected;
                 order++;
                 fputc('*',stdout);
                 fflush(stdout);
             }
        }while ((selected!='\n')&&(selected!='\r')&&(order>=0)&&(order<passLength));
       
   /*
    * Reset the old tty modes.
    */
    ioctl(0, TCSETA, &tio);
    fprintf(stdout,"\nYou entered: %s\n",b);    
    free(b);
    exit(0);

}

posted @ 2007-07-09 17:09 pujing 阅读(777) | 评论 (2)编辑 收藏

仅列出标题  

My Links

Blog Stats

常用链接

留言簿(2)

随笔档案

搜索

最新评论