Enter Password in C Enter Password in C unix unix

Enter Password in C


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).


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


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.