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)
38 case ERROR_FILE_NOT_FOUND:
40 case ERROR_PATH_NOT_FOUND:
42 case ERROR_ACCESS_DENIED:
44 case ERROR_INVALID_HANDLE:
46 case ERROR_NOT_ENOUGH_MEMORY:
48 case ERROR_INVALID_DATA:
50 case ERROR_OUTOFMEMORY:
52 case ERROR_INVALID_DRIVE:
54 case ERROR_NOT_SAME_DEVICE:
56 case ERROR_WRITE_PROTECT:
62 case ERROR_SHARING_VIOLATION:
64 case ERROR_LOCK_VIOLATION:
66 case ERROR_SHARING_BUFFER_EXCEEDED:
68 case ERROR_HANDLE_DISK_FULL:
70 case ERROR_NOT_SUPPORTED:
72 case ERROR_FILE_EXISTS:
74 case ERROR_CANNOT_MAKE:
76 case ERROR_INVALID_PARAMETER:
78 case ERROR_NO_PROC_SLOTS:
80 case ERROR_BROKEN_PIPE:
82 case ERROR_OPEN_FAILED:
84 case ERROR_NO_MORE_SEARCH_HANDLES:
86 case ERROR_CALL_NOT_IMPLEMENTED:
88 case ERROR_INVALID_NAME:
90 case ERROR_WAIT_NO_CHILDREN:
92 case ERROR_CHILD_NOT_COMPLETE:
94 case ERROR_DIR_NOT_EMPTY:
96 case ERROR_SIGNAL_REFUSED:
98 case ERROR_BAD_PATHNAME:
100 case ERROR_SIGNAL_PENDING:
102 case ERROR_MAX_THRDS_REACHED:
106 case ERROR_ALREADY_EXISTS:
108 case ERROR_NO_SIGNAL_SENT:
110 case ERROR_FILENAME_EXCED_RANGE:
112 case ERROR_META_EXPANSION_TOO_LONG:
114 case ERROR_INVALID_SIGNAL_NUMBER:
116 case ERROR_THREAD_1_INACTIVE:
120 case ERROR_PIPE_BUSY:
124 case ERROR_MORE_DATA:
126 case ERROR_DIRECTORY:
128 case ERROR_PIPE_CONNECTED:
132 case ERROR_PROCESS_ABORTED:
134 case ERROR_BAD_DEVICE:
136 case ERROR_BAD_USERNAME:
138 case ERROR_OPEN_FILES:
140 case ERROR_ACTIVE_CONNECTIONS:
142 case ERROR_DEVICE_IN_USE:
144 case ERROR_INVALID_AT_INTERRUPT_TIME:
146 case ERROR_IO_DEVICE:
148 case ERROR_NOT_OWNER:
150 case ERROR_END_OF_MEDIA:
152 case ERROR_EOM_OVERFLOW:
154 case ERROR_BEGINNING_OF_MEDIA:
156 case ERROR_SETMARK_DETECTED:
158 case ERROR_NO_DATA_DETECTED:
160 case ERROR_POSSIBLE_DEADLOCK:
164 case ERROR_NEGATIVE_SEEK:
166 case ERROR_DISK_FULL:
170 case ERROR_FILE_INVALID:
173 log_err("fio: windows error %d not handled\n", winerr);
180 int GetNumLogicalProcessors(void)
182 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
184 DWORD num_processors = 0;
188 while (!GetLogicalProcessorInformation(processor_info, &len)) {
189 error = GetLastError();
190 if (error == ERROR_INSUFFICIENT_BUFFER)
191 processor_info = malloc(len);
193 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
197 if (processor_info == NULL) {
198 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
203 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++) {
204 if (processor_info[i].Relationship == RelationProcessorCore)
205 num_processors += hweight64(processor_info[i].ProcessorMask);
208 free(processor_info);
209 return num_processors;
212 long sysconf(int name)
217 MEMORYSTATUSEX status;
220 case _SC_NPROCESSORS_ONLN:
221 val = GetNumLogicalProcessors();
223 log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
228 GetSystemInfo(&sysInfo);
229 val = sysInfo.dwPageSize;
233 status.dwLength = sizeof(status);
234 val2 = sysconf(_SC_PAGESIZE);
235 if (GlobalMemoryStatusEx(&status) && val2 != -1)
236 val = status.ullTotalPhys / val2;
238 log_err("sysconf(_SC_PHYS_PAGES) failed\n");
241 log_err("sysconf(%d) is not implemented\n", name);
248 char *dl_error = NULL;
250 int dlclose(void *handle)
252 return !FreeLibrary((HMODULE)handle);
255 void *dlopen(const char *file, int mode)
259 hMod = LoadLibrary(file);
260 if (hMod == INVALID_HANDLE_VALUE)
261 dl_error = (char*)"LoadLibrary failed";
268 void *dlsym(void *handle, const char *name)
272 fnPtr = GetProcAddress((HMODULE)handle, name);
274 dl_error = (char*)"GetProcAddress failed";
286 /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
287 void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
291 SYSTEMTIME tempSystemTime;
293 jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
294 utcFT.dwLowDateTime = (DWORD)jan1970;
295 utcFT.dwHighDateTime = jan1970 >> 32;
297 FileTimeToSystemTime((FILETIME*)&utcFT, &tempSystemTime);
298 SystemTimeToTzSpecificLocalTime(NULL, &tempSystemTime, systemTime);
301 char *ctime_r(const time_t *t, char *buf)
304 const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
305 const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
307 Time_tToSystemTime(*t, &systime);
310 * We don't know how long `buf` is, but assume it's rounded up from
311 * the minimum of 25 to 32
313 StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d\n",
314 dayOfWeek[systime.wDayOfWeek % 7],
315 monthOfYear[(systime.wMonth - 1) % 12],
316 systime.wDay, systime.wHour, systime.wMinute,
317 systime.wSecond, systime.wYear);
321 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
324 uint64_t unix_time, windows_time;
325 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
327 /* Ignore the timezone parameter */
331 * Windows time is stored as the number 100 ns intervals since January 1 1601.
332 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
333 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
335 GetSystemTimeAsFileTime(&fileTime);
336 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
337 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
338 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
339 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
340 tp->tv_sec = unix_time / 1000;
341 tp->tv_usec = (unix_time % 1000) * 1000;
345 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
348 void (*prev_handler)(int);
350 prev_handler = signal(sig, act->sa_handler);
352 oact->sa_handler = prev_handler;
354 if (prev_handler == SIG_ERR)
360 int lstat(const char *path, struct stat *buf)
362 return stat(path, buf);
365 void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
372 void* allocAddr = NULL;
374 if (prot & PROT_NONE)
375 vaProt |= PAGE_NOACCESS;
377 if ((prot & PROT_READ) && !(prot & PROT_WRITE)) {
378 vaProt |= PAGE_READONLY;
379 mapAccess = FILE_MAP_READ;
382 if (prot & PROT_WRITE) {
383 vaProt |= PAGE_READWRITE;
384 mapAccess |= FILE_MAP_WRITE;
387 lenlow = len & 0xFFFF;
389 /* If the low DWORD is zero and the high DWORD is non-zero, `CreateFileMapping`
390 will return ERROR_INVALID_PARAMETER. To avoid this, set both to zero. */
394 if (flags & MAP_ANON || flags & MAP_ANONYMOUS) {
395 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
396 if (allocAddr == NULL)
397 errno = win_to_posix_error(GetLastError());
399 hMap = CreateFileMapping((HANDLE)_get_osfhandle(fildes), NULL,
400 vaProt, lenhigh, lenlow, NULL);
403 allocAddr = MapViewOfFile(hMap, mapAccess, off >> 16,
405 if (hMap == NULL || allocAddr == NULL)
406 errno = win_to_posix_error(GetLastError());
413 int munmap(void *addr, size_t len)
417 /* We may have allocated the memory with either MapViewOfFile or
418 VirtualAlloc. Therefore, try calling UnmapViewOfFile first, and if that
419 fails, call VirtualFree. */
420 success = UnmapViewOfFile(addr);
423 success = VirtualFree(addr, 0, MEM_RELEASE);
428 int msync(void *addr, size_t len, int flags)
430 return !FlushViewOfFile(addr, len);
435 log_err("%s is not implemented\n", __func__);
442 log_err("%s is not implemented\n", __func__);
447 static HANDLE log_file = INVALID_HANDLE_VALUE;
449 void openlog(const char *ident, int logopt, int facility)
451 if (log_file != INVALID_HANDLE_VALUE)
454 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
455 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
456 OPEN_ALWAYS, 0, NULL);
461 CloseHandle(log_file);
462 log_file = INVALID_HANDLE_VALUE;
465 void syslog(int priority, const char *message, ... /* argument */)
472 if (log_file == INVALID_HANDLE_VALUE) {
473 log_file = CreateFileA("syslog.txt", GENERIC_WRITE,
474 FILE_SHARE_READ | FILE_SHARE_WRITE,
475 NULL, OPEN_ALWAYS, 0, NULL);
478 if (log_file == INVALID_HANDLE_VALUE) {
479 log_err("syslog: failed to open log file\n");
483 va_start(v, message);
484 len = _vscprintf(message, v);
485 output = malloc(len + sizeof(char));
486 vsprintf(output, message, v);
487 WriteFile(log_file, output, len, &bytes_written, NULL);
492 int kill(pid_t pid, int sig)
499 * This is assumed to be used only by the network code,
500 * and so doesn't try and handle any of the other cases
502 int fcntl(int fildes, int cmd, ...)
505 * non-blocking mode doesn't work the same as in BSD sockets,
510 int val, opt, status;
514 else if (cmd != F_SETFL) {
521 opt = va_arg(ap, int);
522 if (opt & O_NONBLOCK)
527 status = ioctlsocket((SOCKET)fildes, opt, &val);
529 if (status == SOCKET_ERROR) {
542 * Get the value of a local clock source.
543 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
544 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
546 int clock_gettime(clockid_t clock_id, struct timespec *tp)
550 if (clock_id == CLOCK_MONOTONIC) {
551 static LARGE_INTEGER freq = {{0,0}};
552 LARGE_INTEGER counts;
555 QueryPerformanceCounter(&counts);
556 if (freq.QuadPart == 0)
557 QueryPerformanceFrequency(&freq);
559 tp->tv_sec = counts.QuadPart / freq.QuadPart;
560 /* Get the difference between the number of ns stored
561 * in 'tv_sec' and that stored in 'counts' */
562 t = tp->tv_sec * freq.QuadPart;
563 t = counts.QuadPart - t;
564 /* 't' now contains the number of cycles since the last second.
565 * We want the number of nanoseconds, so multiply out by 1,000,000,000
566 * and then divide by the frequency. */
568 tp->tv_nsec = t / freq.QuadPart;
569 } else if (clock_id == CLOCK_REALTIME) {
570 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
571 * higher-precision field. */
573 gettimeofday(&tv, NULL);
574 tp->tv_sec = tv.tv_sec;
575 tp->tv_nsec = tv.tv_usec * 1000;
584 int mlock(const void * addr, size_t len)
588 HANDLE process = GetCurrentProcess();
590 success = GetProcessWorkingSetSize(process, &min, &max);
592 errno = win_to_posix_error(GetLastError());
598 success = SetProcessWorkingSetSize(process, min, max);
600 errno = win_to_posix_error(GetLastError());
604 success = VirtualLock((LPVOID)addr, len);
606 errno = win_to_posix_error(GetLastError());
613 int munlock(const void * addr, size_t len)
615 BOOL success = VirtualUnlock((LPVOID)addr, len);
618 errno = win_to_posix_error(GetLastError());
625 pid_t waitpid(pid_t pid, int *stat_loc, int options)
627 log_err("%s is not implemented\n", __func__);
632 int usleep(useconds_t useconds)
634 Sleep(useconds / 1000);
638 char *basename(char *path)
640 static char name[MAX_PATH];
643 if (path == NULL || strlen(path) == 0)
646 i = strlen(path) - 1;
648 while (path[i] != '\\' && path[i] != '/' && i >= 0)
651 name[MAX_PATH - 1] = '\0';
652 strncpy(name, path + i + 1, MAX_PATH - 1);
657 int fsync(int fildes)
659 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
660 if (!FlushFileBuffers(hFile)) {
661 errno = win_to_posix_error(GetLastError());
668 int nFileMappings = 0;
669 HANDLE fileMappings[1024];
671 int shmget(key_t key, size_t size, int shmflg)
674 uint32_t size_low = size & 0xFFFFFFFF;
675 uint32_t size_high = ((uint64_t)size) >> 32;
678 hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
679 PAGE_EXECUTE_READWRITE | SEC_RESERVE,
680 size_high, size_low, NULL);
681 if (hMapping != NULL) {
682 fileMappings[nFileMappings] = hMapping;
683 mapid = nFileMappings;
691 void *shmat(int shmid, const void *shmaddr, int shmflg)
694 MEMORY_BASIC_INFORMATION memInfo;
696 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
697 if (mapAddr == NULL) {
698 errno = win_to_posix_error(GetLastError());
702 if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) {
703 errno = win_to_posix_error(GetLastError());
707 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
708 if (mapAddr == NULL) {
709 errno = win_to_posix_error(GetLastError());
716 int shmdt(const void *shmaddr)
718 if (!UnmapViewOfFile(shmaddr)) {
719 errno = win_to_posix_error(GetLastError());
726 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
728 if (cmd == IPC_RMID) {
729 fileMappings[shmid] = INVALID_HANDLE_VALUE;
733 log_err("%s is not implemented\n", __func__);
738 int setuid(uid_t uid)
740 log_err("%s is not implemented\n", __func__);
745 int setgid(gid_t gid)
747 log_err("%s is not implemented\n", __func__);
754 DWORD prioclass = NORMAL_PRIORITY_CLASS;
757 prioclass = HIGH_PRIORITY_CLASS;
759 prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
761 prioclass = IDLE_PRIORITY_CLASS;
763 prioclass = BELOW_NORMAL_PRIORITY_CLASS;
765 if (!SetPriorityClass(GetCurrentProcess(), prioclass))
766 log_err("fio: SetPriorityClass failed\n");
771 int getrusage(int who, struct rusage *r_usage)
773 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
774 FILETIME cTime, eTime, kTime, uTime;
778 memset(r_usage, 0, sizeof(*r_usage));
780 if (who == RUSAGE_SELF) {
781 h = GetCurrentProcess();
782 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
783 } else if (who == RUSAGE_THREAD) {
784 h = GetCurrentThread();
785 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
787 log_err("fio: getrusage %d is not implemented\n", who);
791 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
792 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
794 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
795 r_usage->ru_utime.tv_sec = time;
796 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
797 r_usage->ru_utime.tv_usec = 0;
798 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
799 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
801 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
802 r_usage->ru_stime.tv_sec = time;
803 r_usage->ru_stime.tv_usec = 0;
807 int posix_madvise(void *addr, size_t len, int advice)
812 int fdatasync(int fildes)
814 return fsync(fildes);
817 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
820 int64_t pos = _telli64(fildes);
821 ssize_t len = _write(fildes, buf, nbyte);
823 _lseeki64(fildes, pos, SEEK_SET);
827 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
829 int64_t pos = _telli64(fildes);
830 ssize_t len = read(fildes, buf, nbyte);
832 _lseeki64(fildes, pos, SEEK_SET);
836 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
838 log_err("%s is not implemented\n", __func__);
843 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
846 DWORD bytes_written = 0;
848 for (i = 0; i < iovcnt; i++) {
851 len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
852 if (len == SOCKET_ERROR) {
853 DWORD err = GetLastError();
854 errno = win_to_posix_error(err);
858 bytes_written += len;
861 return bytes_written;
864 long long strtoll(const char *restrict str, char **restrict endptr, int base)
866 return _strtoi64(str, endptr, base);
869 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
872 struct timeval *to = NULL;
873 fd_set readfds, writefds, exceptfds;
879 to->tv_sec = timeout / 1000;
880 to->tv_usec = (timeout % 1000) * 1000;
887 for (i = 0; i < nfds; i++) {
893 if (fds[i].events & POLLIN)
894 FD_SET(fds[i].fd, &readfds);
896 if (fds[i].events & POLLOUT)
897 FD_SET(fds[i].fd, &writefds);
899 FD_SET(fds[i].fd, &exceptfds);
901 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
903 if (rc != SOCKET_ERROR) {
904 for (i = 0; i < nfds; i++) {
908 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
909 fds[i].revents |= POLLIN;
911 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
912 fds[i].revents |= POLLOUT;
914 if (FD_ISSET(fds[i].fd, &exceptfds))
915 fds[i].revents |= POLLHUP;
921 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
925 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
930 ms_remaining = ms_total;
932 /* Since Sleep() can sleep for less than the requested time, add a loop to
933 ensure we only return after the requested length of time has elapsed */
935 fio_gettime(&tv, NULL);
937 ms_remaining = ms_total - mtime_since_now(&tv);
938 } while (ms_remaining > 0 && ms_remaining < ms_total);
940 /* this implementation will never sleep for less than the requested time */
949 DIR *opendir(const char *dirname)
951 struct dirent_ctx *dc = NULL;
954 /* See if we can open it. If not, we'll return an error here */
955 file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
956 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
957 if (file != INVALID_HANDLE_VALUE) {
959 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
960 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
961 dc->find_handle = INVALID_HANDLE_VALUE;
963 DWORD error = GetLastError();
964 if (error == ERROR_FILE_NOT_FOUND)
967 else if (error == ERROR_PATH_NOT_FOUND)
969 else if (error == ERROR_TOO_MANY_OPEN_FILES)
971 else if (error == ERROR_ACCESS_DENIED)
980 int closedir(DIR *dirp)
982 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
983 FindClose(dirp->find_handle);
989 struct dirent *readdir(DIR *dirp)
991 static struct dirent de;
992 WIN32_FIND_DATA find_data;
997 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
998 char search_pattern[MAX_PATH];
1000 StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
1001 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
1002 if (dirp->find_handle == INVALID_HANDLE_VALUE)
1005 if (!FindNextFile(dirp->find_handle, &find_data))
1009 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
1017 log_err("%s is not implemented\n", __func__);
1022 in_addr_t inet_network(const char *cp)
1025 in_addr_t nbo = inet_addr(cp);
1026 hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
1030 #ifdef CONFIG_WINDOWS_XP
1031 const char *inet_ntop(int af, const void *restrict src, char *restrict dst,
1034 INT status = SOCKET_ERROR;
1038 if (af != AF_INET && af != AF_INET6) {
1039 errno = EAFNOSUPPORT;
1043 WSAStartup(MAKEWORD(2,2), &wsd);
1045 if (af == AF_INET) {
1046 struct sockaddr_in si;
1049 memset(&si, 0, sizeof(si));
1051 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
1052 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
1053 } else if (af == AF_INET6) {
1054 struct sockaddr_in6 si6;
1057 memset(&si6, 0, sizeof(si6));
1058 si6.sin6_family = af;
1059 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
1060 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
1063 if (status != SOCKET_ERROR)
1073 int inet_pton(int af, const char *restrict src, void *restrict dst)
1075 INT status = SOCKET_ERROR;
1079 if (af != AF_INET && af != AF_INET6) {
1080 errno = EAFNOSUPPORT;
1084 WSAStartup(MAKEWORD(2,2), &wsd);
1086 if (af == AF_INET) {
1087 struct sockaddr_in si;
1088 INT len = sizeof(si);
1090 memset(&si, 0, sizeof(si));
1092 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
1093 if (status != SOCKET_ERROR)
1094 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
1095 } else if (af == AF_INET6) {
1096 struct sockaddr_in6 si6;
1097 INT len = sizeof(si6);
1099 memset(&si6, 0, sizeof(si6));
1100 si6.sin6_family = af;
1101 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
1102 if (status != SOCKET_ERROR)
1103 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
1106 if (status == SOCKET_ERROR) {
1115 #endif /* CONFIG_WINDOWS_XP */