1 /* This file contains functions which implement those POSIX and Linux functions
2 * that MinGW and Microsoft don't provide. The implementations contain just enough
3 * functionality to support fio.
7 #include <netinet/in.h>
16 #include <semaphore.h>
20 #include <sys/resource.h>
25 #include "../os-windows.h"
26 #include "../../lib/hweight.h"
28 extern unsigned long mtime_since_now(struct timespec *);
29 extern void fio_gettime(struct timespec *, void *);
31 /* These aren't defined in the MinGW headers */
32 HRESULT WINAPI StringCchCopyA(
37 HRESULT WINAPI StringCchPrintfA(
40 const char *pszFormat,
43 int win_to_posix_error(DWORD winerr)
47 case ERROR_FILE_NOT_FOUND: return ENOENT;
48 case ERROR_PATH_NOT_FOUND: return ENOENT;
49 case ERROR_ACCESS_DENIED: return EACCES;
50 case ERROR_INVALID_HANDLE: return EBADF;
51 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
52 case ERROR_INVALID_DATA: return EINVAL;
53 case ERROR_OUTOFMEMORY: return ENOMEM;
54 case ERROR_INVALID_DRIVE: return ENODEV;
55 case ERROR_NOT_SAME_DEVICE: return EXDEV;
56 case ERROR_WRITE_PROTECT: return EROFS;
57 case ERROR_BAD_UNIT: return ENODEV;
58 case ERROR_SHARING_VIOLATION: return EACCES;
59 case ERROR_LOCK_VIOLATION: return EACCES;
60 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
61 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
62 case ERROR_NOT_SUPPORTED: return ENOSYS;
63 case ERROR_FILE_EXISTS: return EEXIST;
64 case ERROR_CANNOT_MAKE: return EPERM;
65 case ERROR_INVALID_PARAMETER: return EINVAL;
66 case ERROR_NO_PROC_SLOTS: return EAGAIN;
67 case ERROR_BROKEN_PIPE: return EPIPE;
68 case ERROR_OPEN_FAILED: return EIO;
69 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
70 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
71 case ERROR_INVALID_NAME: return ENOENT;
72 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
73 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
74 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
75 case ERROR_SIGNAL_REFUSED: return EIO;
76 case ERROR_BAD_PATHNAME: return ENOENT;
77 case ERROR_SIGNAL_PENDING: return EBUSY;
78 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
79 case ERROR_BUSY: return EBUSY;
80 case ERROR_ALREADY_EXISTS: return EEXIST;
81 case ERROR_NO_SIGNAL_SENT: return EIO;
82 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
83 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
84 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
85 case ERROR_THREAD_1_INACTIVE: return EINVAL;
86 case ERROR_BAD_PIPE: return EINVAL;
87 case ERROR_PIPE_BUSY: return EBUSY;
88 case ERROR_NO_DATA: return EPIPE;
89 case ERROR_MORE_DATA: return EAGAIN;
90 case ERROR_DIRECTORY: return ENOTDIR;
91 case ERROR_PIPE_CONNECTED: return EBUSY;
92 case ERROR_NO_TOKEN: return EINVAL;
93 case ERROR_PROCESS_ABORTED: return EFAULT;
94 case ERROR_BAD_DEVICE: return ENODEV;
95 case ERROR_BAD_USERNAME: return EINVAL;
96 case ERROR_OPEN_FILES: return EAGAIN;
97 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
98 case ERROR_DEVICE_IN_USE: return EAGAIN;
99 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
100 case ERROR_IO_DEVICE: return EIO;
101 case ERROR_NOT_OWNER: return EPERM;
102 case ERROR_END_OF_MEDIA: return ENOSPC;
103 case ERROR_EOM_OVERFLOW: return ENOSPC;
104 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
105 case ERROR_SETMARK_DETECTED: return ESPIPE;
106 case ERROR_NO_DATA_DETECTED: return ENOSPC;
107 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
108 case ERROR_CRC: return EIO;
109 case ERROR_NEGATIVE_SEEK: return EINVAL;
110 case ERROR_DISK_FULL: return ENOSPC;
111 case ERROR_NOACCESS: return EFAULT;
112 case ERROR_FILE_INVALID: return ENXIO;
118 int GetNumLogicalProcessors(void)
120 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
122 DWORD num_processors = 0;
126 while (!GetLogicalProcessorInformation(processor_info, &len)) {
127 error = GetLastError();
128 if (error == ERROR_INSUFFICIENT_BUFFER)
129 processor_info = malloc(len);
131 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
135 if (processor_info == NULL) {
136 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
141 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
143 if (processor_info[i].Relationship == RelationProcessorCore)
144 num_processors += hweight64(processor_info[i].ProcessorMask);
147 free(processor_info);
148 return num_processors;
151 long sysconf(int name)
156 MEMORYSTATUSEX status;
160 case _SC_NPROCESSORS_ONLN:
161 val = GetNumLogicalProcessors();
163 log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
168 GetSystemInfo(&sysInfo);
169 val = sysInfo.dwPageSize;
173 status.dwLength = sizeof(status);
174 val2 = sysconf(_SC_PAGESIZE);
175 if (GlobalMemoryStatusEx(&status) && val2 != -1)
176 val = status.ullTotalPhys / val2;
178 log_err("sysconf(_SC_PHYS_PAGES) failed\n");
181 log_err("sysconf(%d) is not implemented\n", name);
188 char *dl_error = NULL;
190 int dlclose(void *handle)
192 return !FreeLibrary((HMODULE)handle);
195 void *dlopen(const char *file, int mode)
199 hMod = LoadLibrary(file);
200 if (hMod == INVALID_HANDLE_VALUE)
201 dl_error = (char*)"LoadLibrary failed";
208 void *dlsym(void *handle, const char *name)
212 fnPtr = GetProcAddress((HMODULE)handle, name);
214 dl_error = (char*)"GetProcAddress failed";
226 /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
227 void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
232 jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
233 utcFT.dwLowDateTime = (DWORD)jan1970;
234 utcFT.dwHighDateTime = jan1970 >> 32;
236 FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
239 char* ctime_r(const time_t *t, char *buf)
242 const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
243 const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
245 Time_tToSystemTime(*t, &systime);
246 /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
247 StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d\n", dayOfWeek[systime.wDayOfWeek % 7], monthOfYear[(systime.wMonth - 1) % 12],
248 systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
252 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
255 uint64_t unix_time, windows_time;
256 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
258 /* Ignore the timezone parameter */
262 * Windows time is stored as the number 100 ns intervals since January 1 1601.
263 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
264 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
266 GetSystemTimeAsFileTime(&fileTime);
267 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
268 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
269 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
270 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
271 tp->tv_sec = unix_time / 1000;
272 tp->tv_usec = (unix_time % 1000) * 1000;
276 int sigaction(int sig, const struct sigaction *act,
277 struct sigaction *oact)
280 void (*prev_handler)(int);
282 prev_handler = signal(sig, act->sa_handler);
284 oact->sa_handler = prev_handler;
286 if (prev_handler == SIG_ERR)
292 int lstat(const char * path, struct stat * buf)
294 return stat(path, buf);
297 void *mmap(void *addr, size_t len, int prot, int flags,
298 int fildes, off_t off)
305 void* allocAddr = NULL;
307 if (prot & PROT_NONE)
308 vaProt |= PAGE_NOACCESS;
310 if ((prot & PROT_READ) && !(prot & PROT_WRITE)) {
311 vaProt |= PAGE_READONLY;
312 mapAccess = FILE_MAP_READ;
315 if (prot & PROT_WRITE) {
316 vaProt |= PAGE_READWRITE;
317 mapAccess |= FILE_MAP_WRITE;
320 lenlow = len & 0xFFFF;
322 /* If the low DWORD is zero and the high DWORD is non-zero, `CreateFileMapping`
323 will return ERROR_INVALID_PARAMETER. To avoid this, set both to zero. */
328 if (flags & MAP_ANON || flags & MAP_ANONYMOUS)
330 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
331 if (allocAddr == NULL)
332 errno = win_to_posix_error(GetLastError());
336 hMap = CreateFileMapping((HANDLE)_get_osfhandle(fildes), NULL, vaProt, lenhigh, lenlow, NULL);
340 allocAddr = MapViewOfFile(hMap, mapAccess, off >> 16, off & 0xFFFF, len);
343 if (hMap == NULL || allocAddr == NULL)
344 errno = win_to_posix_error(GetLastError());
351 int munmap(void *addr, size_t len)
355 /* We may have allocated the memory with either MapViewOfFile or
356 VirtualAlloc. Therefore, try calling UnmapViewOfFile first, and if that
357 fails, call VirtualFree. */
358 success = UnmapViewOfFile(addr);
362 success = VirtualFree(addr, 0, MEM_RELEASE);
368 int msync(void *addr, size_t len, int flags)
370 return !FlushViewOfFile(addr, len);
375 log_err("%s is not implemented\n", __func__);
382 log_err("%s is not implemented\n", __func__);
387 static HANDLE log_file = INVALID_HANDLE_VALUE;
389 void openlog(const char *ident, int logopt, int facility)
391 if (log_file == INVALID_HANDLE_VALUE)
392 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
397 CloseHandle(log_file);
398 log_file = INVALID_HANDLE_VALUE;
401 void syslog(int priority, const char *message, ... /* argument */)
408 if (log_file == INVALID_HANDLE_VALUE) {
409 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
412 if (log_file == INVALID_HANDLE_VALUE) {
413 log_err("syslog: failed to open log file\n");
417 va_start(v, message);
418 len = _vscprintf(message, v);
419 output = malloc(len + sizeof(char));
420 vsprintf(output, message, v);
421 WriteFile(log_file, output, len, &bytes_written, NULL);
426 int kill(pid_t pid, int sig)
433 * This is assumed to be used only by the network code,
434 * and so doesn't try and handle any of the other cases
436 int fcntl(int fildes, int cmd, ...)
439 * non-blocking mode doesn't work the same as in BSD sockets,
444 int val, opt, status;
448 else if (cmd != F_SETFL) {
455 opt = va_arg(ap, int);
456 if (opt & O_NONBLOCK)
461 status = ioctlsocket((SOCKET)fildes, opt, &val);
463 if (status == SOCKET_ERROR) {
476 * Get the value of a local clock source.
477 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
478 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
480 int clock_gettime(clockid_t clock_id, struct timespec *tp)
484 if (clock_id == CLOCK_MONOTONIC)
486 static LARGE_INTEGER freq = {{0,0}};
487 LARGE_INTEGER counts;
490 QueryPerformanceCounter(&counts);
491 if (freq.QuadPart == 0)
492 QueryPerformanceFrequency(&freq);
494 tp->tv_sec = counts.QuadPart / freq.QuadPart;
495 /* Get the difference between the number of ns stored
496 * in 'tv_sec' and that stored in 'counts' */
497 t = tp->tv_sec * freq.QuadPart;
498 t = counts.QuadPart - t;
499 /* 't' now contains the number of cycles since the last second.
500 * We want the number of nanoseconds, so multiply out by 1,000,000,000
501 * and then divide by the frequency. */
503 tp->tv_nsec = t / freq.QuadPart;
505 else if (clock_id == CLOCK_REALTIME)
507 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
508 * higher-precision field. */
510 gettimeofday(&tv, NULL);
511 tp->tv_sec = tv.tv_sec;
512 tp->tv_nsec = tv.tv_usec * 1000;
521 int mlock(const void * addr, size_t len)
525 HANDLE process = GetCurrentProcess();
527 success = GetProcessWorkingSetSize(process, &min, &max);
529 errno = win_to_posix_error(GetLastError());
535 success = SetProcessWorkingSetSize(process, min, max);
537 errno = win_to_posix_error(GetLastError());
541 success = VirtualLock((LPVOID)addr, len);
543 errno = win_to_posix_error(GetLastError());
550 int munlock(const void * addr, size_t len)
552 BOOL success = VirtualUnlock((LPVOID)addr, len);
554 errno = win_to_posix_error(GetLastError());
561 pid_t waitpid(pid_t pid, int *stat_loc, int options)
563 log_err("%s is not implemented\n", __func__);
568 int usleep(useconds_t useconds)
570 Sleep(useconds / 1000);
574 char *basename(char *path)
576 static char name[MAX_PATH];
579 if (path == NULL || strlen(path) == 0)
582 i = strlen(path) - 1;
584 while (path[i] != '\\' && path[i] != '/' && i >= 0)
587 name[MAX_PATH - 1] = '\0';
588 strncpy(name, path + i + 1, MAX_PATH - 1);
593 int fsync(int fildes)
595 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
596 if (!FlushFileBuffers(hFile)) {
597 errno = win_to_posix_error(GetLastError());
604 int nFileMappings = 0;
605 HANDLE fileMappings[1024];
607 int shmget(key_t key, size_t size, int shmflg)
610 uint32_t size_low = size & 0xFFFFFFFF;
611 uint32_t size_high = ((uint64_t)size) >> 32;
612 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL);
613 if (hMapping != NULL) {
614 fileMappings[nFileMappings] = hMapping;
615 mapid = nFileMappings;
624 void *shmat(int shmid, const void *shmaddr, int shmflg)
627 MEMORY_BASIC_INFORMATION memInfo;
628 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
629 if (mapAddr == NULL) {
630 errno = win_to_posix_error(GetLastError());
634 if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) {
635 errno = win_to_posix_error(GetLastError());
639 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
640 if (mapAddr == NULL) {
641 errno = win_to_posix_error(GetLastError());
648 int shmdt(const void *shmaddr)
650 if (!UnmapViewOfFile(shmaddr)) {
651 errno = win_to_posix_error(GetLastError());
658 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
660 if (cmd == IPC_RMID) {
661 fileMappings[shmid] = INVALID_HANDLE_VALUE;
664 log_err("%s is not implemented\n", __func__);
670 int setuid(uid_t uid)
672 log_err("%s is not implemented\n", __func__);
677 int setgid(gid_t gid)
679 log_err("%s is not implemented\n", __func__);
686 DWORD prioclass = NORMAL_PRIORITY_CLASS;
689 prioclass = HIGH_PRIORITY_CLASS;
691 prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
693 prioclass = IDLE_PRIORITY_CLASS;
695 prioclass = BELOW_NORMAL_PRIORITY_CLASS;
697 if (!SetPriorityClass(GetCurrentProcess(), prioclass))
698 log_err("fio: SetPriorityClass failed\n");
703 int getrusage(int who, struct rusage *r_usage)
705 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
706 FILETIME cTime, eTime, kTime, uTime;
710 memset(r_usage, 0, sizeof(*r_usage));
712 if (who == RUSAGE_SELF) {
713 h = GetCurrentProcess();
714 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
715 } else if (who == RUSAGE_THREAD) {
716 h = GetCurrentThread();
717 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
719 log_err("fio: getrusage %d is not implemented\n", who);
723 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
724 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
726 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
727 r_usage->ru_utime.tv_sec = time;
728 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
729 r_usage->ru_utime.tv_usec = 0;
730 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
731 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
733 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
734 r_usage->ru_stime.tv_sec = time;
735 r_usage->ru_stime.tv_usec = 0;
739 int posix_madvise(void *addr, size_t len, int advice)
744 int fdatasync(int fildes)
746 return fsync(fildes);
749 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
752 int64_t pos = _telli64(fildes);
753 ssize_t len = _write(fildes, buf, nbyte);
754 _lseeki64(fildes, pos, SEEK_SET);
758 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
760 int64_t pos = _telli64(fildes);
761 ssize_t len = read(fildes, buf, nbyte);
762 _lseeki64(fildes, pos, SEEK_SET);
766 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
768 log_err("%s is not implemented\n", __func__);
773 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
776 DWORD bytes_written = 0;
777 for (i = 0; i < iovcnt; i++)
779 int len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
780 if (len == SOCKET_ERROR)
782 DWORD err = GetLastError();
783 errno = win_to_posix_error(err);
787 bytes_written += len;
790 return bytes_written;
793 long long strtoll(const char *restrict str, char **restrict endptr,
796 return _strtoi64(str, endptr, base);
799 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
802 struct timeval *to = NULL;
803 fd_set readfds, writefds, exceptfds;
809 to->tv_sec = timeout / 1000;
810 to->tv_usec = (timeout % 1000) * 1000;
817 for (i = 0; i < nfds; i++)
824 if (fds[i].events & POLLIN)
825 FD_SET(fds[i].fd, &readfds);
827 if (fds[i].events & POLLOUT)
828 FD_SET(fds[i].fd, &writefds);
830 FD_SET(fds[i].fd, &exceptfds);
832 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
834 if (rc != SOCKET_ERROR) {
835 for (i = 0; i < nfds; i++)
841 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
842 fds[i].revents |= POLLIN;
844 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
845 fds[i].revents |= POLLOUT;
847 if (FD_ISSET(fds[i].fd, &exceptfds))
848 fds[i].revents |= POLLHUP;
854 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
858 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
863 ms_remaining = ms_total;
865 /* Since Sleep() can sleep for less than the requested time, add a loop to
866 ensure we only return after the requested length of time has elapsed */
868 fio_gettime(&tv, NULL);
870 ms_remaining = ms_total - mtime_since_now(&tv);
871 } while (ms_remaining > 0 && ms_remaining < ms_total);
873 /* this implementation will never sleep for less than the requested time */
882 DIR *opendir(const char *dirname)
884 struct dirent_ctx *dc = NULL;
886 /* See if we can open it. If not, we'll return an error here */
887 HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
888 if (file != INVALID_HANDLE_VALUE) {
890 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
891 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
892 dc->find_handle = INVALID_HANDLE_VALUE;
894 DWORD error = GetLastError();
895 if (error == ERROR_FILE_NOT_FOUND)
898 else if (error == ERROR_PATH_NOT_FOUND)
900 else if (error == ERROR_TOO_MANY_OPEN_FILES)
902 else if (error == ERROR_ACCESS_DENIED)
911 int closedir(DIR *dirp)
913 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
914 FindClose(dirp->find_handle);
920 struct dirent *readdir(DIR *dirp)
922 static struct dirent de;
923 WIN32_FIND_DATA find_data;
928 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
929 char search_pattern[MAX_PATH];
930 StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
931 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
932 if (dirp->find_handle == INVALID_HANDLE_VALUE)
935 if (!FindNextFile(dirp->find_handle, &find_data))
939 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
947 log_err("%s is not implemented\n", __func__);
952 in_addr_t inet_network(const char *cp)
955 in_addr_t nbo = inet_addr(cp);
956 hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
960 const char* inet_ntop(int af, const void *restrict src,
961 char *restrict dst, socklen_t size)
963 INT status = SOCKET_ERROR;
967 if (af != AF_INET && af != AF_INET6) {
968 errno = EAFNOSUPPORT;
972 WSAStartup(MAKEWORD(2,2), &wsd);
975 struct sockaddr_in si;
977 memset(&si, 0, sizeof(si));
979 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
980 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
981 } else if (af == AF_INET6) {
982 struct sockaddr_in6 si6;
984 memset(&si6, 0, sizeof(si6));
985 si6.sin6_family = af;
986 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
987 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
990 if (status != SOCKET_ERROR)
1000 int inet_pton(int af, const char *restrict src, void *restrict dst)
1002 INT status = SOCKET_ERROR;
1006 if (af != AF_INET && af != AF_INET6) {
1007 errno = EAFNOSUPPORT;
1011 WSAStartup(MAKEWORD(2,2), &wsd);
1013 if (af == AF_INET) {
1014 struct sockaddr_in si;
1015 INT len = sizeof(si);
1016 memset(&si, 0, sizeof(si));
1018 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
1019 if (status != SOCKET_ERROR)
1020 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
1021 } else if (af == AF_INET6) {
1022 struct sockaddr_in6 si6;
1023 INT len = sizeof(si6);
1024 memset(&si6, 0, sizeof(si6));
1025 si6.sin6_family = af;
1026 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
1027 if (status != SOCKET_ERROR)
1028 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
1031 if (status == SOCKET_ERROR) {