Skip to content

Commit 306c586

Browse files
committed
Merge pull request #413 from billabt/fcntl-extension
Implementation of fcntl() to both Darwin and Glibc overlays. Ported …
2 parents 01b8a35 + 661e103 commit 306c586

6 files changed

Lines changed: 387 additions & 23 deletions

File tree

stdlib/public/Glibc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(sources
22
Glibc.swift
3+
Misc.c
34
)
45

56
set(output_dir "${SWIFTLIB_DIR}/glibc")

stdlib/public/Glibc/Glibc.swift

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,100 @@ public var errno: Int32 {
2929
// fcntl.h
3030
//===----------------------------------------------------------------------===//
3131

32+
@warn_unused_result
33+
@_silgen_name("_swift_Glibc_open")
34+
func _swift_Glibc_open(path: UnsafePointer<CChar>,
35+
_ oflag: CInt,
36+
_ mode: mode_t
37+
) -> CInt
38+
39+
@warn_unused_result
40+
@_silgen_name("_swift_Glibc_openat")
41+
func _swift_Glibc_openat(
42+
fd: CInt,
43+
_ path: UnsafePointer<CChar>,
44+
_ oflag: CInt,
45+
_ mode: mode_t
46+
) -> CInt
47+
48+
@warn_unused_result
49+
public func open(
50+
path: UnsafePointer<CChar>,
51+
_ oflag: CInt
52+
) -> CInt {
53+
return _swift_Glibc_open(path, oflag, 0)
54+
}
55+
56+
@warn_unused_result
57+
public func open(
58+
path: UnsafePointer<CChar>,
59+
_ oflag: CInt,
60+
_ mode: mode_t
61+
) -> CInt {
62+
return _swift_Glibc_open(path, oflag, mode)
63+
}
64+
65+
@warn_unused_result
66+
public func openat(
67+
fd: CInt,
68+
_ path: UnsafePointer<CChar>,
69+
_ oflag: CInt
70+
) -> CInt {
71+
return _swift_Glibc_openat(fd, path, oflag, 0)
72+
}
73+
74+
@warn_unused_result
75+
public func openat(
76+
fd: CInt,
77+
_ path: UnsafePointer<CChar>,
78+
_ oflag: CInt,
79+
_ mode: mode_t
80+
) -> CInt {
81+
return _swift_Glibc_openat(fd, path, oflag, mode)
82+
}
83+
84+
@warn_unused_result
85+
@_silgen_name("_swift_Glibc_fcntl")
86+
internal func _swift_Glibc_fcntl(
87+
fd: CInt,
88+
_ cmd: CInt,
89+
_ value: CInt
90+
) -> CInt
91+
92+
@warn_unused_result
93+
@_silgen_name("_swift_Glibc_fcntlPtr")
94+
internal func _swift_Glibc_fcntlPtr(
95+
fd: CInt,
96+
_ cmd: CInt,
97+
_ ptr: UnsafeMutablePointer<Void>
98+
) -> CInt
99+
100+
@warn_unused_result
101+
public func fcntl(
102+
fd: CInt,
103+
_ cmd: CInt
104+
) -> CInt {
105+
return _swift_Glibc_fcntl(fd, cmd, 0)
106+
}
107+
108+
@warn_unused_result
109+
public func fcntl(
110+
fd: CInt,
111+
_ cmd: CInt,
112+
_ value: CInt
113+
) -> CInt {
114+
return _swift_Glibc_fcntl(fd, cmd, value)
115+
}
116+
117+
@warn_unused_result
118+
public func fcntl(
119+
fd: CInt,
120+
_ cmd: CInt,
121+
_ ptr: UnsafeMutablePointer<Void>
122+
) -> CInt {
123+
return _swift_Glibc_fcntlPtr(fd, cmd, ptr)
124+
}
125+
32126
public var S_IFMT: mode_t { return mode_t(0o170000) }
33127
public var S_IFIFO: mode_t { return mode_t(0o010000) }
34128
public var S_IFCHR: mode_t { return mode_t(0o020000) }
@@ -74,3 +168,46 @@ public var SIG_HOLD: __sighandler_t {
74168
}
75169
#endif
76170

171+
//===----------------------------------------------------------------------===//
172+
// semaphore.h
173+
//===----------------------------------------------------------------------===//
174+
175+
/// The value returned by `sem_open()` in the case of failure.
176+
public var SEM_FAILED: UnsafeMutablePointer<sem_t> {
177+
// The value is ABI. Value verified to be correct on Glibc.
178+
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
179+
}
180+
181+
@warn_unused_result
182+
@_silgen_name("_swift_Glibc_sem_open2")
183+
internal func _swift_Glibc_sem_open2(
184+
name: UnsafePointer<CChar>,
185+
_ oflag: CInt
186+
) -> UnsafeMutablePointer<sem_t>
187+
188+
@warn_unused_result
189+
@_silgen_name("_swift_Glibc_sem_open4")
190+
internal func _swift_Glibc_sem_open4(
191+
name: UnsafePointer<CChar>,
192+
_ oflag: CInt,
193+
_ mode: mode_t,
194+
_ value: CUnsignedInt
195+
) -> UnsafeMutablePointer<sem_t>
196+
197+
@warn_unused_result
198+
public func sem_open(
199+
name: UnsafePointer<CChar>,
200+
_ oflag: CInt
201+
) -> UnsafeMutablePointer<sem_t> {
202+
return _swift_Glibc_sem_open2(name, oflag)
203+
}
204+
205+
@warn_unused_result
206+
public func sem_open(
207+
name: UnsafePointer<CChar>,
208+
_ oflag: CInt,
209+
_ mode: mode_t,
210+
_ value: CUnsignedInt
211+
) -> UnsafeMutablePointer<sem_t> {
212+
return _swift_Glibc_sem_open4(name, oflag, mode, value)
213+
}

stdlib/public/Glibc/Misc.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- Misc.c - Glibc overlay helpers -----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <fcntl.h>
14+
#include <sys/types.h>
15+
#include <sys/stat.h>
16+
#include <semaphore.h>
17+
18+
extern int
19+
_swift_Glibc_open(const char *path, int oflag, mode_t mode) {
20+
return open(path, oflag, mode);
21+
}
22+
23+
extern int
24+
_swift_Glibc_openat(int fd, const char *path, int oflag, mode_t mode) {
25+
return openat(fd, path, oflag, mode);
26+
}
27+
28+
extern sem_t *_swift_Glibc_sem_open2(const char *name, int oflag) {
29+
return sem_open(name, oflag);
30+
}
31+
32+
extern sem_t *_swift_Glibc_sem_open4(const char *name, int oflag,
33+
mode_t mode, unsigned int value) {
34+
return sem_open(name, oflag, mode, value);
35+
}
36+
37+
extern int
38+
_swift_Glibc_fcntl(int fd, int cmd, int value) {
39+
return fcntl(fd, cmd, value);
40+
}
41+
42+
extern int
43+
_swift_Glibc_fcntlPtr(int fd, int cmd, void* ptr) {
44+
return fcntl(fd, cmd, ptr);
45+
}
46+

stdlib/public/SDK/Darwin/Darwin.swift

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ func _convertDarwinBooleanToBool(x: DarwinBoolean) -> Bool {
7979
@_transparent
8080
@warn_unused_result
8181
public func && <T : BooleanType>(
82-
lhs: T, @autoclosure rhs: () -> DarwinBoolean
82+
lhs: T,
83+
@autoclosure rhs: () -> DarwinBoolean
8384
) -> Bool {
8485
return lhs.boolValue ? rhs().boolValue : false
8586
}
8687

8788
@_transparent
8889
@warn_unused_result
8990
public func || <T : BooleanType>(
90-
lhs: T, @autoclosure rhs: () -> DarwinBoolean
91+
lhs: T,
92+
@autoclosure rhs: () -> DarwinBoolean
9193
) -> Bool {
9294
return lhs.boolValue ? true : rhs().boolValue
9395
}
@@ -145,38 +147,98 @@ public var stderr : UnsafeMutablePointer<FILE> {
145147

146148
@warn_unused_result
147149
@_silgen_name("_swift_Darwin_open")
148-
func _swift_Darwin_open(path: UnsafePointer<CChar>,
149-
_ oflag: CInt, _ mode: mode_t) -> CInt
150+
func _swift_Darwin_open(
151+
path: UnsafePointer<CChar>,
152+
_ oflag: CInt,
153+
_ mode: mode_t
154+
) -> CInt
150155

151156
@warn_unused_result
152157
@_silgen_name("_swift_Darwin_openat")
153158
func _swift_Darwin_openat(fd: CInt,
154159
_ path: UnsafePointer<CChar>,
155-
_ oflag: CInt, _ mode: mode_t) -> CInt
160+
_ oflag: CInt,
161+
_ mode: mode_t
162+
) -> CInt
156163

157164
@warn_unused_result
158-
public func open(path: UnsafePointer<CChar>, _ oflag: CInt) -> CInt {
165+
public func open(
166+
path: UnsafePointer<CChar>,
167+
_ oflag: CInt
168+
) -> CInt {
159169
return _swift_Darwin_open(path, oflag, 0)
160170
}
161171

162172
@warn_unused_result
163-
public func open(path: UnsafePointer<CChar>, _ oflag: CInt,
164-
_ mode: mode_t) -> CInt {
173+
public func open(
174+
path: UnsafePointer<CChar>,
175+
_ oflag: CInt,
176+
_ mode: mode_t
177+
) -> CInt {
165178
return _swift_Darwin_open(path, oflag, mode)
166179
}
167180

168181
@warn_unused_result
169-
public func openat(fd: CInt, _ path: UnsafePointer<CChar>,
170-
_ oflag: CInt) -> CInt {
182+
public func openat(
183+
fd: CInt,
184+
_ path: UnsafePointer<CChar>,
185+
_ oflag: CInt
186+
) -> CInt {
171187
return _swift_Darwin_openat(fd, path, oflag, 0)
172188
}
173189

174190
@warn_unused_result
175-
public func openat(fd: CInt, _ path: UnsafePointer<CChar>,
176-
_ oflag: CInt, _ mode: mode_t) -> CInt {
191+
public func openat(
192+
fd: CInt,
193+
_ path: UnsafePointer<CChar>,
194+
_ oflag: CInt,
195+
_ mode: mode_t
196+
) -> CInt {
177197
return _swift_Darwin_openat(fd, path, oflag, mode)
178198
}
179199

200+
@warn_unused_result
201+
@_silgen_name("_swift_Darwin_fcntl")
202+
internal func _swift_Darwin_fcntl(
203+
fd: CInt,
204+
_ cmd: CInt,
205+
_ value: CInt
206+
) -> CInt
207+
208+
@warn_unused_result
209+
@_silgen_name("_swift_Darwin_fcntlPtr")
210+
internal func _swift_Darwin_fcntlPtr(
211+
fd: CInt,
212+
_ cmd: CInt,
213+
_ ptr: UnsafeMutablePointer<Void>
214+
) -> CInt
215+
216+
@warn_unused_result
217+
public func fcntl(
218+
fd: CInt,
219+
_ cmd: CInt
220+
) -> CInt {
221+
return _swift_Darwin_fcntl(fd, cmd, 0)
222+
}
223+
224+
@warn_unused_result
225+
public func fcntl(
226+
fd: CInt,
227+
_ cmd: CInt,
228+
_ value: CInt
229+
) -> CInt {
230+
return _swift_Darwin_fcntl(fd, cmd, value)
231+
}
232+
233+
@warn_unused_result
234+
public func fcntl(
235+
fd: CInt,
236+
_ cmd: CInt,
237+
_ ptr: UnsafeMutablePointer<Void>
238+
) -> CInt {
239+
return _swift_Darwin_fcntlPtr(fd, cmd, ptr)
240+
}
241+
180242
public var S_IFMT: mode_t { return mode_t(0o170000) }
181243
public var S_IFIFO: mode_t { return mode_t(0o010000) }
182244
public var S_IFCHR: mode_t { return mode_t(0o020000) }

stdlib/public/SDK/Darwin/Misc.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@
3232
return sem_open(name, oflag, mode, value);
3333
}
3434

35+
extern "C" int
36+
_swift_Darwin_fcntl(int fd, int cmd, int value) {
37+
return fcntl(fd, cmd, value);
38+
}
39+
40+
extern "C" int
41+
_swift_Darwin_fcntlPtr(int fd, int cmd, void* ptr) {
42+
return fcntl(fd, cmd, ptr);
43+
}
44+

0 commit comments

Comments
 (0)