1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#ifdef sun
#include <stropts.h>
#else
#include <sys/ioctl.h>
#endif
#include <sys/termios.h>
#include <string.h>
#include <stdio.h>
#ifdef __linux__
#define TIOCGETA TCGETS
#define TIOCSETA TCSETS
#else
#ifdef sun
#define TIOCGETA TCGETA
#define TIOCSETA TCSETA
#endif
#endif
/*
** ioctl() recupere les attributs de la line discipline du term
** et permet de les modifier
*/
int init_termcps()
{
struct termios t;
//On recupere les attributs
if (ioctl(0, TIOCGETA, &t) < 0)
return (0);
//On retire l'echo
t.c_lflag &= ~ECHO;
//On set les attributs
if (ioctl(0, TIOCSETA, &t) < 0)
return (0);
return (1);
}
/*
** Ne pas oublier d'appeler cette fonction en fin
** de programme pour remettre l'echo.
*/
int revoke_termcps()
{
struct termios t;
if (ioctl(0, TIOCGETA, &t) < 0)
return (0);
t.c_lflag |= ECHO;
if (ioctl(0, TIOCSETA, &t) < 0)
return (0);
return (1);
}
int main()
{
char buffer[128];
//On retire l'echo
if (!init_termcps())
return (1);
//On lit la phrase
if (fgets(buffer, sizeof(buffer) / sizeof(*buffer), stdin))
{
buffer[strlen(buffer) - 1] = 0;
printf("You entered: '%s'\n", buffer);
}
//On remet l'echo
if (!revoke_termcps())
return (1);
return (0);
} |
Partager