blob: 038b8cdb27f8aa6c5ca49f9aafff79772ce582cd [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
Wolfgang Denk1a459662013-07-08 09:37:19 +020087 * SPDX-License-Identifier: GPL-2.0+
88 */
Remy Bohmerfaf36c12009-10-28 22:13:36 +010089
90/* Parse S into tokens separated by characters in DELIM.
91 If S is NULL, the saved pointer in SAVE_PTR is used as
92 the next starting point. For example:
93 char s[] = "-abc-=-def";
94 char *sp;
95 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
96 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
97 x = strtok_r(NULL, "=", &sp); // x = NULL
98 // s = "abc\0-def\0"
99*/
100char *strtok_r(char *s, const char *delim, char **save_ptr)
101{
102 char *token;
103
104 if (s == NULL)
105 s = *save_ptr;
106
107 /* Scan leading delimiters. */
108 s += strspn(s, delim);
109 if (*s == '\0') {
110 *save_ptr = s;
111 return NULL;
112 }
113
114 /* Find the end of the token. */
115 token = s;
116 s = strpbrk (token, delim);
117 if (s == NULL) {
118 /* This token finishes the string. */
119 *save_ptr = memchr(token, '\0', strlen(token));
120 } else {
121 /* Terminate the token and make *SAVE_PTR point past it. */
122 *s = '\0';
123 *save_ptr = s + 1;
124 }
125 return token;
126}
127
Mike Frysinger64b15022010-01-08 02:48:03 -0500128#include "getline.c"