javanio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* javanio.c -- implementations of functions in javanio.h.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. /*
  32. * Note, because these functions are trivial, and should be inlined,
  33. * we include this file in the header, and do not compile it.
  34. */
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #ifdef HAVE_SYS_SELECT_H
  40. #include <sys/select.h>
  41. #endif
  42. #include <sys/uio.h>
  43. CPNIO_EXPORT ssize_t
  44. cpnio_read (int fd, void *buf, size_t nbytes)
  45. {
  46. return read (fd, buf, nbytes);
  47. }
  48. CPNIO_EXPORT ssize_t
  49. cpnio_readv (int fd, const struct iovec *iov, int iovcnt)
  50. {
  51. return readv (fd, iov, iovcnt);
  52. }
  53. CPNIO_EXPORT ssize_t
  54. cpnio_write (int fd, const void *buf, size_t nbytes)
  55. {
  56. return write (fd, buf, nbytes);
  57. }
  58. CPNIO_EXPORT ssize_t
  59. cpnio_writev (int fd, const struct iovec *iov, size_t iovcnt)
  60. {
  61. return writev (fd, iov, iovcnt);
  62. }
  63. CPNIO_EXPORT int
  64. cpnio_socket (int domain, int type, int protocol)
  65. {
  66. return socket (domain, type, protocol);
  67. }
  68. CPNIO_EXPORT int
  69. cpnio_connect (int fd, const struct sockaddr *addr, socklen_t addrlen)
  70. {
  71. return connect (fd, addr, addrlen);
  72. }
  73. CPNIO_EXPORT int
  74. cpnio_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
  75. {
  76. fd_set rset;
  77. struct timeval tv;
  78. socklen_t tvlen = sizeof(tv);
  79. int ret;
  80. tv.tv_sec = 0;
  81. tv.tv_usec = 0;
  82. getsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, &tv, &tvlen);
  83. if (tv.tv_sec > 0 || tv.tv_usec > 0)
  84. {
  85. FD_ZERO(&rset);
  86. FD_SET(fd,&rset);
  87. ret = select (fd+1,&rset,NULL,NULL,&tv);
  88. if (ret == 0)
  89. {
  90. errno = EAGAIN;
  91. return -1;
  92. }
  93. }
  94. return accept (fd, addr, addrlen);
  95. }
  96. CPNIO_EXPORT ssize_t
  97. cpnio_sendto (int fd, const void *msg, size_t len, int flags,
  98. const struct sockaddr *to, socklen_t tolen)
  99. {
  100. return sendto (fd, msg, len, flags, to, tolen);
  101. }
  102. CPNIO_EXPORT ssize_t
  103. cpnio_recvfrom (int fd, void *buf, size_t len, int flags,
  104. struct sockaddr *from, socklen_t *fromlen)
  105. {
  106. return recvfrom (fd, buf, len, flags, from, fromlen);
  107. }
  108. CPNIO_EXPORT int
  109. cpnio_fcntl (int fd, int cmd, long arg)
  110. {
  111. #ifdef HAVE_FCNTL
  112. return fcntl (fd, cmd, arg);
  113. #else
  114. errno = ENOSUP;
  115. return -1;
  116. #endif /* HAVE_FCNTL */
  117. }
  118. CPNIO_EXPORT int
  119. cpnio_select (int nfds, fd_set *readfds, fd_set *writefds,
  120. fd_set *excepfds, struct timeval *timeo)
  121. {
  122. return select (nfds, readfds, writefds, excepfds, timeo);
  123. }