]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/cloudlibc/src/libc/sys/select/select.c
5d4bc934aab038d977e2894b741b8e5873691afe
[wasi-libc.git] / libc-bottom-half / cloudlibc / src / libc / sys / select / select.c
1 // Copyright (c) 2015 Nuxi, https://nuxi.nl/
2 //
3 // SPDX-License-Identifier: BSD-2-Clause
4
5 #include <sys/select.h>
6
7 #include <errno.h>
8 #include <stddef.h>
9
10 int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
11 fd_set *restrict errorfds, struct timeval *restrict timeout) {
12 if (timeout != NULL) {
13 // Timeout specified.
14 if (timeout->tv_usec < 0 || timeout->tv_usec >= 1000000) {
15 errno = EINVAL;
16 return -1;
17 }
18 struct timespec ts = {.tv_sec = timeout->tv_sec,
19 .tv_nsec = (long)timeout->tv_usec * 1000};
20 #ifdef __wasilibc_unmodified_upstream
21 return pselect(nfds, readfds, writefds, errorfds, &ts);
22 #else
23 return pselect(nfds, readfds, writefds, errorfds, &ts, NULL);
24 #endif
25 } else {
26 // No timeout specified.
27 #ifdef __wasilibc_unmodified_upstream
28 return pselect(nfds, readfds, writefds, errorfds, NULL);
29 #else
30 return pselect(nfds, readfds, writefds, errorfds, NULL, NULL);
31 #endif
32 }
33 }