10

I am aware that it is not possible to echo the * while you type in standard ANSI C. But is there a way to display nothing while someone is typing their password in the console. What I mean is like the sudo prompts in a Unix/Linux terminal. Like if you type in the command: sudo cp /etc/somefile ~/somedir. You are usually prompted for the root password. And while you type it in, the terminal displays nothing. Is this effect possible in C? If it is, how?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
  • What's not possible about showing "*"? printf("*"); will do it. – Peter K. Apr 14 '10 at 00:54
  • @Peter K.: I believe he's referring to the fact that all the standard input functions echo to the console. Sure, you can print *, but the character the user typed is already there. – Billy ONeal Apr 14 '10 at 00:55
  • possible duplicate http://stackoverflow.com/questions/1196418/getting-a-password-in-c-without-using-getpass-3 – N 1.1 Apr 14 '10 at 01:00
  • tinyfiledialogs is a single C file (cross-platform) offering graphic and console basic dialogs (including an inputbox and a password box) http://sf.net/p/tinyfiledialogs – tinyfiledialogs Nov 27 '20 at 09:13

5 Answers5

9

The function that you are looking for is: getpass(). You will note, though, that it is marked as "LEGACY". Although it isn't going to go anywhere, the function doesn't allow the size of the input buffer to be specified, which makes it not a very good interface. As Jefromi has noted, the glibc manual provides portable example code for implementing getpass from scratch in a way that allows an arbitrary input size (and isn't LEGACY).

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
2

sudo is written in C, so yes :). The getpass() function Safyan mentioned is probably what you want, but here's where the actual sudo tool does it if you're interested:

http://sudo.ws/repos/sudo/file/dc3bf870f91b/src/tgetpass.c#l70

Community
  • 1
  • 1
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
1

The poor-man's method of doing this is to read user input character by character, and after each character is received print out a backspace character followed by *. The output is technically sent to the console, but it is immediately erased and overwritten by an asterisk (often before that frame is even drawn to the screen). Note that this is not really a secure method and has several security holes, but for low-tech low-security applications, it works.

bta
  • 43,959
  • 6
  • 69
  • 99
0

*This is not ANSI C (Thanks Billy) sample

You can detect keypress with _kbhit(), then get the value using _getch(). Both function will not echo the content on screen.

#include <conio.h>          //For keyboard events
#include <stdio.h>          //Include this or iostream
#include <locale>           
int main()
{
    bool bContinue = true;
    char szBuffer[255] = {0};
    unsigned int nbufIndex = 0;
    while (bContinue)
    {
        if (_kbhit())
        {
            szBuffer[nbufIndex] = _getch();
            if (szBuffer[nbufIndex] == 0xD)
            {
                bContinue = false;
            }
            else
            {
                ++nbufIndex;
                printf("*");
            }
        }
    }
    printf("\n%s\n", szBuffer);
    return 0;
}
YeenFei
  • 3,180
  • 18
  • 26
0
#include <stdio.h>
#include <conio.h>

void main()
{

    char pwd[15];
    int i;
    printf("Enter Password : ");
    for(i=0;i<15;i++)
    {
    pwd[i]=getch();
        if(pwd[i]!='\r')
        {
            printf("*");
        }
        if(pwd[i]==13)
            break;
    }

    printf("\n     \nPassword is : ");
    for(i=0;i<15;i++)
    {
        printf("%d ",pwd[i]);
    }
}
MaaHii
  • 3
  • 6
  • `conio.h` does not exist in the Linux environment, see https://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux. Further can you explain a little more about your solution? – RobertS supports Monica Cellio Dec 07 '19 at 09:30
  • Welcome to Stackoverflow. Please explain your answer so others can understand easier. – octobus Dec 07 '19 at 09:46
  • For those who are code in windows TurboC/C++, visual Studio editor ------------>Yes, As someone want to start a minor project or any of the beginner wants to get asterisk at the place of array of character or string in C / C++ programming language. . As well as the electrical engineering student may also use this code for the asterisk at the place of input on the display and further proceed. – MaaHii Dec 27 '19 at 04:27