CInterop.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 SYSTEM_PACKAGE_DARWIN
  8. import Darwin
  9. #elseif os(Windows)
  10. import CSystem
  11. import ucrt
  12. #elseif canImport(Glibc)
  13. import CSystem
  14. import Glibc
  15. #elseif canImport(Musl)
  16. import CSystem
  17. import Musl
  18. #elseif canImport(WASILibc)
  19. import WASILibc
  20. #elseif canImport(Bionic)
  21. import CSystem
  22. import Bionic
  23. #else
  24. #error("Unsupported Platform")
  25. #endif
  26. // MARK: - Public typealiases
  27. // FIXME: `CModeT` ought to be deprecated and replaced with `CInterop.Mode`
  28. // if/when the compiler becomes less strict about availability checking
  29. // of "namespaced" typealiases. (rdar://81722893)
  30. #if os(Windows)
  31. /// The C `mode_t` type.
  32. public typealias CModeT = CInt
  33. #else
  34. /// The C `mode_t` type.
  35. @available(System 0.0.1, *)
  36. public typealias CModeT = mode_t
  37. #endif
  38. /// A namespace for C and platform types
  39. @available(System 0.0.2, *)
  40. public enum CInterop {
  41. #if os(Windows)
  42. public typealias Mode = CInt
  43. #else
  44. public typealias Mode = mode_t
  45. #endif
  46. /// The C `char` type
  47. public typealias Char = CChar
  48. #if os(Windows)
  49. /// The platform's preferred character type. On Unix, this is an 8-bit C
  50. /// `char` (which may be signed or unsigned, depending on platform). On
  51. /// Windows, this is `UInt16` (a "wide" character).
  52. public typealias PlatformChar = UInt16
  53. #else
  54. /// The platform's preferred character type. On Unix, this is an 8-bit C
  55. /// `char` (which may be signed or unsigned, depending on platform). On
  56. /// Windows, this is `UInt16` (a "wide" character).
  57. public typealias PlatformChar = CInterop.Char
  58. #endif
  59. #if os(Windows)
  60. /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
  61. /// Windows it is UTF-16. Native strings may contain invalid Unicode,
  62. /// which will be handled by either error-correction or failing, depending
  63. /// on API.
  64. public typealias PlatformUnicodeEncoding = UTF16
  65. #else
  66. /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
  67. /// Windows it is UTF-16. Native strings may contain invalid Unicode,
  68. /// which will be handled by either error-correction or failing, depending
  69. /// on API.
  70. public typealias PlatformUnicodeEncoding = UTF8
  71. #endif
  72. }
  73. #if !os(Windows)
  74. @available(System 0.0.2, *) // Original availability of CInterop
  75. extension CInterop {
  76. /// The C `stat` struct.
  77. public typealias Stat = stat
  78. /// Calls the C `stat()` function.
  79. ///
  80. /// This is a direct wrapper around the C `stat()` system call.
  81. /// For a more ergonomic Swift API, use `Stat` instead.
  82. ///
  83. /// - Warning: This API is primarily intended for migration purposes when
  84. /// supporting older deployment targets. If your deployment target supports
  85. /// it, prefer using the `Stat` API introduced in SYS-0006, which provides
  86. /// type-safe, ergonomic access to file metadata in Swift.
  87. ///
  88. /// - Parameters:
  89. /// - path: A null-terminated C string representing the file path.
  90. /// - s: An `inout` reference to a `CInterop.Stat` struct to populate.
  91. /// - Returns: 0 on success, -1 on error (check `Errno.current`).
  92. @_alwaysEmitIntoClient
  93. public static func stat(_ path: UnsafePointer<CChar>, _ s: inout CInterop.Stat) -> Int32 {
  94. system_stat(path, &s)
  95. }
  96. }
  97. @available(System 1.7.0, *)
  98. extension CInterop {
  99. public typealias DeviceID = dev_t
  100. public typealias Inode = ino_t
  101. public typealias UserID = uid_t
  102. public typealias GroupID = gid_t
  103. #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) || os(OpenBSD)
  104. public typealias FileFlags = UInt32
  105. #endif
  106. }
  107. #endif