readfilemap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000-2017 Expat development team
  10. Licensed under the MIT license:
  11. Permission is hereby granted, free of charge, to any person obtaining
  12. a copy of this software and associated documentation files (the
  13. "Software"), to deal in the Software without restriction, including
  14. without limitation the rights to use, copy, modify, merge, publish,
  15. distribute, sublicense, and/or sell copies of the Software, and to permit
  16. persons to whom the Software is furnished to do so, subject to the
  17. following conditions:
  18. The above copyright notice and this permission notice shall be included
  19. in all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  23. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  26. USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. /* Functions close(2) and read(2) */
  34. #if !defined(_WIN32) && !defined(_WIN64)
  35. # include <unistd.h>
  36. #endif
  37. #ifndef S_ISREG
  38. #ifndef S_IFREG
  39. #define S_IFREG _S_IFREG
  40. #endif
  41. #ifndef S_IFMT
  42. #define S_IFMT _S_IFMT
  43. #endif
  44. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  45. #endif /* not S_ISREG */
  46. #ifndef O_BINARY
  47. #ifdef _O_BINARY
  48. #define O_BINARY _O_BINARY
  49. #else
  50. #define O_BINARY 0
  51. #endif
  52. #endif
  53. #include "filemap.h"
  54. int
  55. filemap(const char *name,
  56. void (*processor)(const void *, size_t, const char *, void *arg),
  57. void *arg)
  58. {
  59. size_t nbytes;
  60. int fd;
  61. ssize_t n;
  62. struct stat sb;
  63. void *p;
  64. fd = open(name, O_RDONLY|O_BINARY);
  65. if (fd < 0) {
  66. perror(name);
  67. return 0;
  68. }
  69. if (fstat(fd, &sb) < 0) {
  70. perror(name);
  71. close(fd);
  72. return 0;
  73. }
  74. if (!S_ISREG(sb.st_mode)) {
  75. fprintf(stderr, "%s: not a regular file\n", name);
  76. close(fd);
  77. return 0;
  78. }
  79. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  80. close(fd);
  81. return 2; /* Cannot be passed to XML_Parse in one go */
  82. }
  83. nbytes = sb.st_size;
  84. /* malloc will return NULL with nbytes == 0, handle files with size 0 */
  85. if (nbytes == 0) {
  86. static const char c = '\0';
  87. processor(&c, 0, name, arg);
  88. close(fd);
  89. return 1;
  90. }
  91. p = malloc(nbytes);
  92. if (!p) {
  93. fprintf(stderr, "%s: out of memory\n", name);
  94. close(fd);
  95. return 0;
  96. }
  97. n = read(fd, p, nbytes);
  98. if (n < 0) {
  99. perror(name);
  100. free(p);
  101. close(fd);
  102. return 0;
  103. }
  104. if (n != (ssize_t)nbytes) {
  105. fprintf(stderr, "%s: read unexpected number of bytes\n", name);
  106. free(p);
  107. close(fd);
  108. return 0;
  109. }
  110. processor(p, nbytes, name, arg);
  111. free(p);
  112. close(fd);
  113. return 1;
  114. }