Hi, Jan und alle.
-----Ursprüngliche Nachricht-----
Von: Jan.Trippler@t-online.de [SMTP:Jan.Trippler@t-online.de]
Gesendet am: Montag, 30. Juli 2001 19:49
An: 'SuSE'
Betreff: Re: Passworteingabe ohne Ausgabe der Zeichen
Nur mal so aus Interesse: Wie ist die Lösung? Wenns zu lang wird,
gern auch per PM, ansonsten ist das wahrscheinlich auch für andere
interessant.
Copyright by Daniel Wolpert (dw@siebel.de):
/*
*
* compiled via: gcc <src>.c -pedantic -Wall -ansi -o <binary>
*
*/
#include
#include
#include
#include
#include
#include
extern void perror();
extern int printf();
#define MAX_CHARS_IN_PWD 7
#define PROMPT 0
#define TIMOUT 1
int main(void) {
int res;
struct pollfd event;
struct termios old_t, new_t;
char pwd[MAX_CHARS_IN_PWD+1];
char *msg[2];
char spare;
int idx;
int timeout;
msg[PROMPT] = "Enter password, dude: ";
msg[TIMOUT] = "Timeout. Where' anybody gone?\n";
event.fd = 0;
event.events = POLLIN;
event.revents = POLLIN;
memset( (char *)pwd,0,MAX_CHARS_IN_PWD);
if ( tcgetattr(0, &old_t) == -1 ) {
perror("tcgetattr() failed");
return 1;
}
memcpy(&new_t, &old_t, sizeof(struct termios));
tcflush(0,TCIFLUSH);
new_t.c_iflag|=(OCRNL);
new_t.c_lflag&=~(ECHO|ISTRIP);
new_t.c_lflag|=ISIG;
if ( tcsetattr(0,TCSANOW, &new_t) == -1 ) {
perror("tcsetattr() failed");
return 1;
}
idx=0;
prompt:
timeout=0;
write(1, msg[PROMPT], strlen(msg[PROMPT]) );
sit_n_wait:
res=poll(&event,1,1000);
switch ( res ) {
case 0:
timeout++;
if ( timeout == 5 ) {
write(1, msg[TIMOUT],
strlen(msg[TIMOUT]) );
goto prompt;
}
break;
case POLLIN:
read(0, (char *)&spare, 1);
switch ( spare ) {
case '\n':
goto restore;
break;
default:
if ( idx <= MAX_CHARS_IN_PWD ) {
pwd[idx]=spare;
spare=0;
idx++;
}
}
break;
}
goto sit_n_wait;
restore:
pwd[idx]=0;
if ( tcsetattr(0,TCSANOW, &old_t) == -1 ) {
perror("tcsetattr() failed");
}
tcflush(0,TCIFLUSH);
tcflush(0,TCOFLUSH);
printf("\npwd: %s\n",pwd);
return 0;
}
--
Wolfgang