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