Click to See Complete Forum and Search --> : Replace password with *


debiandude
08-02-2001, 09:48 PM
Im writing a program that gets a users name and then the password. I would like instead of the password being echo display a * for every character. The code is in c, any help would be appreciated.

sans-hubris
08-02-2001, 10:51 PM
You need ncurses for that. CCAE (http://www.codeexamples.org) has a good example (http://www.codeexamples.org/cgi-bin/c2h/c2h.cgi?type=HTMLdetail&filename=nc_menu.c).

TheLinuxDuck
08-03-2001, 09:18 AM
Although a lot of people like ncurses, I don't particularly like it.

My understanding of your situation is this:

To change what character is echoed, you'll need to first set the terminal so that it is unbuffered, so that when the keyboard is pressed, the key automatically gets pushed through.

It's not simple, but once it's in place, it's not that bad.

I've posted some source for doing this (in C) a number of times in this forum, so do a search, and I'm sure it will turnip.

Included in that example is a simple string building function, using unbuffered input. It would be fairly easy to change the output char of that input function. Maybe even change it so that the function accepts a new parameters, that if true, echoes a '*', instead of the char.

If you don't find that example, let me know and I'll post it again. (try searching for termios).

Ok, so I decided to search for it. here is the link (http://www.linuxnewbie.org/cgi-bin/ubbcgi/ultimatebb.cgi?ubb=get_topic&f=14&t=001634)

jemfinch
08-03-2001, 09:52 AM
Hmm. If I were you, I'd just use termios to turn off terminal echo. Then, at every character you read, if it's not a backspace character, turn on terminal echo, print a '*', turn it back off, and continue processing more characters. If it is a backspace character, print a backspace character.

Jeremy

debiandude
08-03-2001, 04:43 PM
This is what I did. When I get home I am going to put into the actual program, this is just testing for now that my code work.



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

int getch(void);

int main(void) {

char c[20];
int x=0;


do {
c[x]=getch();
if(c[x]!=8) {
printf("*");
x++;
} else if(x>0) {
printf("%c %c", 8,8);
x--;
}
} while((c[x-1]!=10) && x<(20));

c[x]=0;

printf("\n %s\n", c);

return;

}

int getch(void) {

struct termios term, original;
char c;

if(tcgetattr(fileno(stdin), &term) < 0)
{
perror("Error getting terminal information");
return -1;
}

original = term;

term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO;
term.c_cc[VMIN]=1;
term.c_cc[VTIME]=0;

if(tcsetattr(fileno(stdin), TCSANOW, &term) < 0)
{
perror("Error setting terminal information");
return -1;
}

c = getc(stdin);

if(tcsetattr(fileno(stdin), TCSANOW, &original) < 0)
{
perror("Error setting terminal information");
return -1;
}

return c;

}

TheLinuxDuck
08-03-2001, 05:27 PM
debiandude:

I just compiled it and it ran fine. The only complaint I got was not returning a value in main.. (^=

Looks nice! (^=