L
Lars Eighner
In the code below, the line "unsigned int ktop,kbot;"
seems to be useless as ktop and kbot are never used. Yet,
this work with it and breaks without it.
Any idea why?
#include <stdio.h>
#include <termios.h>
#include <string.h>
main ()
{
getkey();
}
getkey ()
{
struct termios unwhacked, whacked;
unsigned int ktop,kbot;
char *k;
char kstr[10];
tcflag_t aswas;
fflush(0);
tcgetattr(0,&unwhacked); /* get terminal state */
whacked = unwhacked; /* save state for restore */
whacked.c_lflag &= ~ICANON; /* turn off cannical input */
whacked.c_lflag &= ~ECHO; /* turn off echoing */
whacked.c_cc[VMIN] = 1; /* capture at least 1 character */
whacked.c_cc[VTIME] = 1; /* and of them that come quick */
tcsetattr(0,TCSANOW,&whacked); /* whack the terminal with new flags now */
read (0,&k,5);
printf ("value is %x \n",k);
sprintf(kstr,"%x",k);
printf("%s length is = %i",kstr,strlen(kstr));
if(strlen(kstr) == 2){ /* not a function string */
if( kstr[0] > '1' && kstr[0] < '8'){
if (kstr[0] == '2' && kstr[1] == '0')
{printf (" letter is <space>");}else
/* yeah, it is printable ASCII sort of */
if (kstr[0] == '7' && kstr[1] == 'f')
{printf (" letter is <^? DEL>");}else
if(kstr[0] >= '2')
{printf (" letter is %c",k);}
/* gets the printable ASCII characters */
}
if (kstr[0] > '9')
{printf (" letter is %c",k);} /* printable 8 bit characters */
}
tcsetattr(0,TCSANOW,&unwhacked); /* unwhack the terminal */
return 0;
}
seems to be useless as ktop and kbot are never used. Yet,
this work with it and breaks without it.
Any idea why?
#include <stdio.h>
#include <termios.h>
#include <string.h>
main ()
{
getkey();
}
getkey ()
{
struct termios unwhacked, whacked;
unsigned int ktop,kbot;
char *k;
char kstr[10];
tcflag_t aswas;
fflush(0);
tcgetattr(0,&unwhacked); /* get terminal state */
whacked = unwhacked; /* save state for restore */
whacked.c_lflag &= ~ICANON; /* turn off cannical input */
whacked.c_lflag &= ~ECHO; /* turn off echoing */
whacked.c_cc[VMIN] = 1; /* capture at least 1 character */
whacked.c_cc[VTIME] = 1; /* and of them that come quick */
tcsetattr(0,TCSANOW,&whacked); /* whack the terminal with new flags now */
read (0,&k,5);
printf ("value is %x \n",k);
sprintf(kstr,"%x",k);
printf("%s length is = %i",kstr,strlen(kstr));
if(strlen(kstr) == 2){ /* not a function string */
if( kstr[0] > '1' && kstr[0] < '8'){
if (kstr[0] == '2' && kstr[1] == '0')
{printf (" letter is <space>");}else
/* yeah, it is printable ASCII sort of */
if (kstr[0] == '7' && kstr[1] == 'f')
{printf (" letter is <^? DEL>");}else
if(kstr[0] >= '2')
{printf (" letter is %c",k);}
/* gets the printable ASCII characters */
}
if (kstr[0] > '9')
{printf (" letter is %c",k);} /* printable 8 bit characters */
}
tcsetattr(0,TCSANOW,&unwhacked); /* unwhack the terminal */
return 0;
}