i need make work linux, know conio.h not linux , main problem getch() function. tried using lib curses.h still got lot of errors. takes users input of password , converts **** safety reasons.
old code:
#include<stdio.h> #include<conio.h> void main() { char password[25],ch; int i; clrscr(); puts("enter password: "); while(1) { if(i<0) i=0; ch=getch(); if(ch==13) break; if(ch==8) { putch('b'); putch(null); putch('b'); i--; continue; } password[i++]=ch; ch='*'; putch(ch); } password[i]=''; printf("\npassword enterd : %s",password); getch(); } updated code based on @souravghosh's answer:
#include<stdio.h> int main(void) { char password[25],ch; int i; //system("clear"); puts("enter password: "); while(1) { if(i<0) i=0; ch=getchar(); if(ch==13) break; if(ch==8) { putchar('b'); putchar('b'); i--; continue; } password[i++]=ch; ch='*'; putchar(ch); } password[i]=' '; printf("\npassword enterd : %s",password); getchar(); return 0; }
if terminal supports these escape codes, conceal typing password entered.
#include <stdio.h> void userpw ( char *pw, size_t pwsize) { int = 0; int ch = 0; printf ( "\033[8m");//conceal typing while ( 1) { ch = getchar(); if ( ch == '\r' || ch == '\n' || ch == eof) {//get characters until cr or nl break; } if ( < pwsize - 1) {//do not save pw longer space in pw pw[i] = ch; //longer pw can entered excess ignored pw[i + 1] = '\0'; } i++; } printf ( "\033[28m");//reveal typing printf ( "\033[1;1h\033[2j");//clear screen } int main ( ) { char password[20]; printf ( "enter password: "); fflush ( stdout);//prompt not have '\n' make sure prints userpw ( password, sizeof ( password));//password array , size printf ( "\nentered [%s]\n", password);//instead of printing verify entered password return 0; }
Comments
Post a Comment