I want to make a ghosted input for a password in one of my programs. Fiddling around with some sample code from the getc() manual, I finally came up with this:
#!/usr/bin/perl
$BSD_STYLE = 1;
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
}
else {
system "stty", '-icanon', 'eol', "\001";
}
my $in = '';
while (($key = getc(STDIN)) ne "\n") {
$in .= $key;
print "\x08 \x08";
}
if ($BSD_STYLE) {
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
}
else {
system "stty", 'icanon', 'eol', '^@'; # ASCII null
}
print "$in\n";
This seems to work perfectly, BUT. Is there an easier/better way of doing this? Failing that, does anyone have any ideas on how to detect whether $BSD_STYLE should be set or not? I was thinking maybe running stty with some parameters and checking the output for some sort of signatures, but this seems to me to be a "bad" solution.