1 #include "syshdrs.h"
2
3 /*
4 * Return zero if the operation timed-out or erred-out, otherwise non-zero.
5 */
6 int
SWaitUntilReadyForReading(const int sfd,const int tlen)7 SWaitUntilReadyForReading(const int sfd, const int tlen)
8 {
9 fd_set ss, ss2;
10 struct timeval tv;
11 int result;
12 int tleft;
13 time_t now, done;
14
15 if (sfd < 0) {
16 errno = EBADF;
17 return (0);
18 }
19
20 time(&now);
21 done = now + tlen;
22 tleft = tlen;
23
24 forever {
25 FD_ZERO(&ss);
26 FD_SET(sfd, &ss);
27 ss2 = ss;
28 tv.tv_sec = tleft;
29 tv.tv_usec = 0;
30 result = select(sfd + 1, SELECT_TYPE_ARG234 &ss, NULL, SELECT_TYPE_ARG234 &ss2, &tv);
31 if (result == 1) {
32 /* ready */
33 return (1);
34 } else if (result < 0) {
35 if (errno != EINTR) {
36 /* error */
37 return (0);
38 }
39 /* try again */
40 time(&now);
41 if (now > done) {
42 /* timed-out */
43 errno = ETIMEDOUT;
44 return (0);
45 }
46 tleft = (int) (done - now);
47 } else {
48 /* timed-out */
49 errno = ETIMEDOUT;
50 return (0);
51 }
52 }
53 } /* SWaitUntilReadyForReading */
54
55
56
57
58 /*
59 * Return zero if the operation timed-out or erred-out, otherwise non-zero.
60 */
61 int
SWaitUntilReadyForWriting(const int sfd,const int tlen)62 SWaitUntilReadyForWriting(const int sfd, const int tlen)
63 {
64 fd_set ss, ss2;
65 struct timeval tv;
66 int result;
67 int tleft;
68 time_t now, done;
69
70 if (sfd < 0) {
71 errno = EBADF;
72 return (0);
73 }
74
75 time(&now);
76 done = now + tlen;
77 tleft = tlen;
78
79 forever {
80 FD_ZERO(&ss);
81 FD_SET(sfd, &ss);
82 ss2 = ss;
83 tv.tv_sec = tleft;
84 tv.tv_usec = 0;
85 result = select(sfd + 1, NULL, SELECT_TYPE_ARG234 &ss, SELECT_TYPE_ARG234 &ss2, &tv);
86 if (result == 1) {
87 /* ready */
88 return (1);
89 } else if (result < 0) {
90 if (errno != EINTR) {
91 /* error */
92 return (0);
93 }
94 /* try again */
95 time(&now);
96 if (now > done) {
97 /* timed-out */
98 errno = ETIMEDOUT;
99 return (0);
100 }
101 tleft = (int) (done - now);
102 } else {
103 /* timed-out */
104 errno = ETIMEDOUT;
105 return (0);
106 }
107 }
108 } /* SWaitUntilReadyForWriting */
109