Repository: apple/swift-nio
File: Sources/_NIOFileSystem/Internal/System Calls/Syscall.swift (Swift)

Code before:
    }

    static func homeDirectoryForCurrentUser() -> Result<FilePath, Errno> {
        if let home = getenv("HOME"), home.pointee != 0 {
            return .success(FilePath(String(cString: home)))
        }

        #if os(Windows)
        if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
            return .success(FilePath(String(cString: profile)))
        }
        return .failure(.noSuchFileOrDirectory)
        #elseif canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
        return getpwuid_r(uid: Syscall.getuid())
        #else
        return .failure(.noSuchFileOrDirectory)
        #endif
    }

    #if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
    static func getpwuid_r(uid: uid_t) -> Result<FilePath, Errno> {
        var pwd = passwd()
        var result: UnsafeMutablePointer<passwd>? = nil
        var buffer = [CChar](repeating: 0, count: 256)

        while true {
            let lookupResult = buffer.withUnsafeMutableBufferPointer { pointer in
                withUnsafeMutablePointer(to: &result) { resultPointer in
                    nothingOrErrno(retryOnInterrupt: true) {
                        libc_getpwuid_r(
                            uid,
                            &pwd,
                            pointer.baseAddress!,
                            pointer.count,
                            resultPointer
                        )
                    }
                }
            }

            switch lookupResult {
            case .success:
                guard result != nil, let directoryPointer = pwd.pw_dir else {
                    return .failure(.noSuchFileOrDirectory)
                }
                return .success(FilePath(String(cString: directoryPointer)))

            case .failure(.outOfRange):
                buffer.append(contentsOf: repeatElement(0, count: buffer.count))
                continue


Review comment (suggestion):
If we use `withUnsafeTemporaryAllocation` here we can avoid the loop by setting the temporary allocation quite large. 1kb is acceptable here.

Code after:
    }

    @_spi(Testing)
    public static func homeDirectoryFromEnvironment() -> FilePath? {
        if let home = getenv("HOME"), home.pointee != 0 {
            return FilePath(String(cString: home))
        }
        #if os(Windows)
        if let profile = getenv("USERPROFILE"), profile.pointee != 0 {
            return FilePath(String(cString: profile))
        }
        #endif
        return nil
    }

    #if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
    @_spi(Testing)
    public static func homeDirectoryFromPasswd() -> Result<FilePath, Errno> {
        let uid = Syscall.getuid()
        var pwd = passwd()
        var result: UnsafeMutablePointer<passwd>? = nil

        return withUnsafeTemporaryAllocation(of: CChar.self, capacity: 1024) { buffer in
            let callResult = nothingOrErrno(retryOnInterrupt: true) {
                libc_getpwuid_r(
                    uid,
                    &pwd,
                    buffer.baseAddress!,
                    buffer.count,
                    &result
                )
            }
            switch callResult {
            case .success:
                guard result != nil, let directoryPointer = pwd.pw_dir else {
                    return .failure(.noSuchFileOrDirectory)
                }
                return .success(FilePath(String(cString: directoryPointer)))
            case .failure(let errno):
                return .failure(errno)
            }
        }
    }
    #endif
}