1
2
3
4
5
6
7
8
9
10
11
12#include "mingw_support.h"
13#include <stdio.h>
14#include <stdint.h>
15#include <string.h>
16#include <errno.h>
17#include <assert.h>
18#include <io.h>
19
20int fsync(int fd)
21{
22 return _commit(fd);
23}
24
25void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
26{
27 void *map = NULL;
28 HANDLE handle = INVALID_HANDLE_VALUE;
29 DWORD cfm_flags = 0, mvf_flags = 0;
30
31 switch (prot) {
32 case PROT_READ | PROT_WRITE:
33 cfm_flags = PAGE_READWRITE;
34 mvf_flags = FILE_MAP_ALL_ACCESS;
35 break;
36 case PROT_WRITE:
37 cfm_flags = PAGE_READWRITE;
38 mvf_flags = FILE_MAP_WRITE;
39 break;
40 case PROT_READ:
41 cfm_flags = PAGE_READONLY;
42 mvf_flags = FILE_MAP_READ;
43 break;
44 default:
45 return MAP_FAILED;
46 }
47
48 handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
49 cfm_flags, HIDWORD(len), LODWORD(len), NULL);
50 if (!handle)
51 return MAP_FAILED;
52
53 map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
54 LODWORD(offset), len);
55 CloseHandle(handle);
56
57 if (!map)
58 return MAP_FAILED;
59
60 return map;
61}
62
63int munmap(void *addr, size_t len)
64{
65 if (!UnmapViewOfFile(addr))
66 return -1;
67
68 return 0;
69}
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88char *strtok_r(char *s, const char *delim, char **save_ptr)
89{
90 char *token;
91
92 if (s == NULL)
93 s = *save_ptr;
94
95
96 s += strspn(s, delim);
97 if (*s == '\0') {
98 *save_ptr = s;
99 return NULL;
100 }
101
102
103 token = s;
104 s = strpbrk (token, delim);
105 if (s == NULL) {
106
107 *save_ptr = memchr(token, '\0', strlen(token));
108 } else {
109
110 *s = '\0';
111 *save_ptr = s + 1;
112 }
113 return token;
114}
115
116#include "getline.c"
117