mingw_support.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  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. #include "mingw_support.h"
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <io.h>
  30. int fsync(int fd)
  31. {
  32. return _commit(fd);
  33. }
  34. void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
  35. {
  36. void *map = NULL;
  37. HANDLE handle = INVALID_HANDLE_VALUE;
  38. DWORD cfm_flags = 0, mvf_flags = 0;
  39. switch (prot) {
  40. case PROT_READ | PROT_WRITE:
  41. cfm_flags = PAGE_READWRITE;
  42. mvf_flags = FILE_MAP_ALL_ACCESS;
  43. break;
  44. case PROT_WRITE:
  45. cfm_flags = PAGE_READWRITE;
  46. mvf_flags = FILE_MAP_WRITE;
  47. break;
  48. case PROT_READ:
  49. cfm_flags = PAGE_READONLY;
  50. mvf_flags = FILE_MAP_READ;
  51. break;
  52. default:
  53. return MAP_FAILED;
  54. }
  55. handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
  56. cfm_flags, HIDWORD(len), LODWORD(len), NULL);
  57. if (!handle)
  58. return MAP_FAILED;
  59. map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
  60. LODWORD(offset), len);
  61. CloseHandle(handle);
  62. if (!map)
  63. return MAP_FAILED;
  64. return map;
  65. }
  66. int munmap(void *addr, size_t len)
  67. {
  68. if (!UnmapViewOfFile(addr))
  69. return -1;
  70. return 0;
  71. }
  72. /* Reentrant string tokenizer. Generic version.
  73. Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc.
  74. This file is part of the GNU C Library.
  75. This program is free software; you can redistribute it and/or modify
  76. it under the terms of the GNU General Public License as published by
  77. the Free Software Foundation; either version 2, or (at your option)
  78. any later version.
  79. This program is distributed in the hope that it will be useful,
  80. but WITHOUT ANY WARRANTY; without even the implied warranty of
  81. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  82. GNU General Public License for more details.
  83. You should have received a copy of the GNU General Public License along
  84. with this program; if not, write to the Free Software Foundation,
  85. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  86. /* Parse S into tokens separated by characters in DELIM.
  87. If S is NULL, the saved pointer in SAVE_PTR is used as
  88. the next starting point. For example:
  89. char s[] = "-abc-=-def";
  90. char *sp;
  91. x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  92. x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  93. x = strtok_r(NULL, "=", &sp); // x = NULL
  94. // s = "abc\0-def\0"
  95. */
  96. char *strtok_r(char *s, const char *delim, char **save_ptr)
  97. {
  98. char *token;
  99. if (s == NULL)
  100. s = *save_ptr;
  101. /* Scan leading delimiters. */
  102. s += strspn(s, delim);
  103. if (*s == '\0') {
  104. *save_ptr = s;
  105. return NULL;
  106. }
  107. /* Find the end of the token. */
  108. token = s;
  109. s = strpbrk (token, delim);
  110. if (s == NULL) {
  111. /* This token finishes the string. */
  112. *save_ptr = memchr(token, '\0', strlen(token));
  113. } else {
  114. /* Terminate the token and make *SAVE_PTR point past it. */
  115. *s = '\0';
  116. *save_ptr = s + 1;
  117. }
  118. return token;
  119. }
  120. /* getline.c -- Replacement for GNU C library function getline
  121. Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc.
  122. This program is free software; you can redistribute it and/or
  123. modify it under the terms of the GNU General Public License as
  124. published by the Free Software Foundation; either version 2 of the
  125. License, or (at your option) any later version.
  126. This program is distributed in the hope that it will be useful, but
  127. WITHOUT ANY WARRANTY; without even the implied warranty of
  128. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  129. General Public License for more details.
  130. You should have received a copy of the GNU General Public License
  131. along with this program; if not, write to the Free Software
  132. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  133. /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
  134. /* Always add at least this many bytes when extending the buffer. */
  135. #define MIN_CHUNK 64
  136. /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
  137. + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
  138. malloc (or NULL), pointing to *N characters of space. It is realloc'd
  139. as necessary. Return the number of characters read (not including the
  140. null terminator), or -1 on error or EOF.
  141. NOTE: There is another getstr() function declared in <curses.h>. */
  142. static int getstr(char **lineptr, size_t *n, FILE *stream,
  143. char terminator, size_t offset)
  144. {
  145. int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
  146. char *read_pos; /* Where we're reading into *LINEPTR. */
  147. int ret;
  148. if (!lineptr || !n || !stream)
  149. return -1;
  150. if (!*lineptr) {
  151. *n = MIN_CHUNK;
  152. *lineptr = malloc(*n);
  153. if (!*lineptr)
  154. return -1;
  155. }
  156. nchars_avail = *n - offset;
  157. read_pos = *lineptr + offset;
  158. for (;;) {
  159. register int c = getc(stream);
  160. /* We always want at least one char left in the buffer, since we
  161. always (unless we get an error while reading the first char)
  162. NUL-terminate the line buffer. */
  163. assert(*n - nchars_avail == read_pos - *lineptr);
  164. if (nchars_avail < 2) {
  165. if (*n > MIN_CHUNK)
  166. *n *= 2;
  167. else
  168. *n += MIN_CHUNK;
  169. nchars_avail = *n + *lineptr - read_pos;
  170. *lineptr = realloc(*lineptr, *n);
  171. if (!*lineptr)
  172. return -1;
  173. read_pos = *n - nchars_avail + *lineptr;
  174. assert(*n - nchars_avail == read_pos - *lineptr);
  175. }
  176. if (c == EOF || ferror (stream)) {
  177. /* Return partial line, if any. */
  178. if (read_pos == *lineptr)
  179. return -1;
  180. else
  181. break;
  182. }
  183. *read_pos++ = c;
  184. nchars_avail--;
  185. if (c == terminator)
  186. /* Return the line. */
  187. break;
  188. }
  189. /* Done - NUL terminate and return the number of chars read. */
  190. *read_pos = '\0';
  191. ret = read_pos - (*lineptr + offset);
  192. return ret;
  193. }
  194. int getline (char **lineptr, size_t *n, FILE *stream)
  195. {
  196. return getstr(lineptr, n, stream, '\n', 0);
  197. }