?
Solved

password mask implemented in c program

Posted on 2003-03-03
8
Medium Priority
?
278 Views
Last Modified: 2008-02-01
Dear all,

I have a c program that accpts a sequence of chars from UNIX prompt for user
to enter password

char identifier [21];
fgets(identifier, 21, stdin);

However I want when the user type his/her password in the prompt

'*' will appear instead of echo the things user type.

Is there any code can do the above?

Thank you very much.

Regards,
Ginrai
0
Comment
Question by:ginrai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8056048
have a look at this .. this doesnt echo * .. but it disables echoing
it may not be a standard function these days
http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?getpasswd+S
0
 
LVL 22

Expert Comment

by:grg99
ID: 8057637
Depends on the system you are doing this on.

On Unix, it might work to do a system( "stty -echo" ) to turn off character echoing.  

Oh, and if you want to echo a "*" in real-time, you'll also need system( "stty -raw" ) or somesuch so you get each character as typed.

This is a big nasty as you have to undo these when your program exits.  If your program bombs, you'd better have an exit procedure or try...except block to catch the error and reset things.

Regards,

grg99
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8057790
>> Is there any code can do the above?

Try this,

char pass[25];
input( pass, 25 );

...

void input( char* buffer, int length )
{
    int pos = 0;
    char c;
 
    buffer[0] = '\0';

    while( ( c = getch() ) != 13 )
    {
        if( c != 8 && pos < ( length - 1 ) )
        {
            printf("*");
            buffer[pos] = c;
            pos++;
        }
        else if( pos != 0 )
        {
            printf("%c %c",8,8);
            pos--;
        }
    }
     buffer[pos] = '\0';
}

Exceter
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:Exceter
ID: 8057805
>> else if( pos != 0 )

Sorry, this line should be,

else if( pos != 0 && c == 8 )

Exceter
0
 

Author Comment

by:ginrai
ID: 8062974
Dear Exceter,

I've tried the code but it does not work, it still echo the character I typed and put some * after I press enter.

Regards,
Ginrai
0
 
LVL 8

Accepted Solution

by:
Exceter earned 150 total points
ID: 8067894
>> I've tried the code but it does not work, it still echo the character I typed and put some * after I press enter.

That is REALLY weird. The code works perfectly on my computer. What OS are you running? Which compiler do you have?

Try this demo program,

#include <stdio.h>
#include <conio.h>
#define STRLEN 25

void input( char*, int );

int main()
{
     char s[STRLEN];
     printf("Enter password: ");
     input(s,STRLEN);
        printf("\n\n%s\n",s);
     return 0;
}

void input( char* buffer, int length )
{
   int pos = 0;
   char c;

   buffer[0] = '\0';

   while( ( c = getch() ) != 13 )
   {
       if( c != 8 && pos < ( length - 1 ) )
       {
           printf("*");
           buffer[pos] = c;
           pos++;
       }
       else if( pos != 0 && c == 8 )
       {
           printf("%c %c",8,8);
           pos--;
       }
   }
    buffer[pos] = '\0';
}

Exceter
0
 

Author Comment

by:ginrai
ID: 8126271
Thank you, Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8127723
Why the B?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question