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