1

I was wondering if it's possible to hide or replace what the user of the program write in the terminal.

When the user press a key when the program ask the user to type something like a password, instead of writing the password it write * (ex: password="AAABBB" but in the terminal it shows ****** or just nothing).

It's the same thing as when you write your Windows/Mac password.

thanks a lot !

Ayly_ / C noobies

Ayly_
  • 13
  • 2

1 Answers1

0

You can do this, but it will be something very system specific depending on your operating system and other factors. There are libraries that may do what you want such as "curses", which you could look into. To demonstrate the sort of hackery involved, I wrote something that works on my Ubuntu laptop. Running this may mess up your terminal settings, particularly if you break out mid program. I can use the "reset" command to fix that on Ubuntu or just reopen the terminal window.

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_LENGTH 20

int main()
{
  struct termios t, backup;
  tcgetattr(STDIN_FILENO, &t);
  backup=t;
  cfmakeraw(&t);
  tcsetattr(STDIN_FILENO, TCSANOW, &t);

  char c=0;
  int position = 0;
  char password[MAX_LENGTH+1] = {0};

  while (position<MAX_LENGTH)
  {
    c = getchar();
    if (c<0x20)
      break;

    password[position++] = c;
    putchar ('*');
  }
  tcsetattr(STDIN_FILENO, TCSANOW, &backup);

  printf("\nPassword is : %s\n", password);
}

CodeWash
  • 155
  • 6