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