#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <signal.h> #include <setjmp.h> sigjmp_buf env; void tfgets_handler(int sig) { signal(SIGALRM, SIG_DFL); siglongjmp(env, 1); } char *tfgets(char *buf, int bufsize, FILE *stream) { static const int TimeLimitSecs = 5; signal(SIGALRM, tfgets_handler) alarm(TimeLimitSecs); int rc = sigsetjmp(env, 1); if(rc == 0) return fgets(buf, bufsize, stream); else return NULL; //alarm,time out }