Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

taking password input

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
US
Hi! i want to be able to take in a password from the prompt. is there a function which gets the char from the keyboard but doesnt echo it to the screen like most gets do. or if there is a better way or some way that i can input a password and not display anything on the screen, that would be great. thanx a lot in advance.
 
try this:

#include <conio.h>
#include <stdio.h>

void main( void )
{

char password[100];
int i =0;
printf (&quot;Input the password:&quot;);
char ch;
do {
while( !_kbhit()); // wait for the char;
ch = _getch();
if (ch != 13) password[i++] = ch;
else password = '\0';
} while (ch !=13);

printf( &quot;\n%s\n&quot;,password);
}
 
Hi! thanx a ton, it works perfect, but i am sorry i forgot to mention one small detail on UNIX, this wouldnt compile on unix cuz of conio.h and if there is a different header file that i can include for the kbhit function in unix please let me know thanx a ton for this example.( it might be helpful when i am doing it on windows sometime)
 
this one should work in Unix.

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sgtty.h>

int u_getchar(void)
{
int ret;
fd_set rfc;
unsigned char buf;

if(read(0, &buf, 1)!=1) ret=0;
else ret=buf;
return ret;
}

int main(void)
{

struct timeval tv;
struct sgttyb ots,ts;
tv.tv_sec = 2;
tv.tv_usec = 0;

gtty(0, &ots);

ts = ots;

// disable the screen echo
ts.sg_flags |= RAW;
ts.sg_flags &=~ ECHO;

stty(0,&ts);

char password[100];
int i =0;
char key = 0;
do{
key = u_getchar();
if(key!=13) password[i++] = key;
else password = '\0';
} while(key!=13);

printf(&quot;\n%s\n&quot;, password);

// reset tty
stty(0,&ots);
}
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question
Replies
4
Views
127
Replies
3
Views
112
Replies
10
Views
175
Replies
3
Views
176
Replies
2
Views
83

Part and Inventory Search

Sponsor

Back
Top