Hongtae/SwiftVVD

strncpy_s not available on Linux

andymule opened this issue · 1 comments

Not sure what is best solution, but here's one. Maybe windows needs that too? IDK

//
//  File: VulkanUnsafePointer.swift
//  Author: Hongtae Kim (tiff2766@gmail.com)
//
//  Copyright (c) 2022-2023 Hongtae Kim. All rights reserved.
//

#if ENABLE_VULKAN
import Foundation
import Vulkan

#if os(Linux)
import Glibc

func strncpy_s(_ dest: UnsafeMutablePointer<CChar>, _ destSize: Int, _ src: UnsafePointer<CChar>, _ count: Int) -> Int {
    // Check if the destination size is zero or less
    if destSize <= 0 {
        return -1 // Invalid destination size
    }

    // Ensure that we do not copy more than the destination can hold
    let copyCount = min(count, destSize - 1)

    // Perform the copy
    strncpy(dest, src, copyCount)

    // Ensure null termination
    dest[copyCount] = 0

    // If the source string is longer than the destination buffer, return an error
    if count >= destSize {
        return -1 // Indicate truncation error
    }

    return 0 // Indicate success
}
#endif

Thank you for reporting this.
I've fixed it to use strncpy instead of strncpy_s.
It should build without any problems now.
If you have any other issues, feel free to let me know.