poll.c 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <poll.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <sys/fswait.h>
  5. extern char * _argv_0;
  6. int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
  7. int count_pollin = 0;
  8. for (nfds_t i = 0; i < nfds; ++i) {
  9. if (fds[i].events & POLLIN) {
  10. count_pollin++;
  11. }
  12. fds[i].revents = 0;
  13. }
  14. for (nfds_t i = 0; i < nfds; ++i) {
  15. if (fds[i].events & (~POLLIN)) {
  16. fprintf(stderr, "%s: poll: unsupported bit set in fds (this implementation only supports POLLIN)\n", _argv_0);
  17. return -EINVAL;
  18. }
  19. }
  20. int fswait_fds[count_pollin];
  21. int fswait_backref[count_pollin];
  22. int j = 0;
  23. for (nfds_t i = 0; i < nfds; ++i) {
  24. if (fds[i].events & POLLIN) {
  25. fswait_fds[j] = fds[i].fd;
  26. fswait_backref[j] = i;
  27. j++;
  28. }
  29. }
  30. int ret = fswait2(count_pollin, fswait_fds, timeout);
  31. if (ret >= 0 && ret < count_pollin) {
  32. fds[fswait_backref[ret]].revents = POLLIN;
  33. return 1;
  34. } else if (ret == count_pollin) {
  35. return 0;
  36. } else {
  37. return ret; /* Error */
  38. }
  39. }