2 #if defined P9Y_NEED_BASENAME
4 char *basename(const char *filename
) {
5 static char base
[_MAX_PATH
* 2], ext
[_MAX_PATH
], *ptr
;
6 (void) _splitpath_s(filename
, NULL
, 0, NULL
, 0, base
, _MAX_PATH
, ext
, _MAX_PATH
);
7 strcat_s(base
, _MAX_PATH
* 2 - 1, ext
);
11 #endif // P9Y_NEED_BASENAME
13 #include "realpath.hpp"
14 #if defined P9Y_NEED_REALPATH
16 char *realpath(const char *path
, char real
[_MAX_PATH
]) {
17 return _fullpath(real
, path
, _MAX_PATH
);
20 #endif // P9Y_NEED_REALPATH
22 #include "gettimeofday.hpp"
23 #if defined P9Y_NEED_GETTIMEOFDAY
25 int gettimeofday(struct timeval
* tp
, struct timezone
*) {
26 using clock
= std::chrono::system_clock
;
27 auto as_sec
= [] (auto d
) {
28 return std::chrono::duration_cast
<std::chrono::seconds
>(d
);
30 auto as_usec
= [] (auto d
) {
31 return std::chrono::duration_cast
<std::chrono::microseconds
>(d
);
34 auto now
= clock::now().time_since_epoch();
35 auto sec
= as_sec(now
);
36 auto usec
= as_usec(now
- sec
);
38 tp
->tv_sec
= sec
.count();
39 tp
->tv_usec
= usec
.count();
42 #endif // P9Y_NEED_GETTIMEOFDAY
45 #if defined P9Y_NEED_GET_SOCKET_ERRNO
47 int get_socket_errno() {
48 int local_errno
= WSAGetLastError();
50 switch (local_errno
) {
52 local_errno
= EINPROGRESS
;
60 local_errno
= ECONNREFUSED
;
64 local_errno
= ENETUNREACH
;
68 local_errno
= ETIMEDOUT
;
72 local_errno
= ECONNRESET
;
76 local_errno
= EADDRINUSE
;
80 local_errno
= EOPNOTSUPP
;
84 local_errno
= ENOPROTOOPT
;
94 #endif // P9Y_NEED_GET_SOCKET_ERRNO
97 #if defined P9Y_NEED_POLL
98 int poll(struct pollfd fds
[], nfds_t nfds
, int tmo
) {
99 fd_set readfds
, writefds
, errorfds
;
106 for (nfds_t x
= 0; x
< nfds
; ++x
) {
107 if (fds
[x
].events
& (POLLIN
| POLLOUT
)) {
109 if (fds
[x
].fd
> maxfd
) {
113 if (fds
[x
].events
& POLLIN
) {
114 FD_SET(fds
[x
].fd
, &readfds
);
116 if (fds
[x
].events
& POLLOUT
) {
117 FD_SET(fds
[x
].fd
, &writefds
);
122 struct timeval timeout
= {tmo
/ 1000, (tmo
% 1000) * 1000};
123 struct timeval
*tp
= &timeout
;
127 int ret
= select(maxfd
+ 1, &readfds
, &writefds
, &errorfds
, tp
);
132 /* Iterate through all of them because I need to clear the revent map */
133 for (nfds_t x
= 0; x
< nfds
; ++x
) {
135 if (FD_ISSET(fds
[x
].fd
, &readfds
)) {
136 fds
[x
].revents
|= POLLIN
;
138 if (FD_ISSET(fds
[x
].fd
, &writefds
)) {
139 fds
[x
].revents
|= POLLOUT
;
141 if (FD_ISSET(fds
[x
].fd
, &errorfds
)) {
142 fds
[x
].revents
|= POLLERR
;
148 #endif // P9Y_NEED_POLL