/crypto/external/bsd/heimdal/dist/lib/roken/net_read.c

http://www.minix3.org/ · C · 120 lines · 65 code · 16 blank · 39 comment · 19 complexity · 079365e12ba5abaab99d31c7bc92548b MD5 · raw file

  1. /* $NetBSD: net_read.c,v 1.1.1.1 2011/04/13 18:15:42 elric Exp $ */
  2. /*
  3. * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska Hรถgskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <krb5/roken.h>
  36. /*
  37. * Like read but never return partial data.
  38. */
  39. #ifndef _WIN32
  40. ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
  41. net_read (rk_socket_t fd, void *buf, size_t nbytes)
  42. {
  43. char *cbuf = (char *)buf;
  44. ssize_t count;
  45. size_t rem = nbytes;
  46. while (rem > 0) {
  47. count = read (fd, cbuf, rem);
  48. if (count < 0) {
  49. if (errno == EINTR)
  50. continue;
  51. else
  52. return count;
  53. } else if (count == 0) {
  54. return count;
  55. }
  56. cbuf += count;
  57. rem -= count;
  58. }
  59. return nbytes;
  60. }
  61. #else
  62. ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
  63. net_read(rk_socket_t sock, void *buf, size_t nbytes)
  64. {
  65. char *cbuf = (char *)buf;
  66. ssize_t count;
  67. size_t rem = nbytes;
  68. #ifdef SOCKET_IS_NOT_AN_FD
  69. int use_read = 0;
  70. #endif
  71. while (rem > 0) {
  72. #ifdef SOCKET_IS_NOT_AN_FD
  73. if (use_read)
  74. count = _read (sock, cbuf, rem);
  75. else
  76. count = recv (sock, cbuf, rem, 0);
  77. if (use_read == 0 &&
  78. rk_IS_SOCKET_ERROR(count) &&
  79. (rk_SOCK_ERRNO == WSANOTINITIALISED ||
  80. rk_SOCK_ERRNO == WSAENOTSOCK)) {
  81. use_read = 1;
  82. count = _read (sock, cbuf, rem);
  83. }
  84. #else
  85. count = recv (sock, cbuf, rem, 0);
  86. #endif
  87. if (count < 0) {
  88. /* With WinSock, the error EINTR (WSAEINTR), is used to
  89. indicate that a blocking call was cancelled using
  90. WSACancelBlockingCall(). */
  91. #ifndef HAVE_WINSOCK
  92. if (rk_SOCK_ERRNO == EINTR)
  93. continue;
  94. #endif
  95. return count;
  96. } else if (count == 0) {
  97. return count;
  98. }
  99. cbuf += count;
  100. rem -= count;
  101. }
  102. return nbytes;
  103. }
  104. #endif