-
Notifications
You must be signed in to change notification settings - Fork 2
/
sys.swift
318 lines (247 loc) · 7.63 KB
/
sys.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import CSua
import Glibc
public enum FileOperation: Int {
case R // Read
case W // Write
case A // Append
}
public enum PopenOperation: String {
case R = "r" // Read
case W = "w" // Write
case RE = "re" // ReadWithCloexec
case WE = "we" // WriteWithCloexec
}
public class Signal {
// The callback closure must be created directly from a function on the
// outer scope so that it does not capture context. As required by Swift for
// callbacks to C functions.
public static var trap = Glibc.signal
public static let INT = SIGINT
public static let TERM = SIGTERM
public static let ABRT = SIGABRT
public static let KILL = SIGKILL
public static let HUP = SIGHUP
public static let ALRM = SIGALRM
public static let CHLD = SIGCHLD
}
// This alias allows other files like the FileBrowser to declare this type
// without having to import Glibc as well.
public typealias CDirentPointer = UnsafeMutablePointer<dirent>
public typealias CStat = stat
public typealias CTimespec = timespec
public typealias CTime = tm
public typealias CFilePointer = UnsafeMutablePointer<FILE>
func statStruct() -> CStat {
return stat()
}
public struct PosixSys {
public static let DEFAULT_DIR_MODE = S_IRWXU | S_IRWXG | S_IRWXO
public static let DEFAULT_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH
public static let USER_RW_FILE_MODE: UInt32 = S_IRUSR | S_IWUSR
public static let SEEK_SET: Int32 = 0
public static let SEEK_CUR: Int32 = 1
public static let SEEK_END: Int32 = 2
public static let STDIN_FD: Int32 = 0
public static let STDOUT_FD: Int32 = 1
public static let STDERR_FD: Int32 = 2
public func open(path: String, flags: Int32, mode: UInt32) -> Int32 {
return retry { Glibc.open(path, flags, mode) }
}
public func openFile(filePath: String, operation: FileOperation = .R,
mode: UInt32 = DEFAULT_FILE_MODE) -> Int32 {
var flags: Int32 = 0
switch operation {
case .R: flags = O_RDONLY
case .W: flags = O_RDWR | O_CREAT | O_TRUNC
case .A: flags = O_RDWR | O_CREAT | O_APPEND
}
flags |= O_CLOEXEC
return open(path: filePath, flags: flags, mode: mode)
}
public func doOpenDir(dirPath: String) -> Int32 {
return open(path: dirPath, flags: O_RDONLY | O_DIRECTORY, mode: 0)
}
public func mkdir(dirPath: String, mode: UInt32 = DEFAULT_DIR_MODE) -> Int32 {
return retry { Glibc.mkdir(dirPath, mode) }
}
public func read(fd: Int32, address: UnsafeMutablePointer<Void>,
length: Int) -> Int {
return retry { Glibc.read(fd, address, length) }
}
public func write(fd: Int32, address: UnsafePointer<Void>,
length: Int) -> Int {
return retry { Glibc.write(fd, address, length) }
}
public func writeString(fd: Int32, string: String) -> Int {
var a = Array(string.utf8)
return write(fd: fd, address: &a, length: a.count)
}
public func writeBytes(fd: Int32, bytes: [UInt8], maxBytes: Int)
-> Int {
var a = bytes
return write(fd: fd, address: &a, length: maxBytes)
}
public func close(fd: Int32) -> Int32 {
return retry { Glibc.close(fd) }
}
public func fflush(stream: CFilePointer? = nil) -> Int32 {
return Glibc.fflush(stream)
}
public func lseek(fd: Int32, offset: Int, whence: Int32) -> Int {
return retry { Glibc.lseek(fd, offset, whence) }
}
public var pid: Int32 {
return Glibc.getpid()
}
public func rename(oldPath: String, newPath: String) -> Int32 {
return Glibc.rename(oldPath, newPath)
}
public func unlink(path: String) -> Int32 {
return Glibc.unlink(path)
}
public var cwd: String? {
var a = [CChar](repeating: 0, count:256)
let i = Glibc.getcwd(&a, 255)
if i != nil {
return String.fromCharCodes(charCodes: a)
}
return nil
}
public func stat(path: String, buffer: UnsafeMutablePointer<CStat>)
-> Int32 {
return Glibc.stat(path, buffer)
}
public func lstat(path: String, buffer: UnsafeMutablePointer<CStat>)
-> Int32 {
return Glibc.lstat(path, buffer)
}
public func statBuffer() -> CStat {
return statStruct()
}
public func readdir(dirp: OpaquePointer?) -> CDirentPointer? {
if let dp = dirp {
return Glibc.readdir(dp)
}
return nil
}
public func opendir(path: String) -> OpaquePointer? {
return Glibc.opendir(path)
}
public func opendir(pathBytes: [UInt8]) -> OpaquePointer? {
return Glibc.opendir(UnsafePointer<CChar>(pathBytes))
}
public func closedir(dirp: OpaquePointer?) -> Int32 {
if let dp = dirp {
return retry { Glibc.closedir(dp) }
}
return 0 // Ignore the nil pointer and consider it a success.
}
public func fgets(buffer: UnsafeMutablePointer<CChar>, length: Int32,
fp: CFilePointer) -> UnsafeMutablePointer<CChar>? {
return Glibc.fgets(buffer, length, fp)
}
public func fread(buffer: UnsafeMutablePointer<Void>, size: Int,
nmemb: Int, fp: CFilePointer) -> Int {
return Glibc.fread(buffer, size, nmemb, fp)
}
public func fclose(fp: CFilePointer) -> Int32 {
return Glibc.fclose(fp)
}
public func popen(command: String, operation: PopenOperation = .R)
-> CFilePointer? {
return Glibc.popen(command, operation.rawValue)
}
public func pclose(fp: CFilePointer) -> Int32 {
return Glibc.pclose(fp)
}
public func isatty(fd: Int32) -> Bool {
return Glibc.isatty(fd) == 1
}
public func strlen(sp: UnsafePointer<CChar>) -> UInt {
return Glibc.strlen(sp)
}
public func sleep(seconds: UInt32) -> UInt32 {
return Glibc.sleep(seconds)
}
public func nanosleep(seconds: Int, nanoseconds: Int = 0) -> Int32 {
var ts: CTimespec = Glibc.timespec(tv_sec: seconds, tv_nsec: nanoseconds)
return retry { Glibc.nanosleep(&ts, nil) }
}
public func time() -> Int {
return Glibc.time(nil)
}
public func timeBuffer() -> CTime {
return Glibc.tm()
}
public func localtime_r(secondsSinceEpoch: Int,
buffer: UnsafeMutablePointer<CTime>) {
var n = secondsSinceEpoch
let _ = Glibc.localtime_r(&n, buffer)
}
public func gmtime_r(secondsSinceEpoch: Int,
buffer: UnsafeMutablePointer<CTime>) {
var n = secondsSinceEpoch
let _ = Glibc.gmtime_r(&n, buffer)
}
public func abs(n: Int32) -> Int32 {
return Glibc.abs(n)
}
public func abs(n: Int) -> Int {
return Glibc.labs(n)
}
public func round(f: Double) -> Int {
return Int(Glibc.round(f))
}
public func getenv(name: String) -> String? {
if let vp = Glibc.getenv(name) {
return String(cString: vp)
}
return nil
}
public func setenv(name: String, value: String) -> Int32 {
return Glibc.setenv(name, value, 1)
}
public func unsetenv(name: String) -> Int32 {
return Glibc.unsetenv(name)
}
// The environ variable is made available by the CSua sister project
// dependency.
public var environment: [String: String] {
var env = [String: String]()
var i = 0
while true {
let nm = (environ + i).pointee
if nm == nil {
break
}
if let np = UnsafePointer<CChar>(nm) {
let s = String(cString: np)
let (name, value) = s.splitOnce(string: "=")
env[name!] = value ?? ""
}
i += 1
}
return env
}
public func getpwnam(name: String) -> UnsafeMutablePointer<passwd>? {
return Glibc.getpwnam(name)
}
public func retry(fn: () -> Int32) -> Int32 {
var value = fn()
while value == -1 {
if errno != EINTR { break }
value = fn()
}
return value
}
public func retry(fn: () -> Int) -> Int {
var value = fn()
while value == -1 {
if errno != EINTR { break }
value = fn()
}
return value
}
}
public let Sys = PosixSys()