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 int win_to_posix_error(DWORD winerr)
36 case ERROR_FILE_NOT_FOUND:
38 case ERROR_PATH_NOT_FOUND:
40 case ERROR_ACCESS_DENIED:
42 case ERROR_INVALID_HANDLE:
44 case ERROR_NOT_ENOUGH_MEMORY:
46 case ERROR_INVALID_DATA:
48 case ERROR_OUTOFMEMORY:
50 case ERROR_INVALID_DRIVE:
52 case ERROR_NOT_SAME_DEVICE:
54 case ERROR_WRITE_PROTECT:
60 case ERROR_SHARING_VIOLATION:
62 case ERROR_LOCK_VIOLATION:
64 case ERROR_SHARING_BUFFER_EXCEEDED:
66 case ERROR_HANDLE_DISK_FULL:
68 case ERROR_NOT_SUPPORTED:
70 case ERROR_FILE_EXISTS:
72 case ERROR_CANNOT_MAKE:
74 case ERROR_INVALID_PARAMETER:
76 case ERROR_NO_PROC_SLOTS:
78 case ERROR_BROKEN_PIPE:
80 case ERROR_OPEN_FAILED:
82 case ERROR_NO_MORE_SEARCH_HANDLES:
84 case ERROR_CALL_NOT_IMPLEMENTED:
86 case ERROR_INVALID_NAME:
88 case ERROR_WAIT_NO_CHILDREN:
90 case ERROR_CHILD_NOT_COMPLETE:
92 case ERROR_DIR_NOT_EMPTY:
94 case ERROR_SIGNAL_REFUSED:
96 case ERROR_BAD_PATHNAME:
98 case ERROR_SIGNAL_PENDING:
100 case ERROR_MAX_THRDS_REACHED:
104 case ERROR_ALREADY_EXISTS:
106 case ERROR_NO_SIGNAL_SENT:
108 case ERROR_FILENAME_EXCED_RANGE:
110 case ERROR_META_EXPANSION_TOO_LONG:
112 case ERROR_INVALID_SIGNAL_NUMBER:
114 case ERROR_THREAD_1_INACTIVE:
118 case ERROR_PIPE_BUSY:
122 case ERROR_MORE_DATA:
124 case ERROR_DIRECTORY:
126 case ERROR_PIPE_CONNECTED:
130 case ERROR_PROCESS_ABORTED:
132 case ERROR_BAD_DEVICE:
134 case ERROR_BAD_USERNAME:
136 case ERROR_OPEN_FILES:
138 case ERROR_ACTIVE_CONNECTIONS:
140 case ERROR_DEVICE_IN_USE:
142 case ERROR_INVALID_AT_INTERRUPT_TIME:
144 case ERROR_IO_DEVICE:
146 case ERROR_NOT_OWNER:
148 case ERROR_END_OF_MEDIA:
150 case ERROR_EOM_OVERFLOW:
152 case ERROR_BEGINNING_OF_MEDIA:
154 case ERROR_SETMARK_DETECTED:
156 case ERROR_NO_DATA_DETECTED:
158 case ERROR_POSSIBLE_DEADLOCK:
162 case ERROR_NEGATIVE_SEEK:
164 case ERROR_DISK_FULL:
168 case ERROR_FILE_INVALID:
171 log_err("fio: windows error %d not handled\n", winerr);
178 int GetNumLogicalProcessors(void)
180 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
182 DWORD num_processors = 0;
186 while (!GetLogicalProcessorInformation(processor_info, &len)) {
187 error = GetLastError();
188 if (error == ERROR_INSUFFICIENT_BUFFER)
189 processor_info = malloc(len);
191 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
195 if (processor_info == NULL) {
196 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
201 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++) {
202 if (processor_info[i].Relationship == RelationProcessorCore)
203 num_processors += hweight64(processor_info[i].ProcessorMask);
206 free(processor_info);
207 return num_processors;
210 long sysconf(int name)
215 MEMORYSTATUSEX status;
218 case _SC_NPROCESSORS_ONLN:
219 val = GetNumLogicalProcessors();
221 log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
226 GetSystemInfo(&sysInfo);
227 val = sysInfo.dwPageSize;
231 status.dwLength = sizeof(status);
232 val2 = sysconf(_SC_PAGESIZE);
233 if (GlobalMemoryStatusEx(&status) && val2 != -1)
234 val = status.ullTotalPhys / val2;
236 log_err("sysconf(_SC_PHYS_PAGES) failed\n");
239 log_err("sysconf(%d) is not implemented\n", name);
246 char *dl_error = NULL;
248 int dlclose(void *handle)
250 return !FreeLibrary((HMODULE)handle);
253 void *dlopen(const char *file, int mode)
257 hMod = LoadLibrary(file);
258 if (hMod == INVALID_HANDLE_VALUE)
259 dl_error = (char*)"LoadLibrary failed";
266 void *dlsym(void *handle, const char *name)
270 fnPtr = GetProcAddress((HMODULE)handle, name);
272 dl_error = (char*)"GetProcAddress failed";
284 /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
285 void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
289 SYSTEMTIME tempSystemTime;
291 jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
292 utcFT.dwLowDateTime = (DWORD)jan1970;
293 utcFT.dwHighDateTime = jan1970 >> 32;
295 FileTimeToSystemTime((FILETIME*)&utcFT, &tempSystemTime);
296 SystemTimeToTzSpecificLocalTime(NULL, &tempSystemTime, systemTime);
299 char *ctime_r(const time_t *t, char *buf)
302 const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
303 const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
305 Time_tToSystemTime(*t, &systime);
308 * We don't know how long `buf` is, but assume it's rounded up from
309 * the minimum of 25 to 32
311 snprintf(buf, 32, "%s %s %d %02d:%02d:%02d %04d\n",
312 dayOfWeek[systime.wDayOfWeek % 7],
313 monthOfYear[(systime.wMonth - 1) % 12],
314 systime.wDay, systime.wHour, systime.wMinute,
315 systime.wSecond, systime.wYear);
319 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
322 uint64_t unix_time, windows_time;
323 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
325 /* Ignore the timezone parameter */
329 * Windows time is stored as the number 100 ns intervals since January 1 1601.
330 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
331 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
333 GetSystemTimeAsFileTime(&fileTime);
334 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
335 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
336 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
337 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
338 tp->tv_sec = unix_time / 1000;
339 tp->tv_usec = (unix_time % 1000) * 1000;
343 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
346 void (*prev_handler)(int);
348 prev_handler = signal(sig, act->sa_handler);
350 oact->sa_handler = prev_handler;
352 if (prev_handler == SIG_ERR)
358 int lstat(const char *path, struct stat *buf)
360 return stat(path, buf);
363 void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
370 void* allocAddr = NULL;
372 if (prot & PROT_NONE)
373 vaProt |= PAGE_NOACCESS;
375 if ((prot & PROT_READ) && !(prot & PROT_WRITE)) {
376 vaProt |= PAGE_READONLY;
377 mapAccess = FILE_MAP_READ;
380 if (prot & PROT_WRITE) {
381 vaProt |= PAGE_READWRITE;
382 mapAccess |= FILE_MAP_WRITE;
385 lenlow = len & 0xFFFF;
387 /* If the low DWORD is zero and the high DWORD is non-zero, `CreateFileMapping`
388 will return ERROR_INVALID_PARAMETER. To avoid this, set both to zero. */
392 if (flags & MAP_ANON || flags & MAP_ANONYMOUS) {
393 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
394 if (allocAddr == NULL)
395 errno = win_to_posix_error(GetLastError());
397 hMap = CreateFileMapping((HANDLE)_get_osfhandle(fildes), NULL,
398 vaProt, lenhigh, lenlow, NULL);
401 allocAddr = MapViewOfFile(hMap, mapAccess, off >> 16,
403 if (hMap == NULL || allocAddr == NULL)
404 errno = win_to_posix_error(GetLastError());
411 int munmap(void *addr, size_t len)
415 /* We may have allocated the memory with either MapViewOfFile or
416 VirtualAlloc. Therefore, try calling UnmapViewOfFile first, and if that
417 fails, call VirtualFree. */
418 success = UnmapViewOfFile(addr);
421 success = VirtualFree(addr, 0, MEM_RELEASE);
426 int msync(void *addr, size_t len, int flags)
428 return !FlushViewOfFile(addr, len);
433 log_err("%s is not implemented\n", __func__);
440 log_err("%s is not implemented\n", __func__);
445 static HANDLE log_file = INVALID_HANDLE_VALUE;
447 void openlog(const char *ident, int logopt, int facility)
449 if (log_file != INVALID_HANDLE_VALUE)
452 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
453 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
454 OPEN_ALWAYS, 0, NULL);
459 CloseHandle(log_file);
460 log_file = INVALID_HANDLE_VALUE;
463 void syslog(int priority, const char *message, ... /* argument */)
470 if (log_file == INVALID_HANDLE_VALUE) {
471 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
472 FILE_SHARE_READ | FILE_SHARE_WRITE,
473 NULL, OPEN_ALWAYS, 0, NULL);
476 if (log_file == INVALID_HANDLE_VALUE) {
477 log_err("syslog: failed to open log file\n");
481 va_start(v, message);
482 len = _vscprintf(message, v);
483 output = malloc(len + sizeof(char));
484 vsprintf(output, message, v);
485 WriteFile(log_file, output, len, &bytes_written, NULL);
490 int kill(pid_t pid, int sig)
497 * This is assumed to be used only by the network code,
498 * and so doesn't try and handle any of the other cases
500 int fcntl(int fildes, int cmd, ...)
503 * non-blocking mode doesn't work the same as in BSD sockets,
508 int val, opt, status;
512 else if (cmd != F_SETFL) {
519 opt = va_arg(ap, int);
520 if (opt & O_NONBLOCK)
525 status = ioctlsocket((SOCKET)fildes, opt, &val);
527 if (status == SOCKET_ERROR) {
540 * Get the value of a local clock source.
541 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
542 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
544 int clock_gettime(clockid_t clock_id, struct timespec *tp)
548 if (clock_id == CLOCK_MONOTONIC) {
549 static LARGE_INTEGER freq = {{0,0}};
550 LARGE_INTEGER counts;
553 QueryPerformanceCounter(&counts);
554 if (freq.QuadPart == 0)
555 QueryPerformanceFrequency(&freq);
557 tp->tv_sec = counts.QuadPart / freq.QuadPart;
558 /* Get the difference between the number of ns stored
559 * in 'tv_sec' and that stored in 'counts' */
560 t = tp->tv_sec * freq.QuadPart;
561 t = counts.QuadPart - t;
562 /* 't' now contains the number of cycles since the last second.
563 * We want the number of nanoseconds, so multiply out by 1,000,000,000
564 * and then divide by the frequency. */
566 tp->tv_nsec = t / freq.QuadPart;
567 } else if (clock_id == CLOCK_REALTIME) {
568 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
569 * higher-precision field. */
571 gettimeofday(&tv, NULL);
572 tp->tv_sec = tv.tv_sec;
573 tp->tv_nsec = tv.tv_usec * 1000;
582 int mlock(const void * addr, size_t len)
586 HANDLE process = GetCurrentProcess();
588 success = GetProcessWorkingSetSize(process, &min, &max);
590 errno = win_to_posix_error(GetLastError());
596 success = SetProcessWorkingSetSize(process, min, max);
598 errno = win_to_posix_error(GetLastError());
602 success = VirtualLock((LPVOID)addr, len);
604 errno = win_to_posix_error(GetLastError());
611 int munlock(const void * addr, size_t len)
613 BOOL success = VirtualUnlock((LPVOID)addr, len);
616 errno = win_to_posix_error(GetLastError());
623 pid_t waitpid(pid_t pid, int *stat_loc, int options)
625 log_err("%s is not implemented\n", __func__);
630 int usleep(useconds_t useconds)
632 Sleep(useconds / 1000);
636 char *basename(char *path)
638 static char name[MAX_PATH];
641 if (path == NULL || strlen(path) == 0)
644 i = strlen(path) - 1;
646 while (path[i] != '\\' && path[i] != '/' && i >= 0)
649 name[MAX_PATH - 1] = '\0';
650 strncpy(name, path + i + 1, MAX_PATH - 1);
655 int fsync(int fildes)
657 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
658 if (!FlushFileBuffers(hFile)) {
659 errno = win_to_posix_error(GetLastError());
666 int nFileMappings = 0;
667 HANDLE fileMappings[1024];
669 int shmget(key_t key, size_t size, int shmflg)
672 uint32_t size_low = size & 0xFFFFFFFF;
673 uint32_t size_high = ((uint64_t)size) >> 32;
676 hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
677 PAGE_EXECUTE_READWRITE | SEC_RESERVE,
678 size_high, size_low, NULL);
679 if (hMapping != NULL) {
680 fileMappings[nFileMappings] = hMapping;
681 mapid = nFileMappings;
689 void *shmat(int shmid, const void *shmaddr, int shmflg)
692 MEMORY_BASIC_INFORMATION memInfo;
694 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
695 if (mapAddr == NULL) {
696 errno = win_to_posix_error(GetLastError());
700 if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) {
701 errno = win_to_posix_error(GetLastError());
705 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
706 if (mapAddr == NULL) {
707 errno = win_to_posix_error(GetLastError());
714 int shmdt(const void *shmaddr)
716 if (!UnmapViewOfFile(shmaddr)) {
717 errno = win_to_posix_error(GetLastError());
724 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
726 if (cmd == IPC_RMID) {
727 fileMappings[shmid] = INVALID_HANDLE_VALUE;
731 log_err("%s is not implemented\n", __func__);
736 int setuid(uid_t uid)
738 log_err("%s is not implemented\n", __func__);
743 int setgid(gid_t gid)
745 log_err("%s is not implemented\n", __func__);
752 DWORD prioclass = NORMAL_PRIORITY_CLASS;
755 prioclass = HIGH_PRIORITY_CLASS;
757 prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
759 prioclass = IDLE_PRIORITY_CLASS;
761 prioclass = BELOW_NORMAL_PRIORITY_CLASS;
763 if (!SetPriorityClass(GetCurrentProcess(), prioclass))
764 log_err("fio: SetPriorityClass failed\n");
769 int getrusage(int who, struct rusage *r_usage)
771 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
772 FILETIME cTime, eTime, kTime, uTime;
776 memset(r_usage, 0, sizeof(*r_usage));
778 if (who == RUSAGE_SELF) {
779 h = GetCurrentProcess();
780 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
781 } else if (who == RUSAGE_THREAD) {
782 h = GetCurrentThread();
783 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
785 log_err("fio: getrusage %d is not implemented\n", who);
789 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
790 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
792 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
793 r_usage->ru_utime.tv_sec = time;
794 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
795 r_usage->ru_utime.tv_usec = 0;
796 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
797 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
799 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
800 r_usage->ru_stime.tv_sec = time;
801 r_usage->ru_stime.tv_usec = 0;
805 int posix_madvise(void *addr, size_t len, int advice)
810 int fdatasync(int fildes)
812 return fsync(fildes);
815 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
818 int64_t pos = _telli64(fildes);
819 ssize_t len = _write(fildes, buf, nbyte);
821 _lseeki64(fildes, pos, SEEK_SET);
825 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
827 int64_t pos = _telli64(fildes);
828 ssize_t len = read(fildes, buf, nbyte);
830 _lseeki64(fildes, pos, SEEK_SET);
834 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
836 log_err("%s is not implemented\n", __func__);
841 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
844 DWORD bytes_written = 0;
846 for (i = 0; i < iovcnt; i++) {
849 len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
850 if (len == SOCKET_ERROR) {
851 DWORD err = GetLastError();
852 errno = win_to_posix_error(err);
856 bytes_written += len;
859 return bytes_written;
862 long long strtoll(const char *restrict str, char **restrict endptr, int base)
864 return _strtoi64(str, endptr, base);
867 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
870 struct timeval *to = NULL;
871 fd_set readfds, writefds, exceptfds;
877 to->tv_sec = timeout / 1000;
878 to->tv_usec = (timeout % 1000) * 1000;
885 for (i = 0; i < nfds; i++) {
886 if (fds[i].fd == INVALID_SOCKET) {
891 if (fds[i].events & POLLIN)
892 FD_SET(fds[i].fd, &readfds);
894 if (fds[i].events & POLLOUT)
895 FD_SET(fds[i].fd, &writefds);
897 FD_SET(fds[i].fd, &exceptfds);
899 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
901 if (rc != SOCKET_ERROR) {
902 for (i = 0; i < nfds; i++) {
903 if (fds[i].fd == INVALID_SOCKET)
906 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
907 fds[i].revents |= POLLIN;
909 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
910 fds[i].revents |= POLLOUT;
912 if (FD_ISSET(fds[i].fd, &exceptfds))
913 fds[i].revents |= POLLHUP;
919 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
923 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
928 ms_remaining = ms_total;
930 /* Since Sleep() can sleep for less than the requested time, add a loop to
931 ensure we only return after the requested length of time has elapsed */
933 fio_gettime(&tv, NULL);
935 ms_remaining = ms_total - mtime_since_now(&tv);
936 } while (ms_remaining > 0 && ms_remaining < ms_total);
938 /* this implementation will never sleep for less than the requested time */
947 DIR *opendir(const char *dirname)
949 struct dirent_ctx *dc = NULL;
952 /* See if we can open it. If not, we'll return an error here */
953 file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
954 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
955 if (file != INVALID_HANDLE_VALUE) {
957 dc = malloc(sizeof(struct dirent_ctx));
958 snprintf(dc->dirname, sizeof(dc->dirname), "%s", dirname);
959 dc->find_handle = INVALID_HANDLE_VALUE;
961 DWORD error = GetLastError();
962 if (error == ERROR_FILE_NOT_FOUND)
965 else if (error == ERROR_PATH_NOT_FOUND)
967 else if (error == ERROR_TOO_MANY_OPEN_FILES)
969 else if (error == ERROR_ACCESS_DENIED)
978 int closedir(DIR *dirp)
980 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
981 FindClose(dirp->find_handle);
987 struct dirent *readdir(DIR *dirp)
989 static struct dirent de;
990 WIN32_FIND_DATA find_data;
995 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
996 char search_pattern[MAX_PATH];
998 snprintf(search_pattern, sizeof(search_pattern), "%s\\*",
1000 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
1001 if (dirp->find_handle == INVALID_HANDLE_VALUE)
1004 if (!FindNextFile(dirp->find_handle, &find_data))
1008 snprintf(de.d_name, sizeof(de.d_name), find_data.cFileName);
1016 log_err("%s is not implemented\n", __func__);
1021 in_addr_t inet_network(const char *cp)
1024 in_addr_t nbo = inet_addr(cp);
1025 hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
1029 #ifdef CONFIG_WINDOWS_XP
1030 const char *inet_ntop(int af, const void *restrict src, char *restrict dst,
1033 INT status = SOCKET_ERROR;
1037 if (af != AF_INET && af != AF_INET6) {
1038 errno = EAFNOSUPPORT;
1042 WSAStartup(MAKEWORD(2,2), &wsd);
1044 if (af == AF_INET) {
1045 struct sockaddr_in si;
1048 memset(&si, 0, sizeof(si));
1050 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
1051 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
1052 } else if (af == AF_INET6) {
1053 struct sockaddr_in6 si6;
1056 memset(&si6, 0, sizeof(si6));
1057 si6.sin6_family = af;
1058 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
1059 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
1062 if (status != SOCKET_ERROR)
1072 int inet_pton(int af, const char *restrict src, void *restrict dst)
1074 INT status = SOCKET_ERROR;
1078 if (af != AF_INET && af != AF_INET6) {
1079 errno = EAFNOSUPPORT;
1083 WSAStartup(MAKEWORD(2,2), &wsd);
1085 if (af == AF_INET) {
1086 struct sockaddr_in si;
1087 INT len = sizeof(si);
1089 memset(&si, 0, sizeof(si));
1091 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
1092 if (status != SOCKET_ERROR)
1093 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
1094 } else if (af == AF_INET6) {
1095 struct sockaddr_in6 si6;
1096 INT len = sizeof(si6);
1098 memset(&si6, 0, sizeof(si6));
1099 si6.sin6_family = af;
1100 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
1101 if (status != SOCKET_ERROR)
1102 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
1105 if (status == SOCKET_ERROR) {
1114 #endif /* CONFIG_WINDOWS_XP */