2 #if defined P9Y_NEED_BASENAME
5 char *basename(const char *filename
) {
6 static char base
[_MAX_PATH
* 2], ext
[_MAX_PATH
], *ptr
;
7 (void) _splitpath_s(filename
, NULL
, 0, NULL
, 0, base
, _MAX_PATH
, ext
, _MAX_PATH
);
8 strcat_s(base
, _MAX_PATH
* 2 - 1, ext
);
12 #endif // P9Y_NEED_BASENAME
14 #include "realpath.hpp"
15 #if defined P9Y_NEED_REALPATH
17 char *realpath(const char *path
, char real
[_MAX_PATH
]) {
18 return _fullpath(real
, path
, _MAX_PATH
);
21 #endif // P9Y_NEED_REALPATH
23 #include "gettimeofday.hpp"
24 #if defined P9Y_NEED_GETTIMEOFDAY
26 int gettimeofday(struct timeval
* tp
, struct timezone
*) {
27 using clock
= std::chrono::system_clock
;
28 auto as_sec
= [] (auto d
) {
29 return std::chrono::duration_cast
<std::chrono::seconds
>(d
);
31 auto as_usec
= [] (auto d
) {
32 return std::chrono::duration_cast
<std::chrono::microseconds
>(d
);
35 auto now
= clock::now().time_since_epoch();
36 auto sec
= as_sec(now
);
37 auto usec
= as_usec(now
- sec
);
39 tp
->tv_sec
= sec
.count();
40 tp
->tv_usec
= usec
.count();
43 #endif // P9Y_NEED_GETTIMEOFDAY
46 #if defined P9Y_NEED_GET_SOCKET_ERRNO
48 int get_socket_errno() {
49 int local_errno
= WSAGetLastError();
51 switch (local_errno
) {
53 local_errno
= EINPROGRESS
;
61 local_errno
= ECONNREFUSED
;
65 local_errno
= ENETUNREACH
;
69 local_errno
= ETIMEDOUT
;
73 local_errno
= ECONNRESET
;
77 local_errno
= EADDRINUSE
;
81 local_errno
= EOPNOTSUPP
;
85 local_errno
= ENOPROTOOPT
;
95 #endif // P9Y_NEED_GET_SOCKET_ERRNO
98 #if defined P9Y_NEED_POLL
99 int poll(struct pollfd fds
[], nfds_t nfds
, int tmo
) {
100 fd_set readfds
, writefds
, errorfds
;
107 for (nfds_t x
= 0; x
< nfds
; ++x
) {
108 if (fds
[x
].events
& (POLLIN
| POLLOUT
)) {
110 if (fds
[x
].fd
> maxfd
) {
114 if (fds
[x
].events
& POLLIN
) {
115 FD_SET(fds
[x
].fd
, &readfds
);
117 if (fds
[x
].events
& POLLOUT
) {
118 FD_SET(fds
[x
].fd
, &writefds
);
123 struct timeval timeout
= {tmo
/ 1000, (tmo
% 1000) * 1000};
124 struct timeval
*tp
= &timeout
;
128 int ret
= select(maxfd
+ 1, &readfds
, &writefds
, &errorfds
, tp
);
133 /* Iterate through all of them because I need to clear the revent map */
134 for (nfds_t x
= 0; x
< nfds
; ++x
) {
136 if (FD_ISSET(fds
[x
].fd
, &readfds
)) {
137 fds
[x
].revents
|= POLLIN
;
139 if (FD_ISSET(fds
[x
].fd
, &writefds
)) {
140 fds
[x
].revents
|= POLLOUT
;
142 if (FD_ISSET(fds
[x
].fd
, &errorfds
)) {
143 fds
[x
].revents
|= POLLERR
;
149 #endif // P9Y_NEED_POLL