mingw_support.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "getline.c"