-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.c
More file actions
48 lines (43 loc) · 773 Bytes
/
Copy pathselect.c
File metadata and controls
48 lines (43 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define TIMEOUT 5
#define BUF_LEN 1024
int main(void)
{
struct timeval tv;
fd_set readfds;
int ret;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO,&readfds);
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
ret = select(STDIN_FILENO+1,
&readfds,
NULL,
NULL,
&tv);
if(ret == -1){
perror("select");
return 1;
}else if(!ret){
printf("%d seconds elapsed.\n",TIMEOUT);
return 0;
}
if(FD_ISSET(STDIN_FILENO,&readfds)){
char buf[BUF_LEN+1];
int len;
len = read(STDIN_FILENO,buf,BUF_LEN);
if(len == -1){
perror("read");
}
if(len){
buf[len] = '\0';
printf("read: %s\n",buf);
}
return 0;
}
fprintf(stderr,"This is should not happen!\n");
return 1;
}