unixfilemap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/mman.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #ifndef MAP_FILE
  37. # define MAP_FILE 0
  38. #endif
  39. #include "xmltchar.h"
  40. #include "filemap.h"
  41. #ifdef XML_UNICODE_WCHAR_T
  42. # define XML_FMT_STR "ls"
  43. #else
  44. # define XML_FMT_STR "s"
  45. #endif
  46. int
  47. filemap(const tchar *name,
  48. void (*processor)(const void *, size_t, const tchar *, void *arg),
  49. void *arg) {
  50. int fd;
  51. size_t nbytes;
  52. struct stat sb;
  53. void *p;
  54. fd = topen(name, O_RDONLY);
  55. if (fd < 0) {
  56. tperror(name);
  57. return 0;
  58. }
  59. if (fstat(fd, &sb) < 0) {
  60. tperror(name);
  61. close(fd);
  62. return 0;
  63. }
  64. if (! S_ISREG(sb.st_mode)) {
  65. close(fd);
  66. fprintf(stderr, "%" XML_FMT_STR ": not a regular file\n", name);
  67. return 0;
  68. }
  69. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  70. close(fd);
  71. return 2; /* Cannot be passed to XML_Parse in one go */
  72. }
  73. nbytes = sb.st_size;
  74. /* mmap fails for zero length files */
  75. if (nbytes == 0) {
  76. static const char c = '\0';
  77. processor(&c, 0, name, arg);
  78. close(fd);
  79. return 1;
  80. }
  81. p = (void *)mmap((void *)0, (size_t)nbytes, PROT_READ, MAP_FILE | MAP_PRIVATE,
  82. fd, (off_t)0);
  83. if (p == (void *)-1) {
  84. tperror(name);
  85. close(fd);
  86. return 0;
  87. }
  88. processor(p, nbytes, name, arg);
  89. munmap((void *)p, nbytes);
  90. close(fd);
  91. return 1;
  92. }