1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2008 Extreme Engineering Solutions, Inc.
5 * mmap/munmap implementation derived from:
6 * Clamav Native Windows Port : mmap win32 compatibility layer
7 * Copyright (c) 2005-2006 Gianluigi Tiesi <sherpya@netfarm.it>
8 * Parts by Kees Zeelenberg <kzlg@users.sourceforge.net> (LibGW32C)
11 #include "mingw_support.h"
24 void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
27 HANDLE handle = INVALID_HANDLE_VALUE;
28 DWORD cfm_flags = 0, mvf_flags = 0;
31 case PROT_READ | PROT_WRITE:
32 cfm_flags = PAGE_READWRITE;
33 mvf_flags = FILE_MAP_ALL_ACCESS;
36 cfm_flags = PAGE_READWRITE;
37 mvf_flags = FILE_MAP_WRITE;
40 cfm_flags = PAGE_READONLY;
41 mvf_flags = FILE_MAP_READ;
47 handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
48 cfm_flags, HIDWORD(len), LODWORD(len), NULL);
52 map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
53 LODWORD(offset), len);
62 int munmap(void *addr, size_t len)
64 if (!UnmapViewOfFile(addr))
70 /* Reentrant string tokenizer. Generic version.
71 Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc.
72 This file is part of the GNU C Library.
75 /* Parse S into tokens separated by characters in DELIM.
76 If S is NULL, the saved pointer in SAVE_PTR is used as
77 the next starting point. For example:
78 char s[] = "-abc-=-def";
80 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
81 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
82 x = strtok_r(NULL, "=", &sp); // x = NULL
85 char *strtok_r(char *s, const char *delim, char **save_ptr)
92 /* Scan leading delimiters. */
93 s += strspn(s, delim);
99 /* Find the end of the token. */
101 s = strpbrk (token, delim);
103 /* This token finishes the string. */
104 *save_ptr = memchr(token, '\0', strlen(token));
106 /* Terminate the token and make *SAVE_PTR point past it. */