Clean up compatibility definitions.
[git/git.git] / compat / mmap.c
CommitLineData
730d48a2
JS
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
4050c0df 5#include "../git-compat-util.h"
730d48a2 6
730d48a2
JS
7void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
8{
9 int n = 0;
10
f48000fc 11 if (start != NULL || !(flags & MAP_PRIVATE))
730d48a2
JS
12 die("Invalid usage of gitfakemmap.");
13
f48000fc 14 if (lseek(fd, offset, SEEK_SET) < 0) {
730d48a2
JS
15 errno = EINVAL;
16 return MAP_FAILED;
17 }
18
19 start = xmalloc(length);
f48000fc 20 if (start == NULL) {
730d48a2
JS
21 errno = ENOMEM;
22 return MAP_FAILED;
23 }
24
f48000fc 25 while (n < length) {
730d48a2
JS
26 int count = read(fd, start+n, length-n);
27
f48000fc 28 if (count == 0) {
730d48a2
JS
29 memset(start+n, 0, length-n);
30 break;
31 }
32
f48000fc 33 if (count < 0) {
730d48a2
JS
34 free(start);
35 errno = EACCES;
36 return MAP_FAILED;
37 }
38
39 n += count;
40 }
41
730d48a2
JS
42 return start;
43}
44
45int gitfakemunmap(void *start, size_t length)
46{
730d48a2 47 free(start);
730d48a2
JS
48 return 0;
49}
50