Swift on macOS can access POSIX APIs by importing the Darwin
module. This gives you access to low-level system calls, file operations, and other Unix functionality. In my case I needed to call openpty()
function from Swift application and handle raw file descriptors.
import Darwin
// Example: Get current working directory
let cwd = getcwd(nil, 0)
if let path = cwd {
print("Current directory: \(String(cString: path))")
free(cwd)
}
// Example: Create a directory (if app is not sandboxed)
let result = mkdir("/tmp/testdir", 0o755)
if result == 0 {
print("Directory created successfully")
} else {
print("Failed to create directory: \(errno)")
}