📄 trdwr.c
字号:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/select.h>
#include <signal.h>
static int TimeOut;
static int RestTime;
static void ( *OldAlarm )();
static void SetAlarm( int Seconds )
{
void SetTimeOut();
TimeOut = 0;
OldAlarm = signal( SIGALRM, SetTimeOut );
RestTime = alarm( Seconds );
}
static void ResetAlarm()
{
alarm( RestTime );
signal( SIGALRM, OldAlarm );
}
static void SetTimeOut()
{
ResetAlarm();
TimeOut = 1;
}
static int TimedIO( int (*IO)(), int Seconds, int Handle, char *Data, int Size )
{
int l, s;
fd_set f;
struct timeval t;
FD_ZERO( &f );
FD_SET( Handle, &f );
t.tv_sec = 0;
t.tv_usec = 1;
if( Seconds > 0 )
SetAlarm( Seconds );
s = 0;
while( Size > 0 ){
if( Seconds > 0 )
if( TimeOut )
break;
if( Seconds != 0 )
select( 0, &f, 0, 0, &t );
l = ( *IO )( Handle, Data, Size );
if( l <= 0 )
break;
s += l;
if( Seconds == 0 )
break;
Size -= l;
Data += l;
}
if( Seconds > 0 && !TimeOut )
ResetAlarm();
if( s == 0 && l <= 0 && ( Seconds <= 0 || !TimeOut ) )
return( -1 );
return( s );
}
int TimedRead( int Seconds, int Handle, char *Data, int Size )
{
return( TimedIO( read, Seconds, Handle, Data, Size ) );
}
int TimedWrite( int Seconds, int Handle, char *Data, int Size )
{
return( TimedIO( write, Seconds, Handle, Data, Size ) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -