Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brenopop

macrumors newbie
Original poster
Dear friends, I wanna write a code in C that when the user writes something, doesn't show what they wrote, instead, the program shows only asterisks.
I can't find a function to do this, because scanf, gets or getchar don`t hide what the user write.

summarizing, I want to do a login system that doesn't show up what the user write in the password field.
 
Last edited:

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Man, I'm sorry, but I'm not understanding this site. If you could make a simple example to show me, I'll thank you a lot. :)

~ Edited ~
I was looking for this and I found something interesting: the function getch(). BUT, it does't work, even with the curses library. Help

You can't use getch by itself, curses require you to do some initial setup and cleanup. I assume you are using Terminal so try the getpass function. I reads from the terminal to an internal buffer and turns off character echo to stdout.

Read more about the function with: man getpass in the terminal.
 

brenopop

macrumors newbie
Original poster
You can't use getch by itself, curses require you to do some initial setup and cleanup. I assume you are using Terminal so try the getpass function. I reads from the terminal to an internal buffer and turns off character echo to stdout.

Read more about the function with: man getpass in the terminal.

Thank you, man. And how can i use curses? this initial setup and cleanup, How can I do it?
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Thank you, man. And how can i use curses? this initial setup and cleanup, How can I do it?

Well, look up some tutorial on curses if you want to use that. I mean you can use noecho with curses, but it uses its own functions to read and write to the screen, after you do the initial setup, depending on what you want. But, I assume you want to use the normal terminal and just get a string without echo to stdout, getpass does that in 1 line.
 

brenopop

macrumors newbie
Original poster
Well, look up some tutorial on curses if you want to use that. I mean you can use noecho with curses, but it uses it's own functions to read and write to the screen, after you do the initial setup, depending on what you want. But, I assume you want to use the normal terminal and just get a string without echo to stdout, getpass does that in 1 line.

Yeah, that's what I want. Thank you so much. :)
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
There's a problem now, I don't know how to use getpass() and how to make show up "*" instead the input of keyboard.

I just want to mention that having * show up is:

1 - Less secure, as people watching the screen now know the password length.

2 - Not normal for a command line program.
 

brenopop

macrumors newbie
Original poster
I just want to mention that having * show up is:

1 - Less secure, as people watching the screen now know the password length.

2 - Not normal for a command line program.

What can I do instead?
Isn't that the pattern for password in almost everything?

----------

It would be quite easy to create a dialog that would require a user to type a password into a secure text field using XCode and Cocoa.

I see, but it is a homework that we suppose to do all of this in C.
anyway, thank you for help. My next task will be bend Cocoa hehe :D
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
What can I do instead?
Isn't that the pattern for password in almost everything?

No, the normal thing for passwords on a command line are that they don't show anything as you type them.

If you're making a command line program that needs the user to enter a password, I would suggest making it behave the same as other command line programs and show nothing as the password is entered. To replicate this, just use getpass().

If you're making a program with a GUI, then those normally show an asterisk for each character entered. You should use the proper password input widget for whatever GUI framework you're using - as others mentioned, in Cocoa the proper widget is the NSSecureTextField.

But you said this program is in C, which suggests you're probably not going to have a GUI, as programs with GUIs are normally done in Obj-C or C++ instead of pure C (or some other language entirely.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.