file.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /** @file file.c
  2. * @brief Functions for working with files.
  3. *
  4. * This library was created to exclude glibc's file stream operations like
  5. * fopen(), fgets() etc. These functions use glibc internal buffer. To work
  6. * with buffer glibc has its own fflush() function and special behaviour while
  7. * fclose(). It brings a problems with stream file objects and system file
  8. * descriptors while fork(). The file streams and system file descriptors can't
  9. * be used interchangeably. So faux file library uses standard system file
  10. * operations like open(), read() and emulate some usefull stream function like
  11. * getline(). The faux file object has own buffer and doesn't use glibc's one.
  12. * The faux_file_close() doesn't lseek() file descriptor as fclose() can do.
  13. * You can use faux file object and standard file operation in the same time.
  14. * The only thing to remember is internal buffer that can contain already
  15. * readed bytes.
  16. */
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include "private.h"
  26. #include "faux/faux.h"
  27. #include "faux/file.h"
  28. #include "faux/str.h"
  29. /** @brief Create file object using existent fd.
  30. *
  31. * Create file object an link it to existent file descriptor.
  32. *
  33. * @return Allocated and initialized file object or NULL on error.
  34. */
  35. faux_file_t *faux_file_fdopen(int fd) {
  36. struct stat stat_struct = {};
  37. faux_file_t *f = NULL;
  38. // Before object creation check is fd valid.
  39. // Try to get stat().
  40. if (fstat(fd, &stat_struct) < 0)
  41. return NULL; // Illegal fd
  42. f = faux_zmalloc(sizeof(*f));
  43. assert(f);
  44. if (!f)
  45. return NULL;
  46. // Init
  47. f->fd = fd;
  48. f->buf_size = FAUX_FILE_CHUNK_SIZE;
  49. f->buf = faux_zmalloc(f->buf_size);
  50. assert(f->buf);
  51. if (!f->buf) {
  52. faux_free(f);
  53. return NULL;
  54. }
  55. f->len = 0;
  56. f->eof = BOOL_FALSE;
  57. return f;
  58. }
  59. faux_file_t *faux_file_open(const char *pathname, int flags, mode_t mode) {
  60. int fd = -1;
  61. assert(pathname);
  62. if (!pathname)
  63. return NULL;
  64. fd = open(pathname, flags, mode);
  65. if (fd < 0)
  66. return NULL;
  67. return faux_file_fdopen(fd);
  68. }
  69. int faux_file_close(faux_file_t *f) {
  70. int fd = -1;
  71. assert(f);
  72. if (!f)
  73. return -1;
  74. fd = f->fd;
  75. faux_free(f->buf);
  76. faux_free(f);
  77. return close(fd);
  78. }
  79. int faux_file_fileno(faux_file_t *f) {
  80. assert(f);
  81. if (!f)
  82. return -1;
  83. return f->fd;
  84. }
  85. bool_t faux_file_eof(const faux_file_t *f) {
  86. assert(f);
  87. if (!f)
  88. return BOOL_FALSE;
  89. return f->eof;
  90. }
  91. static char *faux_file_takeaway(faux_file_t *f,
  92. size_t bytes_get, size_t bytes_drop) {
  93. size_t remove_len = 0;
  94. char *line = NULL;
  95. assert(f);
  96. if (!f)
  97. return NULL;
  98. remove_len = bytes_get + bytes_drop;
  99. // Try to take away more bytes than buffer contain
  100. if ((remove_len > f->len) || (0 == remove_len))
  101. return NULL;
  102. line = faux_zmalloc(bytes_get + 1); // One extra byte for '\0'
  103. assert(line);
  104. if (!line)
  105. return NULL; // Memory problems
  106. memcpy(line, f->buf, bytes_get);
  107. // Remove block from the internal buffer
  108. f->len = f->len - remove_len;
  109. memmove(f->buf, f->buf + remove_len, f->len);
  110. return line;
  111. }
  112. static char *faux_file_takeaway_rest(faux_file_t *f) {
  113. assert(f);
  114. if (!f)
  115. return NULL;
  116. return faux_file_takeaway(f, f->len, 0);
  117. }
  118. static char *faux_file_takeaway_line(faux_file_t *f) {
  119. char *find = NULL;
  120. const char *eol = "\n\r";
  121. size_t line_len = 0;
  122. assert(f);
  123. if (!f)
  124. return NULL;
  125. // Search buffer for EOL
  126. find = faux_str_charsn(f->buf, eol, f->len);
  127. if (!find)
  128. return NULL; // End of line is not found
  129. line_len = find - f->buf;
  130. // Takeaway line without trailing EOL. So drop one last byte
  131. return faux_file_takeaway(f, line_len, 1);
  132. }
  133. static int faux_file_enlarge_buffer(faux_file_t *f) {
  134. size_t new_size = 0;
  135. char *new_buf = NULL;
  136. assert(f);
  137. if (!f)
  138. return -1;
  139. new_size = f->buf_size + FAUX_FILE_CHUNK_SIZE;
  140. new_buf = realloc(f->buf, new_size);
  141. assert(new_buf);
  142. if (!new_buf)
  143. return -1;
  144. // NULLify newly allocated memory
  145. faux_bzero(new_buf + f->buf_size, new_size - f->buf_size);
  146. f->buf = new_buf;
  147. f->buf_size = new_size;
  148. return 0;
  149. }
  150. char *faux_file_getline(faux_file_t *f) {
  151. ssize_t bytes_readed = 0;
  152. assert(f);
  153. if (!f)
  154. return NULL;
  155. do {
  156. char *find = NULL;
  157. // May be buffer already contain line
  158. find = faux_file_takeaway_line(f);
  159. if (find)
  160. return find;
  161. if (f->buf_size == f->len) { // Buffer is full but doesn't contain line
  162. if (faux_file_enlarge_buffer(f) < 0) // Make buffer larger
  163. return NULL; // Memory problem
  164. }
  165. // Read new data from file
  166. do {
  167. bytes_readed = read(f->fd, f->buf + f->len, f->buf_size - f->len);
  168. if ((bytes_readed < 0) && (errno != EINTR))
  169. return NULL; // Some file error
  170. } while (bytes_readed < 0); // i.e. EINTR
  171. f->len += bytes_readed;
  172. } while (bytes_readed > 0);
  173. // EOF (here bytes_readed == 0)
  174. f->eof = BOOL_TRUE;
  175. // The last line can be without eol. Consider it as a line too
  176. return faux_file_takeaway_rest(f);
  177. }