| /home -> /home/fatec/ -> /home/fatec/soii -> /home/fatec/soii/echodisable --::-- |
DESABILITAR ECHO NA ENTRADA DE DADOS
Prof. Rossano Pablo Pinto, MSc.
|
PROGRAMA 1 ⇒ Desabilita echo de caracteres na entrada de dados: termios-test.c
/*
* Author: Rossano Pablo Pinto - rossano at gmail com
* Fri May 21 20:50:57 BRT 2010
*
*/
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
char* get_string_no_echo();
int main(void)
{
char *message;
message = get_string_no_echo();
printf("\nMessage: %s\n",message);
return 0;
}
char* get_string_no_echo()
{
int res = 0;
struct termios *termios_p_orig =
(struct termios *)malloc(sizeof(struct termios));
struct termios *termios_p_alter =
(struct termios *)malloc(sizeof(struct termios));
char *message = (char *)malloc(80);
// GET ORIGINAL CONFIGURATION
tcgetattr(1, termios_p_orig);
// COPY ORIGINAL CONFIGURATION TO termios_p_alter
*termios_p_alter = *termios_p_orig;
// ALTER termios_p_alter TO DISABLE ECHO AT TERMINAL INPUT
termios_p_alter->c_lflag &= ~ECHO;
tcsetattr(1, TCSANOW, termios_p_alter);
printf("password: ");
scanf("%s",message);
// RESTORE ORIGINAL TERMINAL CONFIGURATION
tcsetattr(1, TCSANOW,termios_p_orig);
return message;
}
|