1
0

unixfilemap.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <[email protected]>
  10. Copyright (c) 2001-2002 Fred L. Drake, Jr. <[email protected]>
  11. Copyright (c) 2006 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2025 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017 Rhodri James <[email protected]>
  14. Licensed under the MIT license:
  15. Permission is hereby granted, free of charge, to any person obtaining
  16. a copy of this software and associated documentation files (the
  17. "Software"), to deal in the Software without restriction, including
  18. without limitation the rights to use, copy, modify, merge, publish,
  19. distribute, sublicense, and/or sell copies of the Software, and to permit
  20. persons to whom the Software is furnished to do so, subject to the
  21. following conditions:
  22. The above copyright notice and this permission notice shall be included
  23. in all copies or substantial portions of the Software.
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  27. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  28. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  30. USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <sys/types.h>
  33. #include <sys/mman.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <stdlib.h> // NULL
  40. #include <unistd.h>
  41. #ifndef MAP_FILE
  42. # define MAP_FILE 0
  43. #endif
  44. #include "xmltchar.h"
  45. #include "filemap.h"
  46. #ifdef XML_UNICODE_WCHAR_T
  47. # define XML_FMT_STR "ls"
  48. #else
  49. # define XML_FMT_STR "s"
  50. #endif
  51. int
  52. filemap(const tchar *name,
  53. void (*processor)(const void *, size_t, const tchar *, void *arg),
  54. void *arg) {
  55. int fd;
  56. size_t nbytes;
  57. struct stat sb;
  58. void *p;
  59. fd = topen(name, O_RDONLY);
  60. if (fd < 0) {
  61. tperror(name);
  62. return 0;
  63. }
  64. if (fstat(fd, &sb) < 0) {
  65. tperror(name);
  66. close(fd);
  67. return 0;
  68. }
  69. if (! S_ISREG(sb.st_mode)) {
  70. close(fd);
  71. fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
  72. return 0;
  73. }
  74. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  75. close(fd);
  76. return 2; /* Cannot be passed to XML_Parse in one go */
  77. }
  78. nbytes = sb.st_size;
  79. /* mmap fails for zero length files */
  80. if (nbytes == 0) {
  81. static const char c = '\0';
  82. processor(&c, 0, name, arg);
  83. close(fd);
  84. return 1;
  85. }
  86. p = mmap(NULL, nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, (off_t)0);
  87. if (p == (void *)-1) {
  88. tperror(name);
  89. close(fd);
  90. return 0;
  91. }
  92. processor(p, nbytes, name, arg);
  93. munmap((void *)p, nbytes);
  94. close(fd);
  95. return 1;
  96. }