Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

hiding input from stdin

Posted on 1999-07-19
9
Medium Priority
?
242 Views
Last Modified: 2010-08-05
How could I hide input that a user puts into my program? For example, a password prompt. (Masking etc)

 Thanks
0
Comment
Question by:overge
[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
9 Comments
 

Author Comment

by:overge
ID: 1263742
No I dont have a comment to add!
0
 
LVL 2

Expert Comment

by:s_lavie
ID: 1263743
to get the input use: getch();
after each getch(): printf("\b");
0
 

Author Comment

by:overge
ID: 1263744
The context in which you gave your answer was incorrect

The context is :

printf("\nSecondary password : ", pass2);

scanf("%s", &pass2);

I wish for the program not to display what is entered after the :        Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:s_lavie
ID: 1263745
Let me put it this way:

instead of using: scanf("%s", &pass2);
use:
int i=0;
char ch;
while((ch = getch())!='\n')
{
    printf("\b"); /* delete the password char before you'll notice */
    pass2[i++]=ch;
}
pass2[i]='\0'

I hope it'll help...
0
 
LVL 84

Expert Comment

by:ozo
ID: 1263746
#include <stdlib.h>
strcpy(pass2,getpass("\nSecondary password : ");
0
 
LVL 86

Expert Comment

by:jkr
ID: 1263747
Or sth. like this:

BOOL CheckPwd(int nTries,char *pszPwd,char *pszPrompt)
{
int  nInput=CR;
int  nScrOff=4;
int  nOffset;
int  nPwdLen;
char *acInBuf;
BOOL bCorrPwd=FALSE;

nPwdLen=strlen(pszPwd)*sizeof(char);

if (!(acInBuf=(char *)malloc(nPwdLen+2)))
   {
      printf("\n fatal: malloc() failed!\n\n");
      abort();
   }

memset(acInBuf,0,nPwdLen+2);


for (int i=0 ; i<nTries; i++)
    {

     printf("\n\n %s",pszPrompt);
     fflush(stdout);
     nOffset=0;
     nScrOff=4;

     while(CR!=(nInput=getch()))
          {  
             *(acInBuf+nOffset)=(char)nInput;

              if (!printf("%c",'*')
                  break;
              fflush(stdout);

              if (nOffset<=nPwdLen) nOffset++;

          }

        if (bCorrPwd=(!strcmp(pszPwd,acInBuf))) break;

  };

free(acInBuf);
return(bCorrPwd);
}


Simply call it like

if (!CheckPwd(3,"mypassword","Password: "))
   exit(-1);

which will allow 3 tries ;-)

'getch()' isn't available on UN*X, but replacing 'getch()' with 'fgetc(stdin)' should solve the problem (PS: The above code was originally written using a Borland compiler ;-)
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263748
BTW, getpass() is in conio.h (BC only?)
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1263749
getch is available with UNIX if you use curses.


#include<stdio.h>
#include<curses.h>

int main()
{
int c;

initscr();
cbreak();
noecho();

c = getch();
printf("\nc is %c\n", (char)c);
printf("c is %i\n", c);

endwin();
return 0;
}

0
 

Accepted Solution

by:
lcowan earned 100 total points
ID: 1263750
S_LAVIE has a sufficient answer, but I would use putch instead of printf, and put a backspace,then an asterisk, to get a more webuser recognizable format rather than just locking the cursor in place.  The output/input turnaround should flush the 2 chars to the screen, but if not, follow the printf or 2 putch's with a fflush(stdout).  The real problem with this is that if somebody ran this to a file or through a tee, they could trap the chars to look at.  This probably doesn't matter, but if it does you'll have to turn off echo with stty -echo before, and back on afterwards. Then you can do your own displaying of asterisks or whatever to give the user some visual tracking.
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

715 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