file.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "private.h"
  25. #include "faux/faux.h"
  26. #include "faux/file.h"
  27. #include "faux/str.h"
  28. /** @brief Create file object using existent fd.
  29. *
  30. * Create file object an link it to existent file descriptor.
  31. *
  32. * @return Allocated and initialized file object or NULL on error.
  33. */
  34. faux_file_t *faux_file_fdopen(int fd) {
  35. struct stat stat_struct = {};
  36. faux_file_t *f = NULL;
  37. // Before object creation check is fd valid.
  38. // Try to get stat().
  39. if (fstat(fd, &stat_struct) < 0)
  40. return NULL; // Illegal fd
  41. f = faux_zmalloc(sizeof(*f));
  42. assert(f);
  43. if (!f)
  44. return NULL;
  45. // Init
  46. f->fd = fd;
  47. f->buf_size = FAUX_FILE_CHUNK_SIZE;
  48. f->buf = faux_zmalloc(f->buf_size);
  49. assert(f->buf);
  50. if (!f->buf) {
  51. faux_free(f);
  52. return NULL;
  53. }
  54. f->len = 0;
  55. return f;
  56. }
  57. faux_file_t *faux_file_open(const char *pathname, int flags, mode_t mode) {
  58. int fd = -1;
  59. assert(pathname);
  60. if (!pathname)
  61. return NULL;
  62. fd = open(pathname, flags, mode);
  63. if (fd < 0)
  64. return NULL;
  65. return faux_file_fdopen(fd);
  66. }
  67. int faux_file_close(faux_file_t *f) {
  68. int fd = -1;
  69. assert(f);
  70. if (!f)
  71. return -1;
  72. fd = f->fd;
  73. faux_free(f->buf);
  74. faux_free(f);
  75. return close(fd);
  76. }
  77. int faux_file_fileno(faux_file_t *f) {
  78. assert(f);
  79. if (!f)
  80. return -1;
  81. return f->fd;
  82. }
  83. static char *faux_file_takeaway_line(faux_file_t *f) {
  84. char *find = NULL;
  85. const char *eol = "\n\r";
  86. size_t line_len = 0;
  87. char *line = NULL;
  88. assert(f);
  89. if (!f)
  90. return NULL;
  91. find = faux_str_charsn(f->buf, eol, f->len);
  92. if (!find)
  93. return NULL; // End of line is not found
  94. line_len = find - f->buf;
  95. line = faux_str_dupn(f->buf, line_len);
  96. assert(line);
  97. if (!line)
  98. return NULL; // Memory problems
  99. // Remove line from the internal buffer
  100. // Remove EOL char also. So additional '1' is used
  101. f->len = f->len - line_len - 1;
  102. memmove(f->buf, find + 1, f->len);
  103. return line;
  104. }
  105. char *faux_file_getline(faux_file_t *f) {
  106. char *find = NULL;
  107. assert(f);
  108. if (!f)
  109. return NULL;
  110. // May be buffer already contain line
  111. find = faux_file_takeaway_line(f);
  112. if (find)
  113. return find;
  114. return NULL;
  115. }