9 int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
11 struct timeval timeout, *toptr;
12 fd_set ifds, ofds, efds, *ip, *op;
15 /* Set up the file-descriptor sets in ifds, ofds and efds. */
19 for (i = 0, op = ip = 0; i < nfds; ++i) {
21 if(fds[i].events & (POLLIN|POLLPRI)) {
23 FD_SET(fds[i].fd, ip);
25 if(fds[i].events & POLLOUT) {
27 FD_SET(fds[i].fd, op);
29 FD_SET(fds[i].fd, &efds);
32 /* Set up the timeval structure for the timeout parameter */
37 timeout.tv_sec = timo / 1000;
38 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
42 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
43 (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
45 rc = select(0, ip, op, &efds, toptr);
47 printf("Exiting select rc=%d\n", rc);
54 for (i = 0; i < nfds; ++i) {
56 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
57 fds[i].revents |= POLLIN;
58 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
59 fds[i].revents |= POLLOUT;
60 if(FD_ISSET(fd, &efds))
61 /* Some error was detected ... should be some way to know. */
62 fds[i].revents |= POLLHUP;
64 printf("%d %d %d revent = %x\n",
65 FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
74 set_connect_errno(int winsock_err)
89 set_socket_errno(int winsock_err)
101 * A wrapper around the socket() function. The purpose of this wrapper
102 * is to ensure that the global errno symbol is set if an error occurs,
103 * even if we are using winsock.
106 win32_socket(int domain, int type, int protocol)
108 SOCKET fd = socket(domain, type, protocol);
109 if(fd == INVALID_SOCKET) {
110 set_socket_errno(WSAGetLastError());
115 * A wrapper around the connect() function. The purpose of this wrapper
116 * is to ensure that the global errno symbol is set if an error occurs,
117 * even if we are using winsock.
120 win32_connect(SOCKET fd, struct sockaddr *addr, socklen_t addr_len)
122 int rc = connect(fd, addr, addr_len);
123 assert(rc == 0 || rc == SOCKET_ERROR);
124 if(rc == SOCKET_ERROR) {
125 set_connect_errno(WSAGetLastError());
131 * A wrapper around the accept() function. The purpose of this wrapper
132 * is to ensure that the global errno symbol is set if an error occurs,
133 * even if we are using winsock.
136 win32_accept(SOCKET fd, struct sockaddr *addr, socklen_t *addr_len)
138 SOCKET newfd = accept(fd, addr, addr_len);
139 if(newfd == INVALID_SOCKET) {
140 set_socket_errno(WSAGetLastError());
147 * A wrapper around the shutdown() function. The purpose of this wrapper
148 * is to ensure that the global errno symbol is set if an error occurs,
149 * even if we are using winsock.
152 win32_shutdown(SOCKET fd, int mode)
154 int rc = shutdown(fd, mode);
155 assert(rc == 0 || rc == SOCKET_ERROR);
156 if(rc == SOCKET_ERROR) {
157 set_socket_errno(WSAGetLastError());
161 int win32_close_socket(SOCKET fd) {
164 rc = closesocket(fd);
169 int win32_write_socket(SOCKET fd, void *buf, int n)
171 int rc = send(fd, buf, n, 0);
172 if(rc == SOCKET_ERROR) {
173 set_socket_errno(WSAGetLastError());
178 int win32_read_socket(SOCKET fd, void *buf, int n)
180 int rc = recv(fd, buf, n, 0);
181 if(rc == SOCKET_ERROR) {
182 set_socket_errno(WSAGetLastError());
188 char * win32_strtok_r(char *s, const char *delim, char **lasts)
190 register char *spanp;
195 if (s == NULL && (s = *lasts) == NULL)
199 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
203 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
208 if (c == 0) { /* no non-delimiter characters */
215 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
216 * Note that delim must have one NUL; we stop if we see that, too.
220 spanp = (char *)delim;
222 if ((sc = *spanp++) == c) {
235 char *win32_strsep (char **stringp, const char *delim)
238 register const char *spanp;
242 if ((s = *stringp) == NULL)
248 if ((sc = *spanp++) == c) {