Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

  1. /*
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in
  4. compliance with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6. Software distributed under the License is distributed on an "AS IS"
  7. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. License for the specific language governing rights and limitations
  9. under the License.
  10. The Original Code is expat.
  11. The Initial Developer of the Original Code is James Clark.
  12. Portions created by James Clark are Copyright (C) 1998, 1999
  13. James Clark. All Rights Reserved.
  14. Contributor(s):
  15. Alternatively, the contents of this file may be used under the terms
  16. of the GNU General Public License (the "GPL"), in which case the
  17. provisions of the GPL are applicable instead of those above. If you
  18. wish to allow use of your version of this file only under the terms of
  19. the GPL and not to allow others to use your version of this file under
  20. the MPL, indicate your decision by deleting the provisions above and
  21. replace them with the notice and other provisions required by the
  22. GPL. If you do not delete the provisions above, a recipient may use
  23. your version of this file under either the MPL or the GPL.
  24. */
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #ifndef S_ISREG
  31. #ifndef S_IFREG
  32. #define S_IFREG _S_IFREG
  33. #endif
  34. #ifndef S_IFMT
  35. #define S_IFMT _S_IFMT
  36. #endif
  37. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  38. #endif /* not S_ISREG */
  39. #ifndef O_BINARY
  40. #ifdef _O_BINARY
  41. #define O_BINARY _O_BINARY
  42. #else
  43. #define O_BINARY 0
  44. #endif
  45. #endif
  46. int filemap(const char *name,
  47. void (*processor)(const void *, size_t, const char *, void *arg),
  48. void *arg)
  49. {
  50. size_t nbytes;
  51. int fd;
  52. int n;
  53. struct stat sb;
  54. void *p;
  55. fd = open(name, O_RDONLY|O_BINARY);
  56. if (fd < 0) {
  57. perror(name);
  58. return 0;
  59. }
  60. if (fstat(fd, &sb) < 0) {
  61. perror(name);
  62. return 0;
  63. }
  64. if (!S_ISREG(sb.st_mode)) {
  65. fprintf(stderr, "%s: not a regular file\n", name);
  66. return 0;
  67. }
  68. nbytes = sb.st_size;
  69. p = malloc(nbytes);
  70. if (!p) {
  71. fprintf(stderr, "%s: out of memory\n", name);
  72. return 0;
  73. }
  74. n = read(fd, p, nbytes);
  75. if (n < 0) {
  76. perror(name);
  77. close(fd);
  78. return 0;
  79. }
  80. if (n != nbytes) {
  81. fprintf(stderr, "%s: read unexpected number of bytes\n", name);
  82. close(fd);
  83. return 0;
  84. }
  85. processor(p, nbytes, name, arg);
  86. free(p);
  87. close(fd);
  88. return 1;
  89. }