forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftPrivateLibcExtras.swift
137 lines (121 loc) · 3.52 KB
/
SwiftPrivateLibcExtras.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===--- SwiftPrivateLibcExtras.swift -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftPrivate
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
import Glibc
#endif
#if !os(Windows) || CYGWIN
public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
#if os(Android)
preconditionFailure("mkstemps doesn't work on Android")
#else
var utf8 = template.nulTerminatedUTF8
let (fd, fileName) = utf8.withUnsafeMutableBufferPointer {
(utf8) -> (CInt, String) in
let fd = mkstemps(UnsafeMutablePointer(utf8.baseAddress!), suffixlen)
let fileName = String(cString: UnsafePointer(utf8.baseAddress!))
return (fd, fileName)
}
template = fileName
return fd
#endif
}
#endif
public var _stdlib_FD_SETSIZE: CInt {
return 1024
}
public struct _stdlib_fd_set {
var _data: [UInt]
static var _wordBits: Int {
return sizeof(UInt.self) * 8
}
public init() {
_data = [UInt](
repeating: 0,
count: Int(_stdlib_FD_SETSIZE) / _stdlib_fd_set._wordBits)
}
public func isset(_ fd: CInt) -> Bool {
let fdInt = Int(fd)
return (
_data[fdInt / _stdlib_fd_set._wordBits] &
UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
) != 0
}
public mutating func set(_ fd: CInt) {
let fdInt = Int(fd)
_data[fdInt / _stdlib_fd_set._wordBits] |=
UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
}
public mutating func clear(_ fd: CInt) {
let fdInt = Int(fd)
_data[fdInt / _stdlib_fd_set._wordBits] &=
~UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
}
public mutating func zero() {
let count = _data.count
return _data.withUnsafeMutableBufferPointer {
(_data) in
for i in 0..<count {
_data[i] = 0
}
return
}
}
}
#if !os(Windows) || CYGWIN
public func _stdlib_select(
_ readfds: inout _stdlib_fd_set, _ writefds: inout _stdlib_fd_set,
_ errorfds: inout _stdlib_fd_set, _ timeout: UnsafeMutablePointer<timeval>?
) -> CInt {
return readfds._data.withUnsafeMutableBufferPointer {
(readfds) in
writefds._data.withUnsafeMutableBufferPointer {
(writefds) in
errorfds._data.withUnsafeMutableBufferPointer {
(errorfds) in
let readAddr = readfds.baseAddress
let writeAddr = writefds.baseAddress
let errorAddr = errorfds.baseAddress
return select(
_stdlib_FD_SETSIZE,
UnsafeMutablePointer(readAddr),
UnsafeMutablePointer(writeAddr),
UnsafeMutablePointer(errorAddr),
timeout)
}
}
}
}
#endif
//
// Functions missing in `Darwin` module.
//
public func _WSTATUS(_ status: CInt) -> CInt {
return status & 0x7f
}
public var _WSTOPPED: CInt {
return 0x7f
}
public func WIFEXITED(_ status: CInt) -> Bool {
return _WSTATUS(status) == 0
}
public func WIFSIGNALED(_ status: CInt) -> Bool {
return _WSTATUS(status) != _WSTOPPED && _WSTATUS(status) != 0
}
public func WEXITSTATUS(_ status: CInt) -> CInt {
return (status >> 8) & 0xff
}
public func WTERMSIG(_ status: CInt) -> CInt {
return _WSTATUS(status)
}