shims.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. This source file is part of the Swift System open source project
  3. Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors
  4. Licensed under Apache License v2.0 with Runtime Library Exception
  5. See https://swift.org/LICENSE.txt for license information
  6. */
  7. #if defined(__FreeBSD__)
  8. #define __BSD_VISIBLE
  9. #endif
  10. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  11. #include <unistd.h>
  12. #endif
  13. #if defined(__FreeBSD__)
  14. #include <CSystemFreeBSD.h>
  15. #endif
  16. #ifdef __linux__
  17. #define _GNU_SOURCE
  18. #include <CSystemLinux.h>
  19. #endif
  20. #if defined(_WIN32)
  21. #include <CSystemWindows.h>
  22. #endif
  23. #include <errno.h>
  24. #if !defined(_WIN32) && !defined(__wasi__) && !defined(__APPLE__)
  25. #define HAVE_PIPE2_DUP3
  26. #endif
  27. #if defined(__APPLE__)
  28. #include <Availability.h>
  29. #if defined(__MAC_27_0)
  30. #include <unistd.h>
  31. #define HAVE_PIPE2_DUP3
  32. #endif
  33. #endif // defined(__APPLE__)
  34. // Wrappers are required because _GNU_SOURCE causes a conflict with other imports when defined in CSystemLinux.h
  35. #if !defined(_WIN32)
  36. extern int csystem_posix_pipe2(int fildes[2], int flag) {
  37. #if defined(__APPLE__) && defined(HAVE_PIPE2_DUP3)
  38. if (__builtin_available(macOS 27, iOS 27, tvOS 27, watchOS 27, visionOS 27, *))
  39. return pipe2(fildes, flag);
  40. __builtin_trap();
  41. #elif defined(HAVE_PIPE2_DUP3)
  42. return pipe2(fildes, flag);
  43. #else
  44. errno = ENOSYS;
  45. return -1;
  46. #endif
  47. }
  48. #endif // !defined(_WIN32)
  49. extern int csystem_posix_dup3(int fildes, int fildes2, int flag) {
  50. #if defined(__APPLE__) && defined(HAVE_PIPE2_DUP3)
  51. if (__builtin_available(macOS 27, iOS 27, tvOS 27, watchOS 27, visionOS 27, *))
  52. return dup3(fildes, fildes2, flag);
  53. __builtin_trap();
  54. #elif defined(HAVE_PIPE2_DUP3)
  55. return dup3(fildes, fildes2, flag);
  56. #else
  57. errno = ENOSYS;
  58. return -1;
  59. #endif
  60. }