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(char *pszDest, size_t cchDest, const char *pszSrc);
33 HRESULT WINAPI StringCchPrintfA(char *pszDest, size_t cchDest, const char *pszFormat, ...);
35 int win_to_posix_error(DWORD winerr)
40 case ERROR_FILE_NOT_FOUND:
42 case ERROR_PATH_NOT_FOUND:
44 case ERROR_ACCESS_DENIED:
46 case ERROR_INVALID_HANDLE:
48 case ERROR_NOT_ENOUGH_MEMORY:
50 case ERROR_INVALID_DATA:
52 case ERROR_OUTOFMEMORY:
54 case ERROR_INVALID_DRIVE:
56 case ERROR_NOT_SAME_DEVICE:
58 case ERROR_WRITE_PROTECT:
64 case ERROR_SHARING_VIOLATION:
66 case ERROR_LOCK_VIOLATION:
68 case ERROR_SHARING_BUFFER_EXCEEDED:
70 case ERROR_HANDLE_DISK_FULL:
72 case ERROR_NOT_SUPPORTED:
74 case ERROR_FILE_EXISTS:
76 case ERROR_CANNOT_MAKE:
78 case ERROR_INVALID_PARAMETER:
80 case ERROR_NO_PROC_SLOTS:
82 case ERROR_BROKEN_PIPE:
84 case ERROR_OPEN_FAILED:
86 case ERROR_NO_MORE_SEARCH_HANDLES:
88 case ERROR_CALL_NOT_IMPLEMENTED:
90 case ERROR_INVALID_NAME:
92 case ERROR_WAIT_NO_CHILDREN:
94 case ERROR_CHILD_NOT_COMPLETE:
96 case ERROR_DIR_NOT_EMPTY:
98 case ERROR_SIGNAL_REFUSED:
100 case ERROR_BAD_PATHNAME:
102 case ERROR_SIGNAL_PENDING:
104 case ERROR_MAX_THRDS_REACHED:
108 case ERROR_ALREADY_EXISTS:
110 case ERROR_NO_SIGNAL_SENT:
112 case ERROR_FILENAME_EXCED_RANGE:
114 case ERROR_META_EXPANSION_TOO_LONG:
116 case ERROR_INVALID_SIGNAL_NUMBER:
118 case ERROR_THREAD_1_INACTIVE:
122 case ERROR_PIPE_BUSY:
126 case ERROR_MORE_DATA:
128 case ERROR_DIRECTORY:
130 case ERROR_PIPE_CONNECTED:
134 case ERROR_PROCESS_ABORTED:
136 case ERROR_BAD_DEVICE:
138 case ERROR_BAD_USERNAME:
140 case ERROR_OPEN_FILES:
142 case ERROR_ACTIVE_CONNECTIONS:
144 case ERROR_DEVICE_IN_USE:
146 case ERROR_INVALID_AT_INTERRUPT_TIME:
148 case ERROR_IO_DEVICE:
150 case ERROR_NOT_OWNER:
152 case ERROR_END_OF_MEDIA:
154 case ERROR_EOM_OVERFLOW:
156 case ERROR_BEGINNING_OF_MEDIA:
158 case ERROR_SETMARK_DETECTED:
160 case ERROR_NO_DATA_DETECTED:
162 case ERROR_POSSIBLE_DEADLOCK:
166 case ERROR_NEGATIVE_SEEK:
168 case ERROR_DISK_FULL:
172 case ERROR_FILE_INVALID:
175 log_err("fio: windows error %d not handled\n", winerr);
182 int GetNumLogicalProcessors(void)
184 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
186 DWORD num_processors = 0;
190 while (!GetLogicalProcessorInformation(processor_info, &len)) {
191 error = GetLastError();
192 if (error == ERROR_INSUFFICIENT_BUFFER)
193 processor_info = malloc(len);
195 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
199 if (processor_info == NULL) {
200 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
205 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++) {
206 if (processor_info[i].Relationship == RelationProcessorCore)
207 num_processors += hweight64(processor_info[i].ProcessorMask);
210 free(processor_info);
211 return num_processors;
214 long sysconf(int name)
219 MEMORYSTATUSEX status;
222 case _SC_NPROCESSORS_ONLN:
223 val = GetNumLogicalProcessors();
225 log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
230 GetSystemInfo(&sysInfo);
231 val = sysInfo.dwPageSize;
235 status.dwLength = sizeof(status);
236 val2 = sysconf(_SC_PAGESIZE);
237 if (GlobalMemoryStatusEx(&status) && val2 != -1)
238 val = status.ullTotalPhys / val2;
240 log_err("sysconf(_SC_PHYS_PAGES) failed\n");
243 log_err("sysconf(%d) is not implemented\n", name);
250 char *dl_error = NULL;
252 int dlclose(void *handle)
254 return !FreeLibrary((HMODULE)handle);
257 void *dlopen(const char *file, int mode)
261 hMod = LoadLibrary(file);
262 if (hMod == INVALID_HANDLE_VALUE)
263 dl_error = (char*)"LoadLibrary failed";
270 void *dlsym(void *handle, const char *name)
274 fnPtr = GetProcAddress((HMODULE)handle, name);
276 dl_error = (char*)"GetProcAddress failed";
288 /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
289 void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
293 SYSTEMTIME tempSystemTime;
295 jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
296 utcFT.dwLowDateTime = (DWORD)jan1970;
297 utcFT.dwHighDateTime = jan1970 >> 32;
299 FileTimeToSystemTime((FILETIME*)&utcFT, &tempSystemTime);
300 SystemTimeToTzSpecificLocalTime(NULL, &tempSystemTime, systemTime);
303 char *ctime_r(const time_t *t, char *buf)
306 const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
307 const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
309 Time_tToSystemTime(*t, &systime);
312 * We don't know how long `buf` is, but assume it's rounded up from
313 * the minimum of 25 to 32
315 StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d\n",
316 dayOfWeek[systime.wDayOfWeek % 7],
317 monthOfYear[(systime.wMonth - 1) % 12],
318 systime.wDay, systime.wHour, systime.wMinute,
319 systime.wSecond, systime.wYear);
323 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
326 uint64_t unix_time, windows_time;
327 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
329 /* Ignore the timezone parameter */
333 * Windows time is stored as the number 100 ns intervals since January 1 1601.
334 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
335 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
337 GetSystemTimeAsFileTime(&fileTime);
338 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
339 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
340 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
341 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
342 tp->tv_sec = unix_time / 1000;
343 tp->tv_usec = (unix_time % 1000) * 1000;
347 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
350 void (*prev_handler)(int);
352 prev_handler = signal(sig, act->sa_handler);
354 oact->sa_handler = prev_handler;
356 if (prev_handler == SIG_ERR)
362 int lstat(const char *path, struct stat *buf)
364 return stat(path, buf);
367 void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
374 void* allocAddr = NULL;
376 if (prot & PROT_NONE)
377 vaProt |= PAGE_NOACCESS;
379 if ((prot & PROT_READ) && !(prot & PROT_WRITE)) {
380 vaProt |= PAGE_READONLY;
381 mapAccess = FILE_MAP_READ;
384 if (prot & PROT_WRITE) {
385 vaProt |= PAGE_READWRITE;
386 mapAccess |= FILE_MAP_WRITE;
389 lenlow = len & 0xFFFF;
391 /* If the low DWORD is zero and the high DWORD is non-zero, `CreateFileMapping`
392 will return ERROR_INVALID_PARAMETER. To avoid this, set both to zero. */
396 if (flags & MAP_ANON || flags & MAP_ANONYMOUS) {
397 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
398 if (allocAddr == NULL)
399 errno = win_to_posix_error(GetLastError());
401 hMap = CreateFileMapping((HANDLE)_get_osfhandle(fildes), NULL,
402 vaProt, lenhigh, lenlow, NULL);
405 allocAddr = MapViewOfFile(hMap, mapAccess, off >> 16,
407 if (hMap == NULL || allocAddr == NULL)
408 errno = win_to_posix_error(GetLastError());
415 int munmap(void *addr, size_t len)
419 /* We may have allocated the memory with either MapViewOfFile or
420 VirtualAlloc. Therefore, try calling UnmapViewOfFile first, and if that
421 fails, call VirtualFree. */
422 success = UnmapViewOfFile(addr);
425 success = VirtualFree(addr, 0, MEM_RELEASE);
430 int msync(void *addr, size_t len, int flags)
432 return !FlushViewOfFile(addr, len);
437 log_err("%s is not implemented\n", __func__);
444 log_err("%s is not implemented\n", __func__);
449 static HANDLE log_file = INVALID_HANDLE_VALUE;
451 void openlog(const char *ident, int logopt, int facility)
453 if (log_file != INVALID_HANDLE_VALUE)
456 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
457 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
458 OPEN_ALWAYS, 0, NULL);
463 CloseHandle(log_file);
464 log_file = INVALID_HANDLE_VALUE;
467 void syslog(int priority, const char *message, ... /* argument */)
474 if (log_file == INVALID_HANDLE_VALUE) {
475 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
476 FILE_SHARE_READ | FILE_SHARE_WRITE,
477 NULL, OPEN_ALWAYS, 0, NULL);
480 if (log_file == INVALID_HANDLE_VALUE) {
481 log_err("syslog: failed to open log file\n");
485 va_start(v, message);
486 len = _vscprintf(message, v);
487 output = malloc(len + sizeof(char));
488 vsprintf(output, message, v);
489 WriteFile(log_file, output, len, &bytes_written, NULL);
494 int kill(pid_t pid, int sig)
501 * This is assumed to be used only by the network code,
502 * and so doesn't try and handle any of the other cases
504 int fcntl(int fildes, int cmd, ...)
507 * non-blocking mode doesn't work the same as in BSD sockets,
512 int val, opt, status;
516 else if (cmd != F_SETFL) {
523 opt = va_arg(ap, int);
524 if (opt & O_NONBLOCK)
529 status = ioctlsocket((SOCKET)fildes, opt, &val);
531 if (status == SOCKET_ERROR) {
544 * Get the value of a local clock source.
545 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
546 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
548 int clock_gettime(clockid_t clock_id, struct timespec *tp)
552 if (clock_id == CLOCK_MONOTONIC) {
553 static LARGE_INTEGER freq = {{0,0}};
554 LARGE_INTEGER counts;
557 QueryPerformanceCounter(&counts);
558 if (freq.QuadPart == 0)
559 QueryPerformanceFrequency(&freq);
561 tp->tv_sec = counts.QuadPart / freq.QuadPart;
562 /* Get the difference between the number of ns stored
563 * in 'tv_sec' and that stored in 'counts' */
564 t = tp->tv_sec * freq.QuadPart;
565 t = counts.QuadPart - t;
566 /* 't' now contains the number of cycles since the last second.
567 * We want the number of nanoseconds, so multiply out by 1,000,000,000
568 * and then divide by the frequency. */
570 tp->tv_nsec = t / freq.QuadPart;
571 } else if (clock_id == CLOCK_REALTIME) {
572 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
573 * higher-precision field. */
575 gettimeofday(&tv, NULL);
576 tp->tv_sec = tv.tv_sec;
577 tp->tv_nsec = tv.tv_usec * 1000;
586 int mlock(const void * addr, size_t len)
590 HANDLE process = GetCurrentProcess();
592 success = GetProcessWorkingSetSize(process, &min, &max);
594 errno = win_to_posix_error(GetLastError());
600 success = SetProcessWorkingSetSize(process, min, max);
602 errno = win_to_posix_error(GetLastError());
606 success = VirtualLock((LPVOID)addr, len);
608 errno = win_to_posix_error(GetLastError());
615 int munlock(const void * addr, size_t len)
617 BOOL success = VirtualUnlock((LPVOID)addr, len);
620 errno = win_to_posix_error(GetLastError());
627 pid_t waitpid(pid_t pid, int *stat_loc, int options)
629 log_err("%s is not implemented\n", __func__);
634 int usleep(useconds_t useconds)
636 Sleep(useconds / 1000);
640 char *basename(char *path)
642 static char name[MAX_PATH];
645 if (path == NULL || strlen(path) == 0)
648 i = strlen(path) - 1;
650 while (path[i] != '\\' && path[i] != '/' && i >= 0)
653 name[MAX_PATH - 1] = '\0';
654 strncpy(name, path + i + 1, MAX_PATH - 1);
659 int fsync(int fildes)
661 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
662 if (!FlushFileBuffers(hFile)) {
663 errno = win_to_posix_error(GetLastError());
670 int nFileMappings = 0;
671 HANDLE fileMappings[1024];
673 int shmget(key_t key, size_t size, int shmflg)
676 uint32_t size_low = size & 0xFFFFFFFF;
677 uint32_t size_high = ((uint64_t)size) >> 32;
680 hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
681 PAGE_EXECUTE_READWRITE | SEC_RESERVE,
682 size_high, size_low, NULL);
683 if (hMapping != NULL) {
684 fileMappings[nFileMappings] = hMapping;
685 mapid = nFileMappings;
693 void *shmat(int shmid, const void *shmaddr, int shmflg)
696 MEMORY_BASIC_INFORMATION memInfo;
698 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
699 if (mapAddr == NULL) {
700 errno = win_to_posix_error(GetLastError());
704 if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) {
705 errno = win_to_posix_error(GetLastError());
709 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
710 if (mapAddr == NULL) {
711 errno = win_to_posix_error(GetLastError());
718 int shmdt(const void *shmaddr)
720 if (!UnmapViewOfFile(shmaddr)) {
721 errno = win_to_posix_error(GetLastError());
728 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
730 if (cmd == IPC_RMID) {
731 fileMappings[shmid] = INVALID_HANDLE_VALUE;
735 log_err("%s is not implemented\n", __func__);
740 int setuid(uid_t uid)
742 log_err("%s is not implemented\n", __func__);
747 int setgid(gid_t gid)
749 log_err("%s is not implemented\n", __func__);
756 DWORD prioclass = NORMAL_PRIORITY_CLASS;
759 prioclass = HIGH_PRIORITY_CLASS;
761 prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
763 prioclass = IDLE_PRIORITY_CLASS;
765 prioclass = BELOW_NORMAL_PRIORITY_CLASS;
767 if (!SetPriorityClass(GetCurrentProcess(), prioclass))
768 log_err("fio: SetPriorityClass failed\n");
773 int getrusage(int who, struct rusage *r_usage)
775 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
776 FILETIME cTime, eTime, kTime, uTime;
780 memset(r_usage, 0, sizeof(*r_usage));
782 if (who == RUSAGE_SELF) {
783 h = GetCurrentProcess();
784 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
785 } else if (who == RUSAGE_THREAD) {
786 h = GetCurrentThread();
787 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
789 log_err("fio: getrusage %d is not implemented\n", who);
793 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
794 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
796 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
797 r_usage->ru_utime.tv_sec = time;
798 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
799 r_usage->ru_utime.tv_usec = 0;
800 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
801 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
803 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
804 r_usage->ru_stime.tv_sec = time;
805 r_usage->ru_stime.tv_usec = 0;
809 int posix_madvise(void *addr, size_t len, int advice)
814 int fdatasync(int fildes)
816 return fsync(fildes);
819 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
822 int64_t pos = _telli64(fildes);
823 ssize_t len = _write(fildes, buf, nbyte);
825 _lseeki64(fildes, pos, SEEK_SET);
829 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
831 int64_t pos = _telli64(fildes);
832 ssize_t len = read(fildes, buf, nbyte);
834 _lseeki64(fildes, pos, SEEK_SET);
838 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
840 log_err("%s is not implemented\n", __func__);
845 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
848 DWORD bytes_written = 0;
850 for (i = 0; i < iovcnt; i++) {
853 len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
854 if (len == SOCKET_ERROR) {
855 DWORD err = GetLastError();
856 errno = win_to_posix_error(err);
860 bytes_written += len;
863 return bytes_written;
866 long long strtoll(const char *restrict str, char **restrict endptr, int base)
868 return _strtoi64(str, endptr, base);
871 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
874 struct timeval *to = NULL;
875 fd_set readfds, writefds, exceptfds;
881 to->tv_sec = timeout / 1000;
882 to->tv_usec = (timeout % 1000) * 1000;
889 for (i = 0; i < nfds; i++) {
895 if (fds[i].events & POLLIN)
896 FD_SET(fds[i].fd, &readfds);
898 if (fds[i].events & POLLOUT)
899 FD_SET(fds[i].fd, &writefds);
901 FD_SET(fds[i].fd, &exceptfds);
903 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
905 if (rc != SOCKET_ERROR) {
906 for (i = 0; i < nfds; i++) {
910 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
911 fds[i].revents |= POLLIN;
913 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
914 fds[i].revents |= POLLOUT;
916 if (FD_ISSET(fds[i].fd, &exceptfds))
917 fds[i].revents |= POLLHUP;
923 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
927 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
932 ms_remaining = ms_total;
934 /* Since Sleep() can sleep for less than the requested time, add a loop to
935 ensure we only return after the requested length of time has elapsed */
937 fio_gettime(&tv, NULL);
939 ms_remaining = ms_total - mtime_since_now(&tv);
940 } while (ms_remaining > 0 && ms_remaining < ms_total);
942 /* this implementation will never sleep for less than the requested time */
951 DIR *opendir(const char *dirname)
953 struct dirent_ctx *dc = NULL;
956 /* See if we can open it. If not, we'll return an error here */
957 file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
958 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
959 if (file != INVALID_HANDLE_VALUE) {
961 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
962 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
963 dc->find_handle = INVALID_HANDLE_VALUE;
965 DWORD error = GetLastError();
966 if (error == ERROR_FILE_NOT_FOUND)
969 else if (error == ERROR_PATH_NOT_FOUND)
971 else if (error == ERROR_TOO_MANY_OPEN_FILES)
973 else if (error == ERROR_ACCESS_DENIED)
982 int closedir(DIR *dirp)
984 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
985 FindClose(dirp->find_handle);
991 struct dirent *readdir(DIR *dirp)
993 static struct dirent de;
994 WIN32_FIND_DATA find_data;
999 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
1000 char search_pattern[MAX_PATH];
1002 StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
1003 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
1004 if (dirp->find_handle == INVALID_HANDLE_VALUE)
1007 if (!FindNextFile(dirp->find_handle, &find_data))
1011 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
1019 log_err("%s is not implemented\n", __func__);
1024 in_addr_t inet_network(const char *cp)
1027 in_addr_t nbo = inet_addr(cp);
1028 hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
1032 #ifdef CONFIG_WINDOWS_XP
1033 const char *inet_ntop(int af, const void *restrict src, char *restrict dst,
1036 INT status = SOCKET_ERROR;
1040 if (af != AF_INET && af != AF_INET6) {
1041 errno = EAFNOSUPPORT;
1045 WSAStartup(MAKEWORD(2,2), &wsd);
1047 if (af == AF_INET) {
1048 struct sockaddr_in si;
1051 memset(&si, 0, sizeof(si));
1053 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
1054 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
1055 } else if (af == AF_INET6) {
1056 struct sockaddr_in6 si6;
1059 memset(&si6, 0, sizeof(si6));
1060 si6.sin6_family = af;
1061 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
1062 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
1065 if (status != SOCKET_ERROR)
1075 int inet_pton(int af, const char *restrict src, void *restrict dst)
1077 INT status = SOCKET_ERROR;
1081 if (af != AF_INET && af != AF_INET6) {
1082 errno = EAFNOSUPPORT;
1086 WSAStartup(MAKEWORD(2,2), &wsd);
1088 if (af == AF_INET) {
1089 struct sockaddr_in si;
1090 INT len = sizeof(si);
1092 memset(&si, 0, sizeof(si));
1094 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
1095 if (status != SOCKET_ERROR)
1096 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
1097 } else if (af == AF_INET6) {
1098 struct sockaddr_in6 si6;
1099 INT len = sizeof(si6);
1101 memset(&si6, 0, sizeof(si6));
1102 si6.sin6_family = af;
1103 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
1104 if (status != SOCKET_ERROR)
1105 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
1108 if (status == SOCKET_ERROR) {
1117 #endif /* CONFIG_WINDOWS_XP */