2 * Copyright 2008 Extreme Engineering Solutions, Inc.
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)
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.
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.
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
24 #include "mingw_support.h"
37 void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
40 HANDLE handle = INVALID_HANDLE_VALUE;
41 DWORD cfm_flags = 0, mvf_flags = 0;
44 case PROT_READ | PROT_WRITE:
45 cfm_flags = PAGE_READWRITE;
46 mvf_flags = FILE_MAP_ALL_ACCESS;
49 cfm_flags = PAGE_READWRITE;
50 mvf_flags = FILE_MAP_WRITE;
53 cfm_flags = PAGE_READONLY;
54 mvf_flags = FILE_MAP_READ;
60 handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
61 cfm_flags, HIDWORD(len), LODWORD(len), NULL);
65 map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
66 LODWORD(offset), len);
75 int munmap(void *addr, size_t len)
77 if (!UnmapViewOfFile(addr))
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.
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)
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.
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. */
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";
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
111 char *strtok_r(char *s, const char *delim, char **save_ptr)
118 /* Scan leading delimiters. */
119 s += strspn(s, delim);
125 /* Find the end of the token. */
127 s = strpbrk (token, delim);
129 /* This token finishes the string. */
130 *save_ptr = memchr(token, '\0', strlen(token));
132 /* Terminate the token and make *SAVE_PTR point past it. */
139 /* getline.c -- Replacement for GNU C library function getline
141 Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc.
143 This program is free software; you can redistribute it and/or
144 modify it under the terms of the GNU General Public License as
145 published by the Free Software Foundation; either version 2 of the
146 License, or (at your option) any later version.
148 This program is distributed in the hope that it will be useful, but
149 WITHOUT ANY WARRANTY; without even the implied warranty of
150 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
151 General Public License for more details.
153 You should have received a copy of the GNU General Public License
154 along with this program; if not, write to the Free Software
155 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
157 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
159 /* Always add at least this many bytes when extending the buffer. */
162 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
163 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
164 malloc (or NULL), pointing to *N characters of space. It is realloc'd
165 as necessary. Return the number of characters read (not including the
166 null terminator), or -1 on error or EOF.
167 NOTE: There is another getstr() function declared in <curses.h>. */
168 static int getstr(char **lineptr, size_t *n, FILE *stream,
169 char terminator, size_t offset)
171 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
172 char *read_pos; /* Where we're reading into *LINEPTR. */
175 if (!lineptr || !n || !stream)
180 *lineptr = malloc(*n);
185 nchars_avail = *n - offset;
186 read_pos = *lineptr + offset;
189 register int c = getc(stream);
191 /* We always want at least one char left in the buffer, since we
192 always (unless we get an error while reading the first char)
193 NUL-terminate the line buffer. */
195 assert(*n - nchars_avail == read_pos - *lineptr);
196 if (nchars_avail < 2) {
202 nchars_avail = *n + *lineptr - read_pos;
203 *lineptr = realloc(*lineptr, *n);
206 read_pos = *n - nchars_avail + *lineptr;
207 assert(*n - nchars_avail == read_pos - *lineptr);
210 if (c == EOF || ferror (stream)) {
211 /* Return partial line, if any. */
212 if (read_pos == *lineptr)
222 /* Return the line. */
226 /* Done - NUL terminate and return the number of chars read. */
229 ret = read_pos - (*lineptr + offset);
233 int getline (char **lineptr, size_t *n, FILE *stream)
235 return getstr(lineptr, n, stream, '\n', 0);