23,121
社区成员
发帖
与我相关
我的任务
分享
#include <unistd.h> // for read and write
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MSG_TRY "try again\n"
int main( void )
{
char buf[10];
int fd , readn;
fd = open( "/dev/tty" , O_RDONLY | O_NONBLOCK );
if( fd < 0 )
{
perror( "open /dev/tty" );
exit(1);
}
tryagain:
readn = read( fd , buf , 10 );
if( readn < 0 )
{
if( errno == EAGAIN )
{
sleep(3);
write( STDOUT_FILENO , MSG_TRY , strlen(MSG_TRY) );
goto tryagain;
}
else
{
perror("read /dev/tty");
exit(1);
}
}
write( STDOUT_FILENO , buf , readn );
close(fd);
return 0;
}