Get rid of fallocate on Windows
[fio.git] / os / windows / posix.c
CommitLineData
9277ec14
BC
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.
4 */
5
6#include <arpa/inet.h>
7#include <netinet/in.h>
8#include <windows.h>
9#include <stddef.h>
ad9c0fbc 10#include <string.h>
9277ec14
BC
11#include <stdlib.h>
12#include <unistd.h>
13#include <dirent.h>
14#include <pthread.h>
15#include <semaphore.h>
16#include <sys/shm.h>
17#include <sys/mman.h>
18#include <sys/uio.h>
19#include <sys/resource.h>
20#include <sys/poll.h>
21
22#include "../os-windows.h"
671b0600 23#include "../../lib/hweight.h"
9277ec14 24
21c75387
BC
25extern unsigned long mtime_since_now(struct timeval *);
26extern void fio_gettime(struct timeval *, void *);
27
ad9c0fbc
BC
28/* These aren't defined in the MinGW headers */
29HRESULT WINAPI StringCchCopyA(
30 char *pszDest,
31 size_t cchDest,
32 const char *pszSrc);
33
34HRESULT WINAPI StringCchPrintfA(
35 char *pszDest,
36 size_t cchDest,
37 const char *pszFormat,
38 ...);
39
40int vsprintf_s(
41 char *buffer,
42 size_t numberOfElements,
43 const char *format,
44 va_list argptr);
45
671b0600
BC
46int GetNumLogicalProcessors(void)
47{
48 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL;
49 DWORD len = 0;
50 DWORD num_processors = 0;
51 DWORD error = 0;
52 DWORD i;
53
54 while (!GetLogicalProcessorInformation(processor_info, &len)) {
55 error = GetLastError();
56 if (error == ERROR_INSUFFICIENT_BUFFER)
57 processor_info = malloc(len);
58 else {
59 log_err("Error: GetLogicalProcessorInformation failed: %d\n", error);
60 return -1;
61 }
62
63 if (processor_info == NULL) {
64 log_err("Error: failed to allocate memory for GetLogicalProcessorInformation");
65 return -1;
66 }
67 }
68
69 for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
70 {
71 if (processor_info[i].Relationship == RelationProcessorCore)
4ee47af0 72 num_processors += hweight64(processor_info[i].ProcessorMask);
671b0600
BC
73 }
74
75 free(processor_info);
76 return num_processors;
77}
78
9277ec14
BC
79long sysconf(int name)
80{
671b0600 81 long val = -1;
9277ec14
BC
82 SYSTEM_INFO sysInfo;
83 MEMORYSTATUSEX status;
84
85 switch (name)
86 {
87 case _SC_NPROCESSORS_ONLN:
671b0600
BC
88 val = GetNumLogicalProcessors();
89 if (val == -1)
90 log_err("_SC_NPROCESSORS_ONLN failed\n");
91
9277ec14
BC
92 break;
93
94 case _SC_PAGESIZE:
95 GetSystemInfo(&sysInfo);
96 val = sysInfo.dwPageSize;
97 break;
98
99 case _SC_PHYS_PAGES:
100 status.dwLength = sizeof(status);
101 GlobalMemoryStatusEx(&status);
102 val = status.ullTotalPhys;
103 break;
104 default:
105 log_err("sysconf(%d) is not implemented\n", name);
106 break;
107 }
108
109 return val;
110}
111
112char *dl_error = NULL;
113
114int dlclose(void *handle)
115{
116 return !FreeLibrary((HMODULE)handle);
117}
118
119void *dlopen(const char *file, int mode)
120{
121 HMODULE hMod;
122
123 hMod = LoadLibrary(file);
124 if (hMod == INVALID_HANDLE_VALUE)
125 dl_error = (char*)"LoadLibrary failed";
126 else
127 dl_error = NULL;
128
129 return hMod;
130}
131
132void *dlsym(void *handle, const char *name)
133{
134 FARPROC fnPtr;
135
136 fnPtr = GetProcAddress((HMODULE)handle, name);
137 if (fnPtr == NULL)
138 dl_error = (char*)"GetProcAddress failed";
139 else
140 dl_error = NULL;
141
142 return fnPtr;
143}
144
145char *dlerror(void)
146{
147 return dl_error;
148}
149
150int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
151{
152 FILETIME fileTime;
9576f613
BC
153 uint64_t unix_time, windows_time;
154 const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
9277ec14 155
fc0b830f 156 /* Ignore the timezone parameter */
9277ec14
BC
157 (void)tzp;
158
159 /*
160 * Windows time is stored as the number 100 ns intervals since January 1 1601.
161 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
162 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
163 */
164 GetSystemTimeAsFileTime(&fileTime);
9576f613 165 windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
9277ec14
BC
166 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
167 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
168 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
169 tp->tv_sec = unix_time / 1000;
170 tp->tv_usec = (unix_time % 1000) * 1000;
171 return 0;
172}
173
9277ec14
BC
174int sigaction(int sig, const struct sigaction *act,
175 struct sigaction *oact)
176{
e5b8f91c
BC
177 int rc = 0;
178 void (*prev_handler)(int);
179
180 prev_handler = signal(sig, act->sa_handler);
181 if (oact != NULL)
182 oact->sa_handler = prev_handler;
183
184 if (prev_handler == SIG_ERR)
185 rc = -1;
186
187 return rc;
9277ec14
BC
188}
189
190int lstat(const char * path, struct stat * buf)
191{
192 return stat(path, buf);
193}
194
195void *mmap(void *addr, size_t len, int prot, int flags,
196 int fildes, off_t off)
197{
198 DWORD vaProt = 0;
199 void* allocAddr = NULL;
200
201 if (prot & PROT_NONE)
202 vaProt |= PAGE_NOACCESS;
203
204 if ((prot & PROT_READ) && !(prot & PROT_WRITE))
205 vaProt |= PAGE_READONLY;
206
207 if (prot & PROT_WRITE)
208 vaProt |= PAGE_READWRITE;
209
210 if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS))
211 {
212 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
213 }
214
215 return allocAddr;
216}
217
218int munmap(void *addr, size_t len)
219{
220 return !VirtualFree(addr, 0, MEM_RELEASE);
221}
222
223int fork(void)
224{
225 log_err("%s is not implemented\n", __func__);
226 errno = ENOSYS;
227 return (-1);
228}
229
230pid_t setsid(void)
231{
232 log_err("%s is not implemented\n", __func__);
233 errno = ENOSYS;
234 return (-1);
235}
236
ad9c0fbc
BC
237static HANDLE log_file = INVALID_HANDLE_VALUE;
238
9277ec14
BC
239void openlog(const char *ident, int logopt, int facility)
240{
ad9c0fbc
BC
241 if (log_file == INVALID_HANDLE_VALUE)
242 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
9277ec14
BC
243}
244
245void closelog(void)
246{
ad9c0fbc
BC
247 CloseHandle(log_file);
248 log_file = INVALID_HANDLE_VALUE;
249}
250
251void syslog(int priority, const char *message, ... /* argument */)
252{
253 va_list v;
254 int len;
255 char *output;
256 DWORD bytes_written;
257
258 if (log_file == INVALID_HANDLE_VALUE) {
259 log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
260 }
261
262 if (log_file == INVALID_HANDLE_VALUE) {
263 log_err("syslog: failed to open log file\n");
264 return;
265 }
266
267 va_start(v, message);
268 len = _vscprintf(message, v);
269 output = malloc(len + sizeof(char));
98dc2db5 270 vsprintf(output, message, v);
ad9c0fbc
BC
271 WriteFile(log_file, output, len, &bytes_written, NULL);
272 va_end(v);
98dc2db5 273 free(output);
9277ec14
BC
274}
275
276int kill(pid_t pid, int sig)
277{
278 errno = ESRCH;
279 return (-1);
280}
281
fc0b830f
BC
282/*
283 * This is assumed to be used only by the network code,
284 * and so doesn't try and handle any of the other cases
285 */
9277ec14
BC
286int fcntl(int fildes, int cmd, ...)
287{
fc0b830f
BC
288 /*
289 * non-blocking mode doesn't work the same as in BSD sockets,
290 * so ignore it.
291 */
9277ec14
BC
292#if 0
293 va_list ap;
294 int val, opt, status;
295
296 if (cmd == F_GETFL)
297 return 0;
298 else if (cmd != F_SETFL) {
299 errno = EINVAL;
300 return (-1);
301 }
302
303 va_start(ap, 1);
304
305 opt = va_arg(ap, int);
306 if (opt & O_NONBLOCK)
307 val = 1;
308 else
309 val = 0;
310
311 status = ioctlsocket((SOCKET)fildes, opt, &val);
312
313 if (status == SOCKET_ERROR) {
314 errno = EINVAL;
315 val = -1;
316 }
317
318 va_end(ap);
319
320 return val;
321#endif
322return 0;
323}
324
325/*
326 * Get the value of a local clock source.
327 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
328 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
329 */
330int clock_gettime(clockid_t clock_id, struct timespec *tp)
331{
332 int rc = 0;
333
334 if (clock_id == CLOCK_MONOTONIC)
335 {
336 static LARGE_INTEGER freq = {{0,0}};
337 LARGE_INTEGER counts;
338
339 QueryPerformanceCounter(&counts);
340 if (freq.QuadPart == 0)
341 QueryPerformanceFrequency(&freq);
342
343 tp->tv_sec = counts.QuadPart / freq.QuadPart;
344 /* Get the difference between the number of ns stored
345 * in 'tv_sec' and that stored in 'counts' */
9576f613 346 uint64_t t = tp->tv_sec * freq.QuadPart;
9277ec14
BC
347 t = counts.QuadPart - t;
348 /* 't' now contains the number of cycles since the last second.
349 * We want the number of nanoseconds, so multiply out by 1,000,000,000
350 * and then divide by the frequency. */
351 t *= 1000000000;
352 tp->tv_nsec = t / freq.QuadPart;
353 }
354 else if (clock_id == CLOCK_REALTIME)
355 {
356 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
357 * higher-precision field. */
358 struct timeval tv;
359 gettimeofday(&tv, NULL);
360 tp->tv_sec = tv.tv_sec;
361 tp->tv_nsec = tv.tv_usec * 1000;
362 } else {
363 errno = EINVAL;
364 rc = -1;
365 }
366
367 return rc;
368}
369
370int mlock(const void * addr, size_t len)
371{
372 return !VirtualLock((LPVOID)addr, len);
373}
374
375int munlock(const void * addr, size_t len)
376{
377 return !VirtualUnlock((LPVOID)addr, len);
378}
379
380pid_t waitpid(pid_t pid, int *stat_loc, int options)
381{
382 log_err("%s is not implemented\n", __func__);
383 errno = ENOSYS;
384 return -1;
385}
386
387int usleep(useconds_t useconds)
388{
389 Sleep(useconds / 1000);
390 return 0;
391}
392
393char *basename(char *path)
394{
395 static char name[MAX_PATH];
396 int i;
397
398 if (path == NULL || strlen(path) == 0)
399 return (char*)".";
400
401 i = strlen(path) - 1;
402
9576f613 403 while (path[i] != '\\' && path[i] != '/' && i >= 0)
9277ec14
BC
404 i--;
405
9576f613 406 strncpy(name, path + i + 1, MAX_PATH);
9277ec14
BC
407
408 return name;
409}
410
9277ec14
BC
411int ftruncate(int fildes, off_t length)
412{
413 BOOL bSuccess;
9576f613
BC
414 int64_t prev_pos = _telli64(fildes);
415 _lseeki64(fildes, length, SEEK_SET);
9277ec14
BC
416 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
417 bSuccess = SetEndOfFile(hFile);
9576f613 418 _lseeki64(fildes, prev_pos, SEEK_SET);
9277ec14
BC
419 return !bSuccess;
420}
421
422int fsync(int fildes)
423{
424 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
425 return !FlushFileBuffers(hFile);
426}
427
428int nFileMappings = 0;
429HANDLE fileMappings[1024];
430
431int shmget(key_t key, size_t size, int shmflg)
432{
433 int mapid = -1;
d3987946
BC
434 uint32_t size_low = size & 0xFFFFFFFF;
435 uint32_t size_high = ((uint64_t)size) >> 32;
436 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL);
9277ec14
BC
437 if (hMapping != NULL) {
438 fileMappings[nFileMappings] = hMapping;
439 mapid = nFileMappings;
440 nFileMappings++;
441 } else {
442 errno = ENOSYS;
443 }
444
445 return mapid;
446}
447
448void *shmat(int shmid, const void *shmaddr, int shmflg)
449{
450 void* mapAddr;
451 MEMORY_BASIC_INFORMATION memInfo;
452 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
453 VirtualQuery(mapAddr, &memInfo, sizeof(memInfo));
454 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
455 return mapAddr;
456}
457
458int shmdt(const void *shmaddr)
459{
460 return !UnmapViewOfFile(shmaddr);
461}
462
463int shmctl(int shmid, int cmd, struct shmid_ds *buf)
464{
465 if (cmd == IPC_RMID) {
466 fileMappings[shmid] = INVALID_HANDLE_VALUE;
467 return 0;
468 } else {
469 log_err("%s is not implemented\n", __func__);
470 }
471 return (-1);
472}
473
474int setuid(uid_t uid)
475{
476 log_err("%s is not implemented\n", __func__);
477 errno = ENOSYS;
478 return (-1);
479}
480
481int setgid(gid_t gid)
482{
483 log_err("%s is not implemented\n", __func__);
484 errno = ENOSYS;
485 return (-1);
486}
487
488int nice(int incr)
489{
490 if (incr != 0) {
491 errno = EINVAL;
492 return -1;
493 }
494
495 return 0;
496}
497
498int getrusage(int who, struct rusage *r_usage)
499{
9576f613 500 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
9277ec14
BC
501 FILETIME cTime, eTime, kTime, uTime;
502 time_t time;
7732a09b 503 HANDLE h;
9277ec14
BC
504
505 memset(r_usage, 0, sizeof(*r_usage));
506
7732a09b
HL
507 if (who == RUSAGE_SELF) {
508 h = GetCurrentProcess();
509 GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime);
510 } else if (who == RUSAGE_THREAD) {
511 h = GetCurrentThread();
512 GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime);
513 } else {
514 log_err("fio: getrusage %d is not implemented\n", who);
515 return -1;
516 }
517
9576f613 518 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
9277ec14
BC
519 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
520 * 1601 to 1970 */
521 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
522 r_usage->ru_utime.tv_sec = time;
523 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
524 r_usage->ru_utime.tv_usec = 0;
9576f613 525 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
9277ec14
BC
526 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
527 * 1601 to 1970 */
528 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
529 r_usage->ru_stime.tv_sec = time;
530 r_usage->ru_stime.tv_usec = 0;
531 return 0;
532}
533
2277d5d5
BC
534int posix_fadvise(int fd, off_t offset, off_t len, int advice)
535{
536 return 0;
537}
538
9277ec14
BC
539int posix_madvise(void *addr, size_t len, int advice)
540{
541 log_err("%s is not implemented\n", __func__);
542 return ENOSYS;
543}
544
fc0b830f 545/* Windows doesn't support advice for memory pages. Just ignore it. */
9277ec14
BC
546int msync(void *addr, size_t len, int flags)
547{
9277ec14
BC
548 errno = ENOSYS;
549 return -1;
550}
551
552int fdatasync(int fildes)
553{
554 return fsync(fildes);
555}
556
557ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
558 off_t offset)
559{
9576f613
BC
560 int64_t pos = _telli64(fildes);
561 ssize_t len = _write(fildes, buf, nbyte);
562 _lseeki64(fildes, pos, SEEK_SET);
9277ec14
BC
563 return len;
564}
565
566ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
567{
9576f613 568 int64_t pos = _telli64(fildes);
9277ec14 569 ssize_t len = read(fildes, buf, nbyte);
9576f613 570 _lseeki64(fildes, pos, SEEK_SET);
9277ec14
BC
571 return len;
572}
573
574ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
575{
576 log_err("%s is not implemented\n", __func__);
577 errno = ENOSYS;
578 return (-1);
579}
580
581ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
582{
583 log_err("%s is not implemented\n", __func__);
584 errno = ENOSYS;
585 return (-1);
586}
587
588long long strtoll(const char *restrict str, char **restrict endptr,
589 int base)
590{
591 return _strtoi64(str, endptr, base);
592}
593
9277ec14
BC
594int poll(struct pollfd fds[], nfds_t nfds, int timeout)
595{
596 struct timeval tv;
597 struct timeval *to = NULL;
598 fd_set readfds, writefds, exceptfds;
599 int i;
600 int rc;
601
3f457bea 602 if (timeout != -1) {
f9a58c2a 603 to = &tv;
3f457bea
BC
604 to->tv_sec = timeout / 1000;
605 to->tv_usec = (timeout % 1000) * 1000;
606 }
9277ec14
BC
607
608 FD_ZERO(&readfds);
609 FD_ZERO(&writefds);
610 FD_ZERO(&exceptfds);
611
612 for (i = 0; i < nfds; i++)
613 {
614 if (fds[i].fd < 0) {
615 fds[i].revents = 0;
616 continue;
617 }
618
619 if (fds[i].events & POLLIN)
620 FD_SET(fds[i].fd, &readfds);
621
622 if (fds[i].events & POLLOUT)
623 FD_SET(fds[i].fd, &writefds);
624
f9a58c2a 625 FD_SET(fds[i].fd, &exceptfds);
9277ec14
BC
626 }
627
628 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
629
630 if (rc != SOCKET_ERROR) {
631 for (i = 0; i < nfds; i++)
632 {
633 if (fds[i].fd < 0) {
634 continue;
635 }
636
637 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
638 fds[i].revents |= POLLIN;
639
640 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
641 fds[i].revents |= POLLOUT;
642
643 if (FD_ISSET(fds[i].fd, &exceptfds))
644 fds[i].revents |= POLLHUP;
645 }
646 }
647
648 return rc;
649}
650
651int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
652{
21c75387
BC
653 struct timeval tv;
654 DWORD ms_remaining;
655 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
656
657 if (ms_total == 0)
658 ms_total = 1;
659
660 ms_remaining = ms_total;
661
662 /* Since Sleep() can sleep for less than the requested time, add a loop to
663 ensure we only return after the requested length of time has elapsed */
664 do {
665 fio_gettime(&tv, NULL);
666 Sleep(ms_remaining);
667 ms_remaining = ms_total - mtime_since_now(&tv);
668 } while (ms_remaining > 0 && ms_remaining < ms_total);
669
670 /* this implementation will never sleep for less than the requested time */
671 if (rmtp != NULL) {
672 rmtp->tv_sec = 0;
673 rmtp->tv_nsec = 0;
674 }
675
676 return 0;
9277ec14
BC
677}
678
679DIR *opendir(const char *dirname)
680{
ad9c0fbc
BC
681 struct dirent_ctx *dc = NULL;
682
683 /* See if we can open it. If not, we'll return an error here */
684 HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
685 if (file != INVALID_HANDLE_VALUE) {
686 CloseHandle(file);
687 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
688 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
689 dc->find_handle = INVALID_HANDLE_VALUE;
690 } else {
691 DWORD error = GetLastError();
692 if (error == ERROR_FILE_NOT_FOUND)
693 errno = ENOENT;
694
695 else if (error == ERROR_PATH_NOT_FOUND)
696 errno = ENOTDIR;
697 else if (error == ERROR_TOO_MANY_OPEN_FILES)
698 errno = ENFILE;
699 else if (error == ERROR_ACCESS_DENIED)
700 errno = EACCES;
701 else
702 errno = error;
703 }
704
705 return dc;
9277ec14
BC
706}
707
708int closedir(DIR *dirp)
709{
ad9c0fbc
BC
710 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
711 FindClose(dirp->find_handle);
712
713 free(dirp);
714 return 0;
9277ec14
BC
715}
716
717struct dirent *readdir(DIR *dirp)
718{
ad9c0fbc
BC
719 static struct dirent de;
720 WIN32_FIND_DATA find_data;
721
722 if (dirp == NULL)
723 return NULL;
724
725 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
726 char search_pattern[MAX_PATH];
727 StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
728 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
729 if (dirp->find_handle == INVALID_HANDLE_VALUE)
730 return NULL;
731 } else {
732 if (!FindNextFile(dirp->find_handle, &find_data))
733 return NULL;
734 }
735
736 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
737 de.d_ino = 0;
738
739 return &de;
9277ec14
BC
740}
741
742uid_t geteuid(void)
743{
744 log_err("%s is not implemented\n", __func__);
745 errno = ENOSYS;
746 return -1;
747}
748
9277ec14
BC
749const char* inet_ntop(int af, const void *restrict src,
750 char *restrict dst, socklen_t size)
751{
752 INT status = SOCKET_ERROR;
753 WSADATA wsd;
754 char *ret = NULL;
755
756 if (af != AF_INET && af != AF_INET6) {
757 errno = EAFNOSUPPORT;
758 return NULL;
759 }
760
761 WSAStartup(MAKEWORD(2,2), &wsd);
762
763 if (af == AF_INET) {
764 struct sockaddr_in si;
765 DWORD len = size;
766 memset(&si, 0, sizeof(si));
767 si.sin_family = af;
768 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
769 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
770 } else if (af == AF_INET6) {
771 struct sockaddr_in6 si6;
772 DWORD len = size;
773 memset(&si6, 0, sizeof(si6));
774 si6.sin6_family = af;
775 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
776 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
777 }
778
779 if (status != SOCKET_ERROR)
780 ret = dst;
781 else
782 errno = ENOSPC;
783
784 WSACleanup();
3f457bea 785
9277ec14
BC
786 return ret;
787}
788
789int inet_pton(int af, const char *restrict src, void *restrict dst)
790{
791 INT status = SOCKET_ERROR;
792 WSADATA wsd;
793 int ret = 1;
794
795 if (af != AF_INET && af != AF_INET6) {
796 errno = EAFNOSUPPORT;
797 return -1;
798 }
799
800 WSAStartup(MAKEWORD(2,2), &wsd);
801
802 if (af == AF_INET) {
803 struct sockaddr_in si;
804 INT len = sizeof(si);
805 memset(&si, 0, sizeof(si));
806 si.sin_family = af;
807 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
808 if (status != SOCKET_ERROR)
809 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
810 } else if (af == AF_INET6) {
811 struct sockaddr_in6 si6;
812 INT len = sizeof(si6);
813 memset(&si6, 0, sizeof(si6));
814 si6.sin6_family = af;
815 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
816 if (status != SOCKET_ERROR)
817 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
818 }
819
820 if (status == SOCKET_ERROR) {
821 errno = ENOSPC;
822 ret = 0;
823 }
824
825 WSACleanup();
826
827 return ret;
828}