#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; }
|