Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Sources/Testing/Support/Additions/CommandLineAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@ extension CommandLine {
}
return result!
#elseif os(Linux) || os(Android)
return try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX) * 2) { buffer in
let readCount = readlink("/proc/self/exe", buffer.baseAddress!, buffer.count - 1)
guard readCount >= 0 else {
throw CError(rawValue: swt_errno())
var result: String?
#if DEBUG
var bufferCount = Int(1) // force looping
#else
var bufferCount = Int(PATH_MAX)
#endif
while result == nil {
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: bufferCount) { buffer in
let readCount = readlink("/proc/self/exe", buffer.baseAddress!, buffer.count)
guard readCount >= 0 else {
throw CError(rawValue: swt_errno())
}
if readCount < buffer.count {
buffer[readCount] = 0 // NUL-terminate the string.
result = String(cString: buffer.baseAddress!)
} else {
bufferCount += Int(PATH_MAX) // add more space and try again
}
}
buffer[readCount] = 0 // NUL-terminate the string.
return String(cString: buffer.baseAddress!)
}
return result!
#elseif os(FreeBSD)
var mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1]
return try mib.withUnsafeMutableBufferPointer { mib in
Expand Down
5 changes: 2 additions & 3 deletions Sources/_TestingInternals/ExecutablePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ static constinit std::atomic<const char *> earlyCWD { nullptr };
/// path (which can occur when manually invoking the test executable.)
__attribute__((__constructor__(101), __used__))
static void captureEarlyCWD(void) {
static char buffer[PATH_MAX * 2];
if (getcwd(buffer, sizeof(buffer))) {
earlyCWD.store(buffer);
if (auto cwd = getcwd(nil, 0)) {
earlyCWD.store(cwd);
}
}
#endif
Expand Down