From 5d2adfd48fad9d78cd7122ec62b0fee9086e83a7 Mon Sep 17 00:00:00 2001 From: Dave Inglis Date: Tue, 14 Oct 2025 16:23:12 -0400 Subject: [PATCH] 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. --- .../FileManager/FileManager+Directories.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift index e1db5a9f4..48ca66849 100644 --- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift @@ -510,3 +510,27 @@ extension _FileManagerImpl { #endif } } + +@_cdecl("_NSCurrentDirectoryPath") +internal func _NSCurrentDirectoryPath(_ buffer: UnsafeMutablePointer?, _ size: Int) -> Bool { + guard let buffer = buffer, size > 0 else { + return false // Invalid parameters + } + + let currentPath = FileManager.default.currentDirectoryPath + + // Convert to C string representation + let cString = currentPath.utf8CString + let requiredSize = cString.count // includes null terminator + + if requiredSize > size { + return false // Buffer too small + } + + // Copy the string to the buffer + cString.withUnsafeBufferPointer { sourceBuffer in + buffer.initialize(from: sourceBuffer.baseAddress!, count: requiredSize) + } + + return true // Success +} \ No newline at end of file