io.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /** @file io.c
  2. * @brief Enchanced base IO functions.
  3. */
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/uio.h>
  12. /** @brief Writes data to file.
  13. *
  14. * The system write() can be interrupted by signal. This function will retry to
  15. * write in a case of interrupted call.
  16. *
  17. * @param [in] fd File descriptor.
  18. * @param [in] buf Buffer to write.
  19. * @param [in] n Number of bytes to write.
  20. * @return Number of bytes written or < 0 on error.
  21. */
  22. ssize_t faux_write(int fd, const void *buf, size_t n)
  23. {
  24. ssize_t bytes_written = 0;
  25. assert(fd != -1);
  26. assert(buf);
  27. if ((-1 == fd) || !buf)
  28. return -1;
  29. if (0 == n)
  30. return 0;
  31. do {
  32. bytes_written = write(fd, buf, n);
  33. } while ((bytes_written < 0) && (EINTR == errno));
  34. return bytes_written;
  35. }
  36. /** @brief Writes data block to file.
  37. *
  38. * The system write() can be interrupted by signal or can write less bytes
  39. * than specified. This function will continue to write data until all data
  40. * will be written or error occured.
  41. *
  42. * @param [in] fd File descriptor.
  43. * @param [in] buf Buffer to write.
  44. * @param [in] n Number of bytes to write.
  45. * @return Number of bytes written.
  46. * < n then insufficient space or error (but some data was already written).
  47. * < 0 - error.
  48. */
  49. ssize_t faux_write_block(int fd, const void *buf, size_t n)
  50. {
  51. ssize_t bytes_written = 0;
  52. size_t total_written = 0;
  53. size_t left = n;
  54. const void *data = buf;
  55. do {
  56. bytes_written = faux_write(fd, data, left);
  57. if (bytes_written < 0) { // Error
  58. if (total_written != 0)
  59. return total_written;
  60. return -1;
  61. }
  62. if (0 == bytes_written) // Insufficient space
  63. return total_written;
  64. data += bytes_written;
  65. left = left - bytes_written;
  66. total_written += bytes_written;
  67. } while (left > 0);
  68. return total_written;
  69. }
  70. /** @brief Reads data from file.
  71. *
  72. * The system read() can be interrupted by signal. This function will retry to
  73. * read if it was interrupted by signal.
  74. *
  75. * @param [in] fd File descriptor.
  76. * @param [in] buf Buffer to write.
  77. * @param [in] n Number of bytes to write.
  78. * @return Number of bytes readed or < 0 on error.
  79. * 0 bytes indicates EOF
  80. */
  81. ssize_t faux_read(int fd, void *buf, size_t n)
  82. {
  83. ssize_t bytes_readed = 0;
  84. assert(fd != -1);
  85. assert(buf);
  86. if ((-1 == fd) || !buf)
  87. return -1;
  88. if (0 == n)
  89. return 0;
  90. do {
  91. bytes_readed = read(fd, buf, n);
  92. } while ((bytes_readed < 0) && (EINTR == errno));
  93. return bytes_readed;
  94. }
  95. /** @brief Reads data block from file.
  96. *
  97. * The system read() can be interrupted by signal or can read less bytes
  98. * than specified. This function will continue to read data until all data
  99. * will be readed or error occured.
  100. *
  101. * @param [in] fd File descriptor.
  102. * @param [in] buf Buffer to write.
  103. * @param [in] n Number of bytes to write.
  104. * @return Number of bytes readed.
  105. * < n EOF or error (but some data was already readed).
  106. * < 0 Error.
  107. */
  108. size_t faux_read_block(int fd, void *buf, size_t n)
  109. {
  110. ssize_t bytes_readed = 0;
  111. size_t total_readed = 0;
  112. size_t left = n;
  113. void *data = buf;
  114. do {
  115. bytes_readed = read(fd, data, left);
  116. if (bytes_readed < 0) {
  117. if (total_readed != 0)
  118. return total_readed;
  119. return -1;
  120. }
  121. if (0 == bytes_readed) // EOF
  122. return total_readed;
  123. data += bytes_readed;
  124. left = left - bytes_readed;
  125. total_readed += bytes_readed;
  126. } while (left > 0);
  127. return total_readed;
  128. }