io.c 3.3 KB

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