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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #ifndef SUCCESS
#define SUCCESS 0
#define ERROR 1
#endif
/*
* C h e c k s _ T o _ R e a d _ I n p u t
*
*/
static int Checks_To_Read_Input ( void )
{
int iret ;
struct pollfd fds[1] ;
fds[0].events = POLLIN | POLLRDNORM ;
fds[0].fd = fileno(stdin) ;
/* Checks the state of the communication */
iret = poll (fds, 1, 100 );
if ( (iret <= 0) ||
(((! fds[0].revents & POLLIN) && (fds[0].revents & POLLRDNORM)) ||
(fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP)) )
return ERROR ;
else
return SUCCESS ;
}
/*
* Q u e s t i o n s
*
*/
static int Questions()
{
int rep=0, n ;
char c, buf[3] ;
fprintf (stderr, "\n\n What would you like to do :\n");
fprintf (stderr, "\n 1 : do some stuff");
fprintf (stderr, "\n 2 : do other stuff");
fprintf (stderr, "\n .....");
fprintf (stderr, "\n 50 : exit ");
fprintf (stderr, "\n\n Please enter your choice : ");
while ( Checks_To_Read_Input() == ERROR )
{
}
n = 0 ;
c = 0 ;
while ( (c != '\n') || (c != 10) || (c != 13) )
{
fscanf ( stdin, "%c", &c );
if ( (c != '\n') || (c != 10)|| (c != 13) )
{
buf[n] = c ;
n = n + 1 ;
if ( n == 2 )
break ;
}
}
if ( n == 0 )
rep = Questions();
sscanf ( buf, "%d", &rep );
switch ( rep )
{
....
}
return rep ;
}
/*
* W a i t s _ F o r _ I n p u t
*
*/
static void Waits_For_Input( void )
{
int s=0 ;
while ( (s = Questions()) != 50 )
{
/* Do the processing */
}
}
/*
* Main program
*
*/
int main(int argc, char *argv[])
{
Waits_For_Input();
return 0 ;
} |
Partager