readfilemap.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. */
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. /* Functions close(2) and read(2) */
  10. #if !defined(_WIN32) && !defined(_WIN64)
  11. # include <unistd.h>
  12. #endif
  13. #ifndef S_ISREG
  14. #ifndef S_IFREG
  15. #define S_IFREG _S_IFREG
  16. #endif
  17. #ifndef S_IFMT
  18. #define S_IFMT _S_IFMT
  19. #endif
  20. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  21. #endif /* not S_ISREG */
  22. #ifndef O_BINARY
  23. #ifdef _O_BINARY
  24. #define O_BINARY _O_BINARY
  25. #else
  26. #define O_BINARY 0
  27. #endif
  28. #endif
  29. #include "filemap.h"
  30. int
  31. filemap(const char *name,
  32. void (*processor)(const void *, size_t, const char *, void *arg),
  33. void *arg)
  34. {
  35. size_t nbytes;
  36. int fd;
  37. int n;
  38. struct stat sb;
  39. void *p;
  40. fd = open(name, O_RDONLY|O_BINARY);
  41. if (fd < 0) {
  42. perror(name);
  43. return 0;
  44. }
  45. if (fstat(fd, &sb) < 0) {
  46. perror(name);
  47. close(fd);
  48. return 0;
  49. }
  50. if (!S_ISREG(sb.st_mode)) {
  51. fprintf(stderr, "%s: not a regular file\n", name);
  52. close(fd);
  53. return 0;
  54. }
  55. if (sb.st_size > XML_MAX_CHUNK_LEN) {
  56. close(fd);
  57. return 2; /* Cannot be passed to XML_Parse in one go */
  58. }
  59. nbytes = sb.st_size;
  60. /* malloc will return NULL with nbytes == 0, handle files with size 0 */
  61. if (nbytes == 0) {
  62. static const char c = '\0';
  63. processor(&c, 0, name, arg);
  64. close(fd);
  65. return 1;
  66. }
  67. p = malloc(nbytes);
  68. if (!p) {
  69. fprintf(stderr, "%s: out of memory\n", name);
  70. close(fd);
  71. return 0;
  72. }
  73. n = read(fd, p, nbytes);
  74. if (n < 0) {
  75. perror(name);
  76. free(p);
  77. close(fd);
  78. return 0;
  79. }
  80. if (n != nbytes) {
  81. fprintf(stderr, "%s: read unexpected number of bytes\n", name);
  82. free(p);
  83. close(fd);
  84. return 0;
  85. }
  86. processor(p, nbytes, name, arg);
  87. free(p);
  88. close(fd);
  89. return 1;
  90. }