Skip to content

Commit 6f019e2

Browse files
authored
Merge pull request #1941 from compnerd/temporary-temporaries
2 parents 3acea80 + 7ac5d69 commit 6f019e2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
import CoreFoundation
1111

1212
public func NSTemporaryDirectory() -> String {
13-
#if os(macOS) || os(iOS)
14-
var buf = [Int8](repeating: 0, count: 100)
15-
let r = confstr(_CS_DARWIN_USER_TEMP_DIR, &buf, buf.count)
16-
if r != 0 && r < buf.count {
17-
return String(cString: buf, encoding: .utf8)!
13+
#if canImport(Darwin)
14+
var length: Int = confstr(_CS_DARWIN_USER_TEMP_DIR, nil, 0)
15+
if length > 0 {
16+
var buffer: [Int8] = Array<Int8>(repeating: 0, count: length)
17+
length = confstr(_CS_DARWIN_USER_TEMP_DIR, &buffer, buffer.count)
18+
if length > 0 && length < buffer.count {
19+
return String(cString: buffer, encoding: .utf8)!
20+
}
1821
}
19-
#endif
22+
#endif
2023
if let tmpdir = ProcessInfo.processInfo.environment["TMPDIR"] {
2124
if !tmpdir.hasSuffix("/") {
2225
return tmpdir + "/"

0 commit comments

Comments
 (0)