aboutsummaryrefslogtreecommitdiff
path: root/crypto/compat/posix_win.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/compat/posix_win.c')
-rw-r--r--crypto/compat/posix_win.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/crypto/compat/posix_win.c b/crypto/compat/posix_win.c
index 9409af5..ea229c9 100644
--- a/crypto/compat/posix_win.c
+++ b/crypto/compat/posix_win.c
@@ -22,6 +22,20 @@
22#include <string.h> 22#include <string.h>
23#include <unistd.h> 23#include <unistd.h>
24 24
25#include <sys/stat.h>
26
27static int
28is_socket(int fd)
29{
30 return (fd & 0x80000000) == 0;
31}
32
33static int
34get_real_fd(int fd)
35{
36 return (fd & 0x7fffffff);
37}
38
25void 39void
26posix_perror(const char *s) 40posix_perror(const char *s)
27{ 41{
@@ -43,14 +57,10 @@ posix_fopen(const char *path, const char *mode)
43 return fopen(path, mode); 57 return fopen(path, mode);
44} 58}
45 59
46static int 60int
47oddify_fd(int fd) 61posix_fstat(int fd, struct stat *statbuf)
48{ 62{
49 if (fd & 1) /* also catches an eventual -1 from using up all descriptors */ 63 return fstat(get_real_fd(fd), statbuf);
50 return fd;
51 int clone = oddify_fd(dup(fd));
52 close(fd);
53 return clone;
54} 64}
55 65
56int 66int
@@ -72,7 +82,11 @@ posix_open(const char *path, ...)
72 flags |= O_NOINHERIT; 82 flags |= O_NOINHERIT;
73 } 83 }
74 flags &= ~O_NONBLOCK; 84 flags &= ~O_NONBLOCK;
75 return oddify_fd(open(path, flags, mode)); 85
86 const int fh = open(path, flags, mode);
87
88 // Set high bit to mark file descriptor as a file handle
89 return fh + 0x80000000;
76} 90}
77 91
78char * 92char *
@@ -160,15 +174,6 @@ wsa_errno(int err)
160 return -1; 174 return -1;
161} 175}
162 176
163static int
164is_socket(int fd)
165{
166 /* Border case: Don't break std* file descriptors */
167 if (fd < 3)
168 return 0;
169 return (fd & 1) == 0; /* daringly assumes that any valid socket is even */
170}
171
172int 177int
173posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 178posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
174{ 179{
@@ -182,14 +187,13 @@ int
182posix_close(int fd) 187posix_close(int fd)
183{ 188{
184 int rc; 189 int rc;
185
186 if (is_socket(fd)) { 190 if (is_socket(fd)) {
187 if ((rc = closesocket(fd)) == SOCKET_ERROR) { 191 if ((rc = closesocket(fd)) == SOCKET_ERROR) {
188 int err = WSAGetLastError(); 192 int err = WSAGetLastError();
189 rc = wsa_errno(err); 193 rc = wsa_errno(err);
190 } 194 }
191 } else { 195 } else {
192 rc = close(fd); 196 rc = close(get_real_fd(fd));
193 } 197 }
194 return rc; 198 return rc;
195} 199}
@@ -198,14 +202,13 @@ ssize_t
198posix_read(int fd, void *buf, size_t count) 202posix_read(int fd, void *buf, size_t count)
199{ 203{
200 ssize_t rc; 204 ssize_t rc;
201
202 if (is_socket(fd)) { 205 if (is_socket(fd)) {
203 if ((rc = recv(fd, buf, count, 0)) == SOCKET_ERROR) { 206 if ((rc = recv(fd, buf, count, 0)) == SOCKET_ERROR) {
204 int err = WSAGetLastError(); 207 int err = WSAGetLastError();
205 rc = wsa_errno(err); 208 rc = wsa_errno(err);
206 } 209 }
207 } else { 210 } else {
208 rc = read(fd, buf, count); 211 rc = read(get_real_fd(fd), buf, count);
209 } 212 }
210 return rc; 213 return rc;
211} 214}
@@ -219,7 +222,7 @@ posix_write(int fd, const void *buf, size_t count)
219 rc = wsa_errno(WSAGetLastError()); 222 rc = wsa_errno(WSAGetLastError());
220 } 223 }
221 } else { 224 } else {
222 rc = write(fd, buf, count); 225 rc = write(get_real_fd(fd), buf, count);
223 } 226 }
224 return rc; 227 return rc;
225} 228}