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