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 timeval *);
29 extern void fio_gettime(struct timeval *, void *);
31 /* These aren't defined in the MinGW headers */
32 HRESULT WINAPI StringCchCopyA(
37 HRESULT WINAPI StringCchPrintfA(
40 const char *pszFormat,
45 size_t numberOfElements,
49 int win_to_posix_error(DWORD winerr)
53 case ERROR_FILE_NOT_FOUND: return ENOENT;
54 case ERROR_PATH_NOT_FOUND: return ENOENT;
55 case ERROR_ACCESS_DENIED: return EACCES;
56 case ERROR_INVALID_HANDLE: return EBADF;
57 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
58 case ERROR_INVALID_DATA: return EINVAL;
59 case ERROR_OUTOFMEMORY: return ENOMEM;
60 case ERROR_INVALID_DRIVE: return ENODEV;
61 case ERROR_NOT_SAME_DEVICE: return EXDEV;
62 case ERROR_WRITE_PROTECT: return EROFS;
63 case ERROR_BAD_UNIT: return ENODEV;
64 case ERROR_SHARING_VIOLATION: return EACCES;
65 case ERROR_LOCK_VIOLATION: return EACCES;
66 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
67 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
68 case ERROR_NOT_SUPPORTED: return ENOSYS;
69 case ERROR_FILE_EXISTS: return EEXIST;
70 case ERROR_CANNOT_MAKE: return EPERM;
71 case ERROR_INVALID_PARAMETER: return EINVAL;
72 case ERROR_NO_PROC_SLOTS: return EAGAIN;
73 case ERROR_BROKEN_PIPE: return EPIPE;
74 case ERROR_OPEN_FAILED: return EIO;
75 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
76 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
77 case ERROR_INVALID_NAME: return ENOENT;
78 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
79 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
80 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
81 case ERROR_SIGNAL_REFUSED: return EIO;
82 case ERROR_BAD_PATHNAME: return ENOENT;
83 case ERROR_SIGNAL_PENDING: return EBUSY;
84 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
85 case ERROR_BUSY: return EBUSY;
86 case ERROR_ALREADY_EXISTS: return EEXIST;
87 case ERROR_NO_SIGNAL_SENT: return EIO;
88 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
89 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
90 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
91 case ERROR_THREAD_1_INACTIVE: return EINVAL;
92 case ERROR_BAD_PIPE: return EINVAL;
93 case ERROR_PIPE_BUSY: return EBUSY;
94 case ERROR_NO_DATA: return EPIPE;
95 case ERROR_MORE_DATA: return EAGAIN;
96 case ERROR_DIRECTORY: return ENOTDIR;
97 case ERROR_PIPE_CONNECTED: return EBUSY;
98 case ERROR_NO_TOKEN: return EINVAL;
99 case ERROR_PROCESS_ABORTED: return EFAULT;
100 case ERROR_BAD_DEVICE: return ENODEV;
101 case ERROR_BAD_USERNAME: return EINVAL;
102 case ERROR_OPEN_FILES: return EAGAIN;
103 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
104 case ERROR_DEVICE_IN_USE: return EAGAIN;
105 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
106 case ERROR_IO_DEVICE: return EIO;
107 case ERROR_NOT_OWNER: return EPERM;
108 case ERROR_END_OF_MEDIA: return ENOSPC;
109 case ERROR_EOM_OVERFLOW: return ENOSPC;
110 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
111 case ERROR_SETMARK_DETECTED: return ESPIPE;
112 case ERROR_NO_DATA_DETECTED: return ENOSPC;
113 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
114 case ERROR_CRC: return EIO;
115 case ERROR_NEGATIVE_SEEK: return EINVAL;
116 case ERROR_DISK_FULL: return ENOSPC;
117 case ERROR_NOACCESS: return EFAULT;
118 case ERROR_FILE_INVALID: return ENXIO;
124 int GetNumLogicalProcessors(void)
126 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
128 DWORD num_processors = 0;
132 while (!GetLogicalProcessorInformation(processor_info, &len)) {
133 error = GetLastError();
134 if (error == ERROR_INSUFFICIENT_BUFFER)
135 processor_info = malloc(len);
137 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
141 if (processor_info == NULL) {
142 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
147 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
149 if (processor_info[i].Relationship == RelationProcessorCore)
150 num_processors += hweight64(processor_info[i].ProcessorMask);
153 free(processor_info);
154 return num_processors;
157 long sysconf(int name)
162 MEMORYSTATUSEX status;
166 case _SC_NPROCESSORS_ONLN:
167 val = GetNumLogicalProcessors();
169 log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
174 GetSystemInfo(&sysInfo);
175 val = sysInfo.dwPageSize;
179 status.dwLength = sizeof(status);
180 val2 = sysconf(_SC_PAGESIZE);
181 if (GlobalMemoryStatusEx(&status) && val2 != -1)
182 val = status.ullTotalPhys / val2;
184 log_err("sysconf(_SC_PHYS_PAGES) failed\n");
187 log_err("sysconf(%d) is not implemented\n", name);
194 char *dl_error = NULL;
196 int dlclose(void *handle)
198 return !FreeLibrary((HMODULE)handle);
201 void *dlopen(const char *file, int mode)
205 hMod = LoadLibrary(file);
206 if (hMod == INVALID_HANDLE_VALUE)
207 dl_error = (char*)"LoadLibrary failed";
214 void *dlsym(void *handle, const char *name)
218 fnPtr = GetProcAddress((HMODULE)handle, name);
220 dl_error = (char*)"GetProcAddress failed";
232 /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
233 void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
235 LARGE_INTEGER jan1970FT;
237 jan1970FT.QuadPart = 116444736000000000LL; // january 1st 1970
238 utcFT.QuadPart = ((unsigned __int64)dosTime) * 10000000 + jan1970FT.QuadPart;
240 FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
243 char* ctime_r(const time_t *t, char *buf)
246 const char * const dayOfWeek[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
247 const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
249 Time_tToSystemTime(*t, &systime);
250 /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
251 StringCchPrintfA(buf, 32, "%s %s %d %02d:%02d:%02d %04d", dayOfWeek[systime.wDayOfWeek - 1], monthOfYear[systime.wMonth - 1],
252 systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
256 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
259 uint64_t unix_time, windows_time;
260 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
262 /* Ignore the timezone parameter */
266 * Windows time is stored as the number 100 ns intervals since January 1 1601.
267 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
268 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
270 GetSystemTimeAsFileTime(&fileTime);
271 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
272 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
273 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
274 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
275 tp->tv_sec = unix_time / 1000;
276 tp->tv_usec = (unix_time % 1000) * 1000;
280 int sigaction(int sig, const struct sigaction *act,
281 struct sigaction *oact)
284 void (*prev_handler)(int);
286 prev_handler = signal(sig, act->sa_handler);
288 oact->sa_handler = prev_handler;
290 if (prev_handler == SIG_ERR)
296 int lstat(const char * path, struct stat * buf)
298 return stat(path, buf);
301 void *mmap(void *addr, size_t len, int prot, int flags,
302 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;
313 if (prot & PROT_WRITE)
314 vaProt |= PAGE_READWRITE;
316 if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS))
318 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
319 if (allocAddr == NULL)
320 errno = win_to_posix_error(GetLastError());
326 int munmap(void *addr, size_t len)
328 if (!VirtualFree(addr, 0, MEM_RELEASE)) {
329 errno = win_to_posix_error(GetLastError());
338 log_err("%s is not implemented\n", __func__);
345 log_err("%s is not implemented\n", __func__);
350 static HANDLE log_file = INVALID_HANDLE_VALUE;
352 void openlog(const char *ident, int logopt, int facility)
354 if (log_file == INVALID_HANDLE_VALUE)
355 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
360 CloseHandle(log_file);
361 log_file = INVALID_HANDLE_VALUE;
364 void syslog(int priority, const char *message, ... /* argument */)
371 if (log_file == INVALID_HANDLE_VALUE) {
372 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
375 if (log_file == INVALID_HANDLE_VALUE) {
376 log_err("syslog: failed to open log file\n");
380 va_start(v, message);
381 len = _vscprintf(message, v);
382 output = malloc(len + sizeof(char));
383 vsprintf(output, message, v);
384 WriteFile(log_file, output, len, &bytes_written, NULL);
389 int kill(pid_t pid, int sig)
396 * This is assumed to be used only by the network code,
397 * and so doesn't try and handle any of the other cases
399 int fcntl(int fildes, int cmd, ...)
402 * non-blocking mode doesn't work the same as in BSD sockets,
407 int val, opt, status;
411 else if (cmd != F_SETFL) {
418 opt = va_arg(ap, int);
419 if (opt & O_NONBLOCK)
424 status = ioctlsocket((SOCKET)fildes, opt, &val);
426 if (status == SOCKET_ERROR) {
439 * Get the value of a local clock source.
440 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
441 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
443 int clock_gettime(clockid_t clock_id, struct timespec *tp)
447 if (clock_id == CLOCK_MONOTONIC)
449 static LARGE_INTEGER freq = {{0,0}};
450 LARGE_INTEGER counts;
453 QueryPerformanceCounter(&counts);
454 if (freq.QuadPart == 0)
455 QueryPerformanceFrequency(&freq);
457 tp->tv_sec = counts.QuadPart / freq.QuadPart;
458 /* Get the difference between the number of ns stored
459 * in 'tv_sec' and that stored in 'counts' */
460 t = tp->tv_sec * freq.QuadPart;
461 t = counts.QuadPart - t;
462 /* 't' now contains the number of cycles since the last second.
463 * We want the number of nanoseconds, so multiply out by 1,000,000,000
464 * and then divide by the frequency. */
466 tp->tv_nsec = t / freq.QuadPart;
468 else if (clock_id == CLOCK_REALTIME)
470 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
471 * higher-precision field. */
473 gettimeofday(&tv, NULL);
474 tp->tv_sec = tv.tv_sec;
475 tp->tv_nsec = tv.tv_usec * 1000;
484 int mlock(const void * addr, size_t len)
488 HANDLE process = GetCurrentProcess();
490 success = GetProcessWorkingSetSize(process, &min, &max);
492 errno = win_to_posix_error(GetLastError());
498 success = SetProcessWorkingSetSize(process, min, max);
500 errno = win_to_posix_error(GetLastError());
504 success = VirtualLock((LPVOID)addr, len);
506 errno = win_to_posix_error(GetLastError());
513 int munlock(const void * addr, size_t len)
515 BOOL success = VirtualUnlock((LPVOID)addr, len);
517 errno = win_to_posix_error(GetLastError());
524 pid_t waitpid(pid_t pid, int *stat_loc, int options)
526 log_err("%s is not implemented\n", __func__);
531 int usleep(useconds_t useconds)
533 Sleep(useconds / 1000);
537 char *basename(char *path)
539 static char name[MAX_PATH];
542 if (path == NULL || strlen(path) == 0)
545 i = strlen(path) - 1;
547 while (path[i] != '\\' && path[i] != '/' && i >= 0)
550 strncpy(name, path + i + 1, MAX_PATH);
555 int fsync(int fildes)
557 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
558 if (!FlushFileBuffers(hFile)) {
559 errno = win_to_posix_error(GetLastError());
566 int nFileMappings = 0;
567 HANDLE fileMappings[1024];
569 int shmget(key_t key, size_t size, int shmflg)
572 uint32_t size_low = size & 0xFFFFFFFF;
573 uint32_t size_high = ((uint64_t)size) >> 32;
574 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL);
575 if (hMapping != NULL) {
576 fileMappings[nFileMappings] = hMapping;
577 mapid = nFileMappings;
586 void *shmat(int shmid, const void *shmaddr, int shmflg)
589 MEMORY_BASIC_INFORMATION memInfo;
590 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
591 if (mapAddr == NULL) {
592 errno = win_to_posix_error(GetLastError());
596 if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) {
597 errno = win_to_posix_error(GetLastError());
601 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
602 if (mapAddr == NULL) {
603 errno = win_to_posix_error(GetLastError());
610 int shmdt(const void *shmaddr)
612 if (!UnmapViewOfFile(shmaddr)) {
613 errno = win_to_posix_error(GetLastError());
620 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
622 if (cmd == IPC_RMID) {
623 fileMappings[shmid] = INVALID_HANDLE_VALUE;
626 log_err("%s is not implemented\n", __func__);
632 int setuid(uid_t uid)
634 log_err("%s is not implemented\n", __func__);
639 int setgid(gid_t gid)
641 log_err("%s is not implemented\n", __func__);
656 int getrusage(int who, struct rusage *r_usage)
658 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
659 FILETIME cTime, eTime, kTime, uTime;
663 memset(r_usage, 0, sizeof(*r_usage));
665 if (who == RUSAGE_SELF) {
666 h = GetCurrentProcess();
667 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
668 } else if (who == RUSAGE_THREAD) {
669 h = GetCurrentThread();
670 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
672 log_err("fio: getrusage %d is not implemented\n", who);
676 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
677 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
679 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
680 r_usage->ru_utime.tv_sec = time;
681 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
682 r_usage->ru_utime.tv_usec = 0;
683 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
684 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
686 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
687 r_usage->ru_stime.tv_sec = time;
688 r_usage->ru_stime.tv_usec = 0;
692 int posix_madvise(void *addr, size_t len, int advice)
694 log_err("%s is not implemented\n", __func__);
698 /* Windows doesn't support advice for memory pages. Just ignore it. */
699 int msync(void *addr, size_t len, int flags)
705 int fdatasync(int fildes)
707 return fsync(fildes);
710 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
713 int64_t pos = _telli64(fildes);
714 ssize_t len = _write(fildes, buf, nbyte);
715 _lseeki64(fildes, pos, SEEK_SET);
719 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
721 int64_t pos = _telli64(fildes);
722 ssize_t len = read(fildes, buf, nbyte);
723 _lseeki64(fildes, pos, SEEK_SET);
727 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
729 log_err("%s is not implemented\n", __func__);
734 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
737 DWORD bytes_written = 0;
738 for (i = 0; i < iovcnt; i++)
740 int len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
741 if (len == SOCKET_ERROR)
743 DWORD err = GetLastError();
744 errno = win_to_posix_error(err);
748 bytes_written += len;
751 return bytes_written;
754 long long strtoll(const char *restrict str, char **restrict endptr,
757 return _strtoi64(str, endptr, base);
760 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
763 struct timeval *to = NULL;
764 fd_set readfds, writefds, exceptfds;
770 to->tv_sec = timeout / 1000;
771 to->tv_usec = (timeout % 1000) * 1000;
778 for (i = 0; i < nfds; i++)
785 if (fds[i].events & POLLIN)
786 FD_SET(fds[i].fd, &readfds);
788 if (fds[i].events & POLLOUT)
789 FD_SET(fds[i].fd, &writefds);
791 FD_SET(fds[i].fd, &exceptfds);
793 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
795 if (rc != SOCKET_ERROR) {
796 for (i = 0; i < nfds; i++)
802 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
803 fds[i].revents |= POLLIN;
805 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
806 fds[i].revents |= POLLOUT;
808 if (FD_ISSET(fds[i].fd, &exceptfds))
809 fds[i].revents |= POLLHUP;
815 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
819 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
824 ms_remaining = ms_total;
826 /* Since Sleep() can sleep for less than the requested time, add a loop to
827 ensure we only return after the requested length of time has elapsed */
829 fio_gettime(&tv, NULL);
831 ms_remaining = ms_total - mtime_since_now(&tv);
832 } while (ms_remaining > 0 && ms_remaining < ms_total);
834 /* this implementation will never sleep for less than the requested time */
843 DIR *opendir(const char *dirname)
845 struct dirent_ctx *dc = NULL;
847 /* See if we can open it. If not, we'll return an error here */
848 HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
849 if (file != INVALID_HANDLE_VALUE) {
851 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
852 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
853 dc->find_handle = INVALID_HANDLE_VALUE;
855 DWORD error = GetLastError();
856 if (error == ERROR_FILE_NOT_FOUND)
859 else if (error == ERROR_PATH_NOT_FOUND)
861 else if (error == ERROR_TOO_MANY_OPEN_FILES)
863 else if (error == ERROR_ACCESS_DENIED)
872 int closedir(DIR *dirp)
874 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
875 FindClose(dirp->find_handle);
881 struct dirent *readdir(DIR *dirp)
883 static struct dirent de;
884 WIN32_FIND_DATA find_data;
889 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
890 char search_pattern[MAX_PATH];
891 StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
892 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
893 if (dirp->find_handle == INVALID_HANDLE_VALUE)
896 if (!FindNextFile(dirp->find_handle, &find_data))
900 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
908 log_err("%s is not implemented\n", __func__);
913 in_addr_t inet_network(const char *cp)
916 in_addr_t nbo = inet_addr(cp);
917 hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
921 const char* inet_ntop(int af, const void *restrict src,
922 char *restrict dst, socklen_t size)
924 INT status = SOCKET_ERROR;
928 if (af != AF_INET && af != AF_INET6) {
929 errno = EAFNOSUPPORT;
933 WSAStartup(MAKEWORD(2,2), &wsd);
936 struct sockaddr_in si;
938 memset(&si, 0, sizeof(si));
940 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
941 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
942 } else if (af == AF_INET6) {
943 struct sockaddr_in6 si6;
945 memset(&si6, 0, sizeof(si6));
946 si6.sin6_family = af;
947 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
948 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
951 if (status != SOCKET_ERROR)
961 int inet_pton(int af, const char *restrict src, void *restrict dst)
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;
976 INT len = sizeof(si);
977 memset(&si, 0, sizeof(si));
979 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
980 if (status != SOCKET_ERROR)
981 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
982 } else if (af == AF_INET6) {
983 struct sockaddr_in6 si6;
984 INT len = sizeof(si6);
985 memset(&si6, 0, sizeof(si6));
986 si6.sin6_family = af;
987 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
988 if (status != SOCKET_ERROR)
989 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
992 if (status == SOCKET_ERROR) {