Chinaunix首页 | 论坛 | 博客
  • 博客访问: 332594
  • 博文数量: 53
  • 博客积分: 2200
  • 博客等级: 大尉
  • 技术积分: 881
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-28 18:00
文章分类

全部博文(53)

文章存档

2010年(12)

2009年(33)

2008年(8)

我的朋友

分类:

2008-08-27 16:20:40

 

#include <termio.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int GetTermInput(char *buf, int len ,char *prompt, char echo);

int main(int argc, char **argv)
{
    char name[1024];
    char password[1024], verifypwd[1024];
    int ret;

    memset(name, 0, sizeof(name));
    GetTermInput(name, 1024, "New user name:", 0);
    printf("\n");
    memset(password, 0, sizeof(password));
    GetTermInput(password, 1024, "New password:", '*');
    printf("\n");
    memset(verifypwd, 0, sizeof(verifypwd));
    GetTermInput(verifypwd, 1024, "Retype new password:", '*');
    printf("\n");

    printf("name[%s]\n", name);
    printf("pwd[%s]\n", password);
    printf("verify[%s]\n", verifypwd);

}

int GetTermInput(char *buf, int len ,char *prompt, char echo)
{
    struct termio tio, tin;
    char selected;
    int order;

    order = 0;

    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;
    ioctl(0,TCSETA,&tin);

    printf("%s", prompt);
    do{
        selected =fgetc(stdin);
        if((selected=='\b')&&(order>0))
        {
            fputc('\b',stdout);
            fputc(' ',stdout);
            fputc('\b',stdout);
            order--;
            buf--;
            *buf='\0';
        }
        else if((selected!='\n')&&(selected!='\r')&&(selected!='\b'))
        {

            *buf++=selected;
            order++;
            fputc(echo?echo:selected,stdout);
            fflush(stdout);
        }
    }while ((selected!='\n')&&(selected!='\r')&&(order>=0)&&(order<len));

    /*
     * * Reset the old tty modes.
     * */
    ioctl(0, TCSETA, &tio);

    return 0;
}


阅读(995) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Kerberos简介

给主人留下些什么吧!~~