Password, while typing should not be visible(stars should be visible instead of characters). Can it be done in C, as we have in JAVA.
Asked
Active
Viewed 79 times
-3
-
1Yes, it can be done, but it is platform dependent and not related to the C language. – Jabberwocky Jan 25 '17 at 09:45
-
Can anyone suggest me some function which will work same. – Deepak Tatyaji Ahire Jan 25 '17 at 09:46
-
If you tell us what platform you are on, you might get some useful answers. – Jabberwocky Jan 25 '17 at 09:46
-
Codeblocks , I guess – Deepak Tatyaji Ahire Jan 25 '17 at 09:47
-
Please put al relevant information into the question as well. And Codeblocks is not a platform, but Linux and Windows are. – Jabberwocky Jan 25 '17 at 09:50
-
Then kindly suggest for both man!! – Deepak Tatyaji Ahire Jan 25 '17 at 09:51
-
4Possible duplicate of [Better alternative for getpass function](http://stackoverflow.com/questions/32396188/better-alternative-for-getpass-function) – Michi Jan 25 '17 at 09:52
-
1Is this a command line application or are you using some widget toolkit? There's nothing in C to do with passwords, much the same as there's nothing in C to get weather forecasts, but all can be implemented. Unless you want accurate weather forecasts... – Colin Jan 25 '17 at 09:53
-
I am expecting a simple C code!!! in any platform and not downvotes. – Deepak Tatyaji Ahire Jan 25 '17 at 09:57
-
@DeepakAhire There is an Answer for you, but are you sure you need it for C only, does GTK helps ? – Michi Jan 25 '17 at 10:02
1 Answers
3
Using a linux platform and terminal you must modify terminal behavior, disabling echoing and using non canonical mode
#include <termios.h>
#include <stdio.h>
#include <stdbool.h>
static struct termios oldPar, newPar;
void initTermios(bool echo) {
tcgetattr(0, &oldPar);
newPar = oldPar;
newPar.c_lflag &= (tcflag_t)~ICANON;
newPar.c_lflag &= echo ? (tcflag_t)ECHO : (tcflag_t)~ECHO;
tcsetattr(0, TCSANOW, &newPar);
}
void resetTermios(void) {
tcsetattr(0, TCSANOW, &oldPar);
}
char getch_(bool echo) {
int ch;
initTermios(echo);
ch = getchar();
resetTermios();
return (char)ch;
}
char getch(void) {
return getch_(false);
}
int main(void) {
char c;
char psw[16] = { 0 };
size_t index = 0;
printf("Insert password: ");
do
{
c = getch();
printf("*");
psw[index++]= c;
}
while ((index < (sizeof(psw)-1)) && (c!='\n'));
printf("\n\nPassword: %s\n", psw);
return 0;
}

LPs
- 16,045
- 8
- 30
- 61
-
-
@Michi I'm not getting you, but I edited changing the name of that var, if that was the problem. – LPs Jan 25 '17 at 09:59
-
-
@Michi What is your platform? What are you compilation flags? Nothing triggered on my Linux platform with gcc 4.9.2 with `-Wall -Wextra -Werror -pedantic-errors` – LPs Jan 25 '17 at 10:04
-
You are using an verry old Compiler version, I'm on GCC-6 with ==>>>: `-Wpedantic -std=c11 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes -Wmisleading-indentation -Wduplicated-cond -Wold-style-definition -Wconversion -Wshadow -Winit-self -Wfloat-equal -Wwrite-strings -O0 -g` – Michi Jan 25 '17 at 10:05
-
any way in `char getch_(int echo)⅞°` `char ch` should be `int ch` and the function should also return int or at least you need a cast there like `return (char)ch;` – Michi Jan 25 '17 at 10:08
-
-
Your code still need to be fixed `negative integer implicitly converted to unsigned type` Here ==> `newPar.c_lflag &= ~ICANON;` and Here =>> `newPar.c_lflag &= echo ? ECHO : ~ECHO;` – Michi Jan 25 '17 at 10:11
-
1@Michi Not my aim here, in a too broad question. Fill free to post your own (better) answer... – LPs Jan 25 '17 at 10:12
-
Sir, I'm not judge you or your Answer, I'm just telling you that your code should be fixed. I will not post another Answer, when you gave already a good one. Fixing those two lines makes it fine. – Michi Jan 25 '17 at 10:13
-
I forgot to tell you about my Platform as you asked. I'm on Linux mint with GGC-6. There is a cast needed there on (unsigned)~ICANON and (unsigned)~ECHO and compiles fine – Michi Jan 25 '17 at 10:27
-
@Michi I know, I'll edit,. This due to the "bad" implementation of that literals that are octet instead of Hex. I really don't know why ..? – LPs Jan 25 '17 at 10:38
-
I also changed in `void initTermios(unsigned echo);` and `char getch_(unsigned echo);` ==> echo to `unsigned` – Michi Jan 25 '17 at 10:40
-