blob: 18b69bb277f3bab2089ce712a51f1745b570f2b1 [file] [log] [blame]
Peter Tyser2f8d3962009-03-13 18:54:51 -05001/*
2 * Copyright 2008 Extreme Engineering Solutions, Inc.
3 *
4 * mmap/munmap implementation derived from:
5 * Clamav Native Windows Port : mmap win32 compatibility layer
6 * Copyright (c) 2005-2006 Gianluigi Tiesi <sherpya@netfarm.it>
7 * Parts by Kees Zeelenberg <kzlg@users.sourceforge.net> (LibGW32C)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this software; if not, write to the
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include "mingw_support.h"
25#include <stdio.h>
26#include <stdint.h>
Remy Bohmerfaf36c12009-10-28 22:13:36 +010027#include <string.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050028#include <errno.h>
Remy Bohmerfaf36c12009-10-28 22:13:36 +010029#include <assert.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050030#include <io.h>
31
32int fsync(int fd)
33{
34 return _commit(fd);
35}
36
37void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
38{
39 void *map = NULL;
40 HANDLE handle = INVALID_HANDLE_VALUE;
41 DWORD cfm_flags = 0, mvf_flags = 0;
42
43 switch (prot) {
44 case PROT_READ | PROT_WRITE:
45 cfm_flags = PAGE_READWRITE;
46 mvf_flags = FILE_MAP_ALL_ACCESS;
47 break;
48 case PROT_WRITE:
49 cfm_flags = PAGE_READWRITE;
50 mvf_flags = FILE_MAP_WRITE;
51 break;
52 case PROT_READ:
53 cfm_flags = PAGE_READONLY;
54 mvf_flags = FILE_MAP_READ;
55 break;
56 default:
57 return MAP_FAILED;
58 }
59
60 handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
61 cfm_flags, HIDWORD(len), LODWORD(len), NULL);
62 if (!handle)
63 return MAP_FAILED;
64
65 map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
66 LODWORD(offset), len);
67 CloseHandle(handle);
68
69 if (!map)
70 return MAP_FAILED;
71
72 return map;
73}
74
75int munmap(void *addr, size_t len)
76{
77 if (!UnmapViewOfFile(addr))
78 return -1;
79
80 return 0;
81}
Remy Bohmerfaf36c12009-10-28 22:13:36 +010082
83/* Reentrant string tokenizer. Generic version.
84 Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc.
85 This file is part of the GNU C Library.
86
87 This program is free software; you can redistribute it and/or modify
88 it under the terms of the GNU General Public License as published by
89 the Free Software Foundation; either version 2, or (at your option)
90 any later version.
91
92 This program is distributed in the hope that it will be useful,
93 but WITHOUT ANY WARRANTY; without even the implied warranty of
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 GNU General Public License for more details.
96
97 You should have received a copy of the GNU General Public License along
98 with this program; if not, write to the Free Software Foundation,
99 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
100
101/* Parse S into tokens separated by characters in DELIM.
102 If S is NULL, the saved pointer in SAVE_PTR is used as
103 the next starting point. For example:
104 char s[] = "-abc-=-def";
105 char *sp;
106 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
107 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
108 x = strtok_r(NULL, "=", &sp); // x = NULL
109 // s = "abc\0-def\0"
110*/
111char *strtok_r(char *s, const char *delim, char **save_ptr)
112{
113 char *token;
114
115 if (s == NULL)
116 s = *save_ptr;
117
118 /* Scan leading delimiters. */
119 s += strspn(s, delim);
120 if (*s == '\0') {
121 *save_ptr = s;
122 return NULL;
123 }
124
125 /* Find the end of the token. */
126 token = s;
127 s = strpbrk (token, delim);
128 if (s == NULL) {
129 /* This token finishes the string. */
130 *save_ptr = memchr(token, '\0', strlen(token));
131 } else {
132 /* Terminate the token and make *SAVE_PTR point past it. */
133 *s = '\0';
134 *save_ptr = s + 1;
135 }
136 return token;
137}
138
Mike Frysinger64b15022010-01-08 02:48:03 -0500139#include "getline.c"