xref: /netbsd-src/external/bsd/nvi/dist/clib/mmap.c (revision dbd550ed1a6686d6600f748306f9cc03d8cd4c94)
1 #include "config.h"
2 
3 #include <sys/types.h>
4 
5 #include <stdlib.h>
6 #include <unistd.h>
7 
8 /*
9  * This function fakes mmap() by reading `len' bytes from the file descriptor
10  * `fd' and returning a pointer to that memory.  The "mapped" region can later
11  * be deallocated with munmap().
12  *
13  * Note: ONLY reading is supported and only reading of the exact size of the
14  * file will work.
15  *
16  * PUBLIC: #ifndef HAVE_MMAP
17  * PUBLIC: char *mmap __P((char *, size_t, int, int, int, off_t));
18  * PUBLIC: #endif
19  */
20 char *
mmap(char * addr,size_t len,int prot,int flags,int fd,off_t off)21 mmap(char *addr, size_t len, int prot, int flags, int fd, off_t off)
22 {
23 	char *ptr;
24 
25 	if ((ptr = (char *)malloc(len)) == 0)
26 		return ((char *)-1);
27 	if (read(fd, ptr, len) < 0) {
28 		free(ptr);
29 		return ((char *)-1);
30 	}
31 	return (ptr);
32 }
33 
34 /*
35  * PUBLIC: #ifndef HAVE_MMAP
36  * PUBLIC: int munmap __P((char *, size_t));
37  * PUBLIC: #endif
38  */
39 int
munmap(char * addr,size_t len)40 munmap(char *addr, size_t len)
41 {
42 	free(addr);
43 	return (0);
44 }
45