-3

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.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35

1 Answers1

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
  • `new.c_lflag &= ~ICANON;` You are using `new` Here? – Michi Jan 25 '17 at 09:57
  • @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
  • how about [fixing this](http://pastebin.com/raw/LdxPVbc9)? – Michi Jan 25 '17 at 10:00
  • @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
  • @Michi Yes it is, on some exotic platform can be a problem. I'm editing – LPs Jan 25 '17 at 10:09
  • 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
  • @Michi I edited, in a different way, but your is correct too. – LPs Jan 25 '17 at 10:47