aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2015-06-05 04:31:56 -0500
committerBrent Cook <bcook@openbsd.org>2015-06-05 04:50:18 -0500
commitb4a6a615134b59435efbecdd8c5b5407b6af8e8f (patch)
tree7e1b645be3b352fab0021da3b0e43e78a423347a /include
parent1d27b22e82ce00d27d0886c8488e4cbed1cb618e (diff)
downloadportable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.tar.gz
portable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.tar.bz2
portable-b4a6a615134b59435efbecdd8c5b5407b6af8e8f.zip
refactor win32 shims into posix_win.c
this also adds a rename shim that allows overwrites
Diffstat (limited to 'include')
-rw-r--r--include/stdio.h31
-rw-r--r--include/win32netcompat.h137
2 files changed, 18 insertions, 150 deletions
diff --git a/include/stdio.h b/include/stdio.h
index db369c9..76bd9da 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -15,35 +15,16 @@ int asprintf(char **str, const char *fmt, ...);
15#endif 15#endif
16 16
17#ifdef _WIN32 17#ifdef _WIN32
18#include <errno.h>
19#include <string.h>
20 18
21static inline void 19void posix_perror(const char *s);
22posix_perror(const char *s) 20FILE * posix_fopen(const char *path, const char *mode);
23{ 21int posix_rename(const char *oldpath, const char *newpath);
24 fprintf(stderr, "%s: %s\n", s, strerror(errno));
25}
26 22
23#ifndef NO_REDEF_POSIX_FUNCTIONS
27#define perror(errnum) posix_perror(errnum) 24#define perror(errnum) posix_perror(errnum)
28
29static inline FILE *
30posix_fopen(const char *path, const char *mode)
31{
32 char *bin_mode = mode;
33 if (strchr(mode, 'b') == NULL) {
34 bin_mode = NULL;
35 if (asprintf(&bin_mode, "%sb", mode) == -1)
36 return NULL;
37 fprintf(stderr, "opening bin file %s\n", bin_mode);
38 }
39
40 FILE *f = fopen(path, bin_mode);
41 if (bin_mode != mode)
42 free(bin_mode);
43 return f;
44}
45
46#define fopen(path, mode) posix_fopen(path, mode) 25#define fopen(path, mode) posix_fopen(path, mode)
26#define rename(oldpath, newpath) posix_rename(oldpath, newpath)
27#endif
47 28
48#endif 29#endif
49 30
diff --git a/include/win32netcompat.h b/include/win32netcompat.h
index 51384d0..452cfba 100644
--- a/include/win32netcompat.h
+++ b/include/win32netcompat.h
@@ -19,142 +19,29 @@
19#include <errno.h> 19#include <errno.h>
20#include <unistd.h> 20#include <unistd.h>
21 21
22static int 22int posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
23wsa_errno(int err)
24{
25 switch (err) {
26 case WSAENOBUFS:
27 errno = ENOMEM;
28 break;
29 case WSAEACCES:
30 errno = EACCES;
31 break;
32 case WSANOTINITIALISED:
33 errno = EPERM;
34 break;
35 case WSAEHOSTUNREACH:
36 case WSAENETDOWN:
37 errno = EIO;
38 break;
39 case WSAEFAULT:
40 errno = EFAULT;
41 break;
42 case WSAEINTR:
43 errno = EINTR;
44 break;
45 case WSAEINVAL:
46 errno = EINVAL;
47 break;
48 case WSAEINPROGRESS:
49 errno = EINPROGRESS;
50 break;
51 case WSAEWOULDBLOCK:
52 errno = EAGAIN;
53 break;
54 case WSAEOPNOTSUPP:
55 errno = ENOTSUP;
56 break;
57 case WSAEMSGSIZE:
58 errno = EFBIG;
59 break;
60 case WSAENOTSOCK:
61 errno = ENOTSOCK;
62 break;
63 case WSAENOPROTOOPT:
64 errno = ENOPROTOOPT;
65 break;
66 case WSAECONNREFUSED:
67 errno = ECONNREFUSED;
68 break;
69 case WSAEAFNOSUPPORT:
70 errno = EAFNOSUPPORT;
71 break;
72 case WSAENETRESET:
73 case WSAENOTCONN:
74 case WSAECONNABORTED:
75 case WSAECONNRESET:
76 case WSAESHUTDOWN:
77 case WSAETIMEDOUT:
78 errno = EPIPE;
79 break;
80 }
81 return -1;
82}
83 23
84static inline int 24int posix_close(int fd);
85posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 25ssize_t posix_read(int fd, void *buf, size_t count);
86{
87 int rc = connect(sockfd, addr, addrlen);
88 if (rc == SOCKET_ERROR)
89 return wsa_errno(WSAGetLastError());
90 return rc;
91}
92 26
93#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen) 27ssize_t posix_write(int fd, const void *buf, size_t count);
94
95static inline int
96posix_close(int fd)
97{
98 if (closesocket(fd) == SOCKET_ERROR) {
99 int err = WSAGetLastError();
100 return err == WSAENOTSOCK ?
101 close(fd) : wsa_errno(err);
102 }
103 return 0;
104}
105 28
106#define close(fd) posix_close(fd) 29int posix_getsockopt(int sockfd, int level, int optname,
30 void *optval, socklen_t *optlen);
107 31
108static inline ssize_t 32int posix_setsockopt(int sockfd, int level, int optname,
109posix_read(int fd, void *buf, size_t count) 33 const void *optval, socklen_t optlen);
110{
111 ssize_t rc = recv(fd, buf, count, 0);
112 if (rc == SOCKET_ERROR) {
113 int err = WSAGetLastError();
114 return err == WSAENOTSOCK ?
115 read(fd, buf, count) : wsa_errno(err);
116 }
117 return rc;
118}
119 34
35#ifndef NO_REDEF_POSIX_FUNCTIONS
36#define connect(sockfd, addr, addrlen) posix_connect(sockfd, addr, addrlen)
37#define close(fd) posix_close(fd)
120#define read(fd, buf, count) posix_read(fd, buf, count) 38#define read(fd, buf, count) posix_read(fd, buf, count)
121
122static inline ssize_t
123posix_write(int fd, const void *buf, size_t count)
124{
125 ssize_t rc = send(fd, buf, count, 0);
126 if (rc == SOCKET_ERROR) {
127 int err = WSAGetLastError();
128 return err == WSAENOTSOCK ?
129 write(fd, buf, count) : wsa_errno(err);
130 }
131 return rc;
132}
133
134#define write(fd, buf, count) posix_write(fd, buf, count) 39#define write(fd, buf, count) posix_write(fd, buf, count)
135
136static inline int
137posix_getsockopt(int sockfd, int level, int optname,
138 void *optval, socklen_t *optlen)
139{
140 int rc = getsockopt(sockfd, level, optname, (char *)optval, optlen);
141 return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
142
143}
144
145#define getsockopt(sockfd, level, optname, optval, optlen) \ 40#define getsockopt(sockfd, level, optname, optval, optlen) \
146 posix_getsockopt(sockfd, level, optname, optval, optlen) 41 posix_getsockopt(sockfd, level, optname, optval, optlen)
147
148static inline int
149posix_setsockopt(int sockfd, int level, int optname,
150 const void *optval, socklen_t optlen)
151{
152 int rc = setsockopt(sockfd, level, optname, (char *)optval, optlen);
153 return rc == 0 ? 0 : wsa_errno(WSAGetLastError());
154}
155
156#define setsockopt(sockfd, level, optname, optval, optlen) \ 42#define setsockopt(sockfd, level, optname, optval, optlen) \
157 posix_setsockopt(sockfd, level, optname, optval, optlen) 43 posix_setsockopt(sockfd, level, optname, optval, optlen)
44#endif
158 45
159#endif 46#endif
160 47