mingw_support.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  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)
  9. */
  10. #include "mingw_support.h"
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <io.h>
  17. int fsync(int fd)
  18. {
  19. return _commit(fd);
  20. }
  21. void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
  22. {
  23. void *map = NULL;
  24. HANDLE handle = INVALID_HANDLE_VALUE;
  25. DWORD cfm_flags = 0, mvf_flags = 0;
  26. switch (prot) {
  27. case PROT_READ | PROT_WRITE:
  28. cfm_flags = PAGE_READWRITE;
  29. mvf_flags = FILE_MAP_ALL_ACCESS;
  30. break;
  31. case PROT_WRITE:
  32. cfm_flags = PAGE_READWRITE;
  33. mvf_flags = FILE_MAP_WRITE;
  34. break;
  35. case PROT_READ:
  36. cfm_flags = PAGE_READONLY;
  37. mvf_flags = FILE_MAP_READ;
  38. break;
  39. default:
  40. return MAP_FAILED;
  41. }
  42. handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
  43. cfm_flags, HIDWORD(len), LODWORD(len), NULL);
  44. if (!handle)
  45. return MAP_FAILED;
  46. map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
  47. LODWORD(offset), len);
  48. CloseHandle(handle);
  49. if (!map)
  50. return MAP_FAILED;
  51. return map;
  52. }
  53. int munmap(void *addr, size_t len)
  54. {
  55. if (!UnmapViewOfFile(addr))
  56. return -1;
  57. return 0;
  58. }
  59. /* Reentrant string tokenizer. Generic version.
  60. Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc.
  61. This file is part of the GNU C Library.
  62. */
  63. /* Parse S into tokens separated by characters in DELIM.
  64. If S is NULL, the saved pointer in SAVE_PTR is used as
  65. the next starting point. For example:
  66. char s[] = "-abc-=-def";
  67. char *sp;
  68. x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  69. x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  70. x = strtok_r(NULL, "=", &sp); // x = NULL
  71. // s = "abc\0-def\0"
  72. */
  73. char *strtok_r(char *s, const char *delim, char **save_ptr)
  74. {
  75. char *token;
  76. if (s == NULL)
  77. s = *save_ptr;
  78. /* Scan leading delimiters. */
  79. s += strspn(s, delim);
  80. if (*s == '\0') {
  81. *save_ptr = s;
  82. return NULL;
  83. }
  84. /* Find the end of the token. */
  85. token = s;
  86. s = strpbrk (token, delim);
  87. if (s == NULL) {
  88. /* This token finishes the string. */
  89. *save_ptr = memchr(token, '\0', strlen(token));
  90. } else {
  91. /* Terminate the token and make *SAVE_PTR point past it. */
  92. *s = '\0';
  93. *save_ptr = s + 1;
  94. }
  95. return token;
  96. }
  97. #include "getline.c"