Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / libs / libbib / map.c
1 /* Copyright (C) 1989- 2014  Free Software Foundation, Inc.
2      Written by James Clark (jjc@jclark.com)
3
4 This file is part of groff.
5
6 groff is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 groff is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <stdlib.h>
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #ifdef HAVE_MMAP
26
27 #include <sys/types.h>
28 #include <sys/mman.h>
29
30 /* The Net-2 man pages says that a MAP_FILE flag is required. */
31 #ifndef MAP_FILE
32 #define MAP_FILE 0
33 #endif
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* Prototypes */
40 char *mapread(int, int);
41 int unmap(char *, int);
42
43 char *mapread(int fd, int nbytes)
44 {
45   char *p = (char *)mmap((void *)0, (size_t)nbytes, PROT_READ,
46                          MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
47   if (p == (char *)-1)
48     return 0;
49   /* mmap() shouldn't return 0 since MAP_FIXED wasn't specified. */
50   if (p == 0)
51     abort();
52   return p;
53 }
54
55 int unmap(char *p, int len)
56 {
57   return munmap((void *)p, len);
58 }
59
60 #ifdef __cplusplus
61 }
62 #endif
63
64 #else /* not HAVE_MMAP */
65
66 #include <errno.h>
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 char *mapread(int fd, int nbytes)
73 {
74   errno = ENODEV;
75   return 0;
76 }
77
78 int unmap(char *p, int len)
79 {
80   errno = EINVAL;
81   return -1;
82 }
83
84 #ifdef __cplusplus
85 }
86 #endif
87
88 #endif /* not HAVE_MMAP */