diff options
| author | Brent Cook <busterb@gmail.com> | 2025-04-12 16:59:51 -0500 |
|---|---|---|
| committer | Brent Cook <busterb@gmail.com> | 2025-04-12 16:59:51 -0500 |
| commit | 78b86a2af698d771a4db5286a4222c92d074a6da (patch) | |
| tree | 81bc88fb0a82bf580386ff68717b5b10381e5c23 | |
| parent | ec123956230a44603196fd68725e03a9ecf36df3 (diff) | |
| download | portable-78b86a2af698d771a4db5286a4222c92d074a6da.tar.gz portable-78b86a2af698d771a4db5286a4222c92d074a6da.tar.bz2 portable-78b86a2af698d771a4db5286a4222c92d074a6da.zip | |
switch to using high bit for fd detection of file/socket
| -rw-r--r-- | crypto/compat/posix_win.c | 47 | ||||
| -rw-r--r-- | include/compat/sys/stat.h | 8 |
2 files changed, 33 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 | |||
| 27 | static int | ||
| 28 | is_socket(int fd) | ||
| 29 | { | ||
| 30 | return (fd & 0x80000000) == 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | static int | ||
| 34 | get_real_fd(int fd) | ||
| 35 | { | ||
| 36 | return (fd & 0x7fffffff); | ||
| 37 | } | ||
| 38 | |||
| 25 | void | 39 | void |
| 26 | posix_perror(const char *s) | 40 | posix_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 | ||
| 46 | static int | 60 | int |
| 47 | oddify_fd(int fd) | 61 | posix_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 | ||
| 56 | int | 66 | int |
| @@ -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 | ||
| 78 | char * | 92 | char * |
| @@ -160,15 +174,6 @@ wsa_errno(int err) | |||
| 160 | return -1; | 174 | return -1; |
| 161 | } | 175 | } |
| 162 | 176 | ||
| 163 | static int | ||
| 164 | is_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 | |||
| 172 | int | 177 | int |
| 173 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | 178 | posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) |
| 174 | { | 179 | { |
| @@ -182,14 +187,13 @@ int | |||
| 182 | posix_close(int fd) | 187 | posix_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 | |||
| 198 | posix_read(int fd, void *buf, size_t count) | 202 | posix_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 | } |
diff --git a/include/compat/sys/stat.h b/include/compat/sys/stat.h index b88da1d..aa15c9a 100644 --- a/include/compat/sys/stat.h +++ b/include/compat/sys/stat.h | |||
| @@ -118,4 +118,12 @@ | |||
| 118 | 118 | ||
| 119 | #endif | 119 | #endif |
| 120 | 120 | ||
| 121 | #ifdef _WIN32 | ||
| 122 | int posix_fstat(int fd, struct stat *statbuf); | ||
| 123 | |||
| 124 | #ifndef NO_REDEF_POSIX_FUNCTIONS | ||
| 125 | #define fstat(fd, statbuf) posix_fstat(fd, statbuf) | ||
| 126 | #endif | ||
| 127 | #endif | ||
| 128 | |||
| 121 | #endif | 129 | #endif |
