Skip to content

Commit 5d2adfd

Browse files
committed
Create a C callable _NSCurrentDirectoryPath function
In order to remove the libc getcwd dependency and move it to the the foundation FileManager.currentDirectoryPath API we needed a C callable method that the swift-corelib-foundation could use in its NSURL implementation. This will allow any platform specifics in the FileManager.currentDirectoryPath implementation (like prefix striping on Windows) to be handled in a single location.
1 parent 7566cd4 commit 5d2adfd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Sources/FoundationEssentials/FileManager/FileManager+Directories.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,27 @@ extension _FileManagerImpl {
510510
#endif
511511
}
512512
}
513+
514+
@_cdecl("_NSCurrentDirectoryPath")
515+
internal func _NSCurrentDirectoryPath(_ buffer: UnsafeMutablePointer<CChar>?, _ size: Int) -> Bool {
516+
guard let buffer = buffer, size > 0 else {
517+
return false // Invalid parameters
518+
}
519+
520+
let currentPath = FileManager.default.currentDirectoryPath
521+
522+
// Convert to C string representation
523+
let cString = currentPath.utf8CString
524+
let requiredSize = cString.count // includes null terminator
525+
526+
if requiredSize > size {
527+
return false // Buffer too small
528+
}
529+
530+
// Copy the string to the buffer
531+
cString.withUnsafeBufferPointer { sourceBuffer in
532+
buffer.initialize(from: sourceBuffer.baseAddress!, count: requiredSize)
533+
}
534+
535+
return true // Success
536+
}

0 commit comments

Comments
 (0)