该程序测试相应终端的 ctrl - c 中断和终端的控制等
/*
*
* testsignal.c
*/
#include < stdio.h >
#include < signal.h >
#include < termios.h >
#include < fcntl.h >
void ctrl_c_hander();
void set_cr_mode();
void reset_mode( int flag);
int main( int ac, char * av[])
{
int counts = 0 ,i = 0 ;
reset_mode( 0 );
set_cr_mode();
signal(SIGINT,ctrl_c_hander);
while ( 1 )
{
counts ++ ;
printf( " loop " );
for (i = counts;i > 0 ;i -- )
printf( " ! " );
printf( " \n " );
if (ac == 2 )
{
// if(atoi(av[1][0])==counts)
// exit(1);
}
sleep( 1 );
}
reset_mode( 1 );
}
void ctrl_c_hander()
{
puts( " do you want exit.(y/n)\n " );
if (tolower(getchar()) == ' y ' )
{
reset_mode( 1 );
exit( 1 );
}
return ;
}
void set_cr_mode()
{
struct termios setting;
tcgetattr( 0 , & setting);
setting.c_lflag &= ~ ICANON;
setting.c_lflag &= ~ ECHO;
setting.c_cc[VMIN] = 1 ;
tcsetattr( 0 ,TCSANOW, & setting);
}
void reset_mode( int flag)
{
static struct termios info;
// static int termflag;
static int does;
if (flag == 0 )
{
tcgetattr( 0 , & info);
// termflag=fcntl(0,F_GETFL);
does = 1 ;
}
else if (does == 1 )
{
// fcntl(0,F_SETFL,termflag);
tcsetattr( 0 ,TCSANOW, & info);
}
}
* testsignal.c
*/
#include < stdio.h >
#include < signal.h >
#include < termios.h >
#include < fcntl.h >
void ctrl_c_hander();
void set_cr_mode();
void reset_mode( int flag);
int main( int ac, char * av[])
{
int counts = 0 ,i = 0 ;
reset_mode( 0 );
set_cr_mode();
signal(SIGINT,ctrl_c_hander);
while ( 1 )
{
counts ++ ;
printf( " loop " );
for (i = counts;i > 0 ;i -- )
printf( " ! " );
printf( " \n " );
if (ac == 2 )
{
// if(atoi(av[1][0])==counts)
// exit(1);
}
sleep( 1 );
}
reset_mode( 1 );
}
void ctrl_c_hander()
{
puts( " do you want exit.(y/n)\n " );
if (tolower(getchar()) == ' y ' )
{
reset_mode( 1 );
exit( 1 );
}
return ;
}
void set_cr_mode()
{
struct termios setting;
tcgetattr( 0 , & setting);
setting.c_lflag &= ~ ICANON;
setting.c_lflag &= ~ ECHO;
setting.c_cc[VMIN] = 1 ;
tcsetattr( 0 ,TCSANOW, & setting);
}
void reset_mode( int flag)
{
static struct termios info;
// static int termflag;
static int does;
if (flag == 0 )
{
tcgetattr( 0 , & info);
// termflag=fcntl(0,F_GETFL);
does = 1 ;
}
else if (does == 1 )
{
// fcntl(0,F_SETFL,termflag);
tcsetattr( 0 ,TCSANOW, & info);
}
}