blob: 2b17bf7dd28b6f78d4a1d81aea69fc3f800a8e05 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peter Tyser2f8d3962009-03-13 18:54:51 -05002/*
3 * Copyright 2008 Extreme Engineering Solutions, Inc.
4 *
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)
Peter Tyser2f8d3962009-03-13 18:54:51 -05009 */
10
11#include "mingw_support.h"
12#include <stdio.h>
13#include <stdint.h>
Remy Bohmerfaf36c12009-10-28 22:13:36 +010014#include <string.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050015#include <errno.h>
Remy Bohmerfaf36c12009-10-28 22:13:36 +010016#include <assert.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050017#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}
Remy Bohmerfaf36c12009-10-28 22:13:36 +010069
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.
Wolfgang Denk1a459662013-07-08 09:37:19 +020073 */
Remy Bohmerfaf36c12009-10-28 22:13:36 +010074
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";
79 char *sp;
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
83 // s = "abc\0-def\0"
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 /* Scan leading delimiters. */
93 s += strspn(s, delim);
94 if (*s == '\0') {
95 *save_ptr = s;
96 return NULL;
97 }
98
99 /* Find the end of the token. */
100 token = s;
101 s = strpbrk (token, delim);
102 if (s == NULL) {
103 /* This token finishes the string. */
104 *save_ptr = memchr(token, '\0', strlen(token));
105 } else {
106 /* Terminate the token and make *SAVE_PTR point past it. */
107 *s = '\0';
108 *save_ptr = s + 1;
109 }
110 return token;
111}
112
Mike Frysinger64b15022010-01-08 02:48:03 -0500113#include "getline.c"