Merge branch 'master' into gfio
[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
2277d5d5
BC
538int posix_fadvise(int fd, off_t offset, off_t len, int advice)
539{
540 return 0;
541}
542
9277ec14
BC
543int posix_madvise(void *addr, size_t len, int advice)
544{
545 log_err("%s is not implemented\n", __func__);
546 return ENOSYS;
547}
548
fc0b830f 549/* Windows doesn't support advice for memory pages. Just ignore it. */
9277ec14
BC
550int msync(void *addr, size_t len, int flags)
551{
9277ec14
BC
552 errno = ENOSYS;
553 return -1;
554}
555
556int fdatasync(int fildes)
557{
558 return fsync(fildes);
559}
560
561ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
562 off_t offset)
563{
9576f613
BC
564 int64_t pos = _telli64(fildes);
565 ssize_t len = _write(fildes, buf, nbyte);
566 _lseeki64(fildes, pos, SEEK_SET);
9277ec14
BC
567 return len;
568}
569
570ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
571{
9576f613 572 int64_t pos = _telli64(fildes);
9277ec14 573 ssize_t len = read(fildes, buf, nbyte);
9576f613 574 _lseeki64(fildes, pos, SEEK_SET);
9277ec14
BC
575 return len;
576}
577
578ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
579{
580 log_err("%s is not implemented\n", __func__);
581 errno = ENOSYS;
582 return (-1);
583}
584
585ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
586{
587 log_err("%s is not implemented\n", __func__);
588 errno = ENOSYS;
589 return (-1);
590}
591
592long long strtoll(const char *restrict str, char **restrict endptr,
593 int base)
594{
595 return _strtoi64(str, endptr, base);
596}
597
598char *strsep(char **stringp, const char *delim)
599{
600 char *orig = *stringp;
601 BOOL gotMatch = FALSE;
602 int i = 0;
603 int j = 0;
604
605 if (*stringp == NULL)
606 return NULL;
607
608 while ((*stringp)[i] != '\0') {
609 j = 0;
610 while (delim[j] != '\0') {
611 if ((*stringp)[i] == delim[j]) {
612 gotMatch = TRUE;
613 (*stringp)[i] = '\0';
614 *stringp = *stringp + i + 1;
615 break;
616 }
617 j++;
618 }
619 if (gotMatch)
620 break;
621
622 i++;
623 }
624
625 if (!gotMatch)
626 *stringp = NULL;
627
628 return orig;
629}
630
631int poll(struct pollfd fds[], nfds_t nfds, int timeout)
632{
633 struct timeval tv;
634 struct timeval *to = NULL;
635 fd_set readfds, writefds, exceptfds;
636 int i;
637 int rc;
638
3f457bea 639 if (timeout != -1) {
f9a58c2a 640 to = &tv;
3f457bea
BC
641 to->tv_sec = timeout / 1000;
642 to->tv_usec = (timeout % 1000) * 1000;
643 }
9277ec14
BC
644
645 FD_ZERO(&readfds);
646 FD_ZERO(&writefds);
647 FD_ZERO(&exceptfds);
648
649 for (i = 0; i < nfds; i++)
650 {
651 if (fds[i].fd < 0) {
652 fds[i].revents = 0;
653 continue;
654 }
655
656 if (fds[i].events & POLLIN)
657 FD_SET(fds[i].fd, &readfds);
658
659 if (fds[i].events & POLLOUT)
660 FD_SET(fds[i].fd, &writefds);
661
f9a58c2a 662 FD_SET(fds[i].fd, &exceptfds);
9277ec14
BC
663 }
664
665 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
666
667 if (rc != SOCKET_ERROR) {
668 for (i = 0; i < nfds; i++)
669 {
670 if (fds[i].fd < 0) {
671 continue;
672 }
673
674 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
675 fds[i].revents |= POLLIN;
676
677 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
678 fds[i].revents |= POLLOUT;
679
680 if (FD_ISSET(fds[i].fd, &exceptfds))
681 fds[i].revents |= POLLHUP;
682 }
683 }
684
685 return rc;
686}
687
688int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
689{
21c75387
BC
690 struct timeval tv;
691 DWORD ms_remaining;
692 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
693
694 if (ms_total == 0)
695 ms_total = 1;
696
697 ms_remaining = ms_total;
698
699 /* Since Sleep() can sleep for less than the requested time, add a loop to
700 ensure we only return after the requested length of time has elapsed */
701 do {
702 fio_gettime(&tv, NULL);
703 Sleep(ms_remaining);
704 ms_remaining = ms_total - mtime_since_now(&tv);
705 } while (ms_remaining > 0 && ms_remaining < ms_total);
706
707 /* this implementation will never sleep for less than the requested time */
708 if (rmtp != NULL) {
709 rmtp->tv_sec = 0;
710 rmtp->tv_nsec = 0;
711 }
712
713 return 0;
9277ec14
BC
714}
715
716DIR *opendir(const char *dirname)
717{
ad9c0fbc
BC
718 struct dirent_ctx *dc = NULL;
719
720 /* See if we can open it. If not, we'll return an error here */
721 HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
722 if (file != INVALID_HANDLE_VALUE) {
723 CloseHandle(file);
724 dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
725 StringCchCopyA(dc->dirname, MAX_PATH, dirname);
726 dc->find_handle = INVALID_HANDLE_VALUE;
727 } else {
728 DWORD error = GetLastError();
729 if (error == ERROR_FILE_NOT_FOUND)
730 errno = ENOENT;
731
732 else if (error == ERROR_PATH_NOT_FOUND)
733 errno = ENOTDIR;
734 else if (error == ERROR_TOO_MANY_OPEN_FILES)
735 errno = ENFILE;
736 else if (error == ERROR_ACCESS_DENIED)
737 errno = EACCES;
738 else
739 errno = error;
740 }
741
742 return dc;
9277ec14
BC
743}
744
745int closedir(DIR *dirp)
746{
ad9c0fbc
BC
747 if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
748 FindClose(dirp->find_handle);
749
750 free(dirp);
751 return 0;
9277ec14
BC
752}
753
754struct dirent *readdir(DIR *dirp)
755{
ad9c0fbc
BC
756 static struct dirent de;
757 WIN32_FIND_DATA find_data;
758
759 if (dirp == NULL)
760 return NULL;
761
762 if (dirp->find_handle == INVALID_HANDLE_VALUE) {
763 char search_pattern[MAX_PATH];
764 StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
765 dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
766 if (dirp->find_handle == INVALID_HANDLE_VALUE)
767 return NULL;
768 } else {
769 if (!FindNextFile(dirp->find_handle, &find_data))
770 return NULL;
771 }
772
773 StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
774 de.d_ino = 0;
775
776 return &de;
9277ec14
BC
777}
778
779uid_t geteuid(void)
780{
781 log_err("%s is not implemented\n", __func__);
782 errno = ENOSYS;
783 return -1;
784}
785
9277ec14
BC
786const char* inet_ntop(int af, const void *restrict src,
787 char *restrict dst, socklen_t size)
788{
789 INT status = SOCKET_ERROR;
790 WSADATA wsd;
791 char *ret = NULL;
792
793 if (af != AF_INET && af != AF_INET6) {
794 errno = EAFNOSUPPORT;
795 return NULL;
796 }
797
798 WSAStartup(MAKEWORD(2,2), &wsd);
799
800 if (af == AF_INET) {
801 struct sockaddr_in si;
802 DWORD len = size;
803 memset(&si, 0, sizeof(si));
804 si.sin_family = af;
805 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
806 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
807 } else if (af == AF_INET6) {
808 struct sockaddr_in6 si6;
809 DWORD len = size;
810 memset(&si6, 0, sizeof(si6));
811 si6.sin6_family = af;
812 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
813 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
814 }
815
816 if (status != SOCKET_ERROR)
817 ret = dst;
818 else
819 errno = ENOSPC;
820
821 WSACleanup();
3f457bea 822
9277ec14
BC
823 return ret;
824}
825
3f457bea
BC
826int inet_aton(const char *cp, struct in_addr *inp)
827{
828 return inet_pton(AF_INET, cp, inp);
829}
830
9277ec14
BC
831int inet_pton(int af, const char *restrict src, void *restrict dst)
832{
833 INT status = SOCKET_ERROR;
834 WSADATA wsd;
835 int ret = 1;
836
837 if (af != AF_INET && af != AF_INET6) {
838 errno = EAFNOSUPPORT;
839 return -1;
840 }
841
842 WSAStartup(MAKEWORD(2,2), &wsd);
843
844 if (af == AF_INET) {
845 struct sockaddr_in si;
846 INT len = sizeof(si);
847 memset(&si, 0, sizeof(si));
848 si.sin_family = af;
849 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
850 if (status != SOCKET_ERROR)
851 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
852 } else if (af == AF_INET6) {
853 struct sockaddr_in6 si6;
854 INT len = sizeof(si6);
855 memset(&si6, 0, sizeof(si6));
856 si6.sin6_family = af;
857 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
858 if (status != SOCKET_ERROR)
859 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
860 }
861
862 if (status == SOCKET_ERROR) {
863 errno = ENOSPC;
864 ret = 0;
865 }
866
867 WSACleanup();
868
869 return ret;
870}