/contrib/groff/src/libs/libbib/map.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 86 lines · 49 code · 16 blank · 21 comment · 4 complexity · f6f74d4d4371d0a15e978ad5f63cd1d2 MD5 · raw file

  1. /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2004
  2. Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <stdlib.h>
  17. #ifdef HAVE_CONFIG_H
  18. #include <config.h>
  19. #endif
  20. #ifdef HAVE_MMAP
  21. #include <sys/types.h>
  22. #include <sys/mman.h>
  23. /* The Net-2 man pages says that a MAP_FILE flag is required. */
  24. #ifndef MAP_FILE
  25. #define MAP_FILE 0
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. char *mapread(int fd, int nbytes)
  31. {
  32. char *p = (char *)mmap((void *)0, (size_t)nbytes, PROT_READ,
  33. MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
  34. if (p == (char *)-1)
  35. return 0;
  36. /* mmap() shouldn't return 0 since MAP_FIXED wasn't specified. */
  37. if (p == 0)
  38. abort();
  39. return p;
  40. }
  41. int unmap(char *p, int len)
  42. {
  43. return munmap((void *)p, len);
  44. }
  45. #ifdef __cplusplus
  46. }
  47. #endif
  48. #else /* not HAVE_MMAP */
  49. #include <errno.h>
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. char *mapread(int fd, int nbytes)
  54. {
  55. errno = ENODEV;
  56. return 0;
  57. }
  58. int unmap(char *p, int len)
  59. {
  60. errno = EINVAL;
  61. return -1;
  62. }
  63. #ifdef __cplusplus
  64. }
  65. #endif
  66. #endif /* not HAVE_MMAP */