summaryrefslogtreecommitdiff
path: root/src/libc/select.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libc/select.c')
-rw-r--r--src/libc/select.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libc/select.c b/src/libc/select.c
new file mode 100644
index 0000000..e3e95e4
--- /dev/null
+++ b/src/libc/select.c
@@ -0,0 +1,47 @@
+#include <errno.h>
+#include <sys/select.h>
+#include <stdio.h>
+
+static int
+countset(int nfds, fd_set *set)
+{
+ int count = 0;
+ if (set == NULL) return 0;
+ for (int i = 0; i < nfds; i++) {
+ if (FD_ISSET(i, set)) {
+ count++;
+ }
+ }
+ return count;
+}
+
+int
+select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
+{
+ (void)timeout;
+ FD_ZERO(exceptfds);
+ if (countset(nfds, readfds) != 0) {
+ return errno = ENOSYS, -1;
+ }
+ return countset(nfds, writefds); /* assume everything is ready for writing */
+}
+
+void FD_CLR(int fd, fd_set *set) {
+ if (0 <= fd && fd < FD_SETSIZE) {
+ *set = *set & ~(1<<fd);
+ }
+}
+
+int FD_ISSET(int fd, fd_set *set) {
+ return 0 <= fd && fd < FD_SETSIZE && (*set & (1 << fd));
+}
+
+void FD_SET(int fd, fd_set *set) {
+ if (0 <= fd && fd < FD_SETSIZE) {
+ *set = *set | (1<<fd);
+ }
+}
+
+void FD_ZERO(fd_set *set) {
+ *set = 0;
+}