2

Is there any way to hide user input when asked for in C? For example:

char *str = malloc(sizeof(char *));
printf("Enter something: ");
scanf("%s", str);getchar();
printf("\nYou entered: %s", str);

// This program would show you what you were writing something as you wrote it. 
// Is there any way to stop that?

Another thing, is how can you only allow certain characters? For example:

char c;
printf("Yes or No? (y/n): ");
scanf("%c", &c);getchar();
printf("\nYou entered: %c", c);

// No matter what the user inputs, it will show up, can you restrict that only 
// showing up if y or n are entered?
atb
  • 943
  • 4
  • 14
  • 30
  • Side Note: `char *str = malloc(sizeof(char *));` seems wrong. scanf is not safe to read C strings – another.anon.coward Mar 16 '12 at 04:35
  • 1
    possible duplicate of [Read a password from std::cin](http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin) (even though OP is not asking about password input, the accepted post in the linked thread shows how to disable/enable `terminal echo`) – Filip Roséen - refp Mar 16 '12 at 04:35
  • forgot to mention environment, is this some kind of posix compliant shell, win console or what? Your terminal handles input buffer and afaik there is no portable way to do this. – Tomas Pruzina Mar 16 '12 at 04:36

2 Answers2

3
    #include <stdio.h>  
#include <termios.h>  
#include <unistd.h>  
#include <errno.h>  
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)  
int set_disp_mode(int fd,int option)  
{  
   int err;  
   struct termios term;  
   if(tcgetattr(fd,&term)==-1){  
     perror("Cannot get the attribution of the terminal");  
     return 1;  
   }  
   if(option)  
        term.c_lflag|=ECHOFLAGS;  
   else  
        term.c_lflag &=~ECHOFLAGS;  
   err=tcsetattr(fd,TCSAFLUSH,&term);  
   if(err==-1 && err==EINTR){  
        perror("Cannot set the attribution of the terminal");  
        return 1;  
   }  
   return 0;  
}  
int getpasswd(char* passwd, int size)  
{  
   int c;  
   int n = 0;  

   printf("Please Input password:");  

   do{  
      c=getchar();  
      if (c != '\n'||c!='\r'){  
         passwd[n++] = c;  
      }  
   }while(c != '\n' && c !='\r' && n < (size - 1));  
   passwd[n] = '\0';  
   return n;  
}  
int main()  
{  
   char *p,passwd[20],name[20];  
   printf("Please Input name:");  
   scanf("%s",name);  
   getchar();
   set_disp_mode(STDIN_FILENO,0);  
   getpasswd(passwd, sizeof(passwd));    
   p=passwd;  
   while(*p!='\n')  
     p++;  
   *p='\0';  
   printf("\nYour name is: %s",name);  
   printf("\nYour passwd is: %s\n", passwd);  
   printf("Press any key continue ...\n");  
   set_disp_mode(STDIN_FILENO,1);  
   getchar();  
   return 0;  
}  

for linux

gdj
  • 61
  • 1
0

For the sake of completeness: There is no way to do this in C. (That is, standard, plain C without any platform-specific libraries or extensions.)

You did not state why you wanted to do this (or on what platform), so it's hard to make relevant suggestions. You could try a console UI library or a GUI library. You could also try your platform's console libraries. (Windows, Linux)

aib
  • 45,516
  • 10
  • 73
  • 79