1 #define _POSIX_SOURCE 2 #define _RESEARCH_SOURCE 3 #include <stdio.h> 4 #include <signal.h> 5 #include <limits.h> 6 #include <libv.h> 7 8 char * getpass(char * prompt)9getpass(char *prompt) 10 { 11 int c; 12 char *p; 13 FILE *fi; 14 static char pbuf[PASS_MAX]; 15 void (*sig)(int); 16 17 if ((fi = fopen("/dev/cons", "r")) == NULL) 18 fi = stdin; 19 else 20 setbuf(fi, NULL); 21 sig = signal(SIGINT, SIG_IGN); 22 tty_echooff(fileno(fi)); 23 fprintf(stderr, "%s", prompt); 24 fflush(stderr); 25 26 for (p = pbuf; (c = getc(fi)) != '\n' && c != EOF; ) 27 if (c == ('u' & 037)) 28 p = pbuf; 29 else if (c == '\b') { 30 if (p > pbuf) 31 p--; 32 } else if (p < &pbuf[sizeof(pbuf)-1]) 33 *p++ = c; 34 *p = '\0'; 35 36 fprintf(stderr, "\n"); 37 fflush(stderr); 38 tty_echoon(fileno(fi)); 39 signal(SIGINT, sig); 40 if (fi != stdin) 41 fclose(fi); 42 return(pbuf); 43 } 44