mingw_support.c 2.5 KB

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