os/os2: add get_executable_path and get_executable_directory

This commit is contained in:
Laytan Laats
2025-01-21 18:54:45 +01:00
parent 223970671f
commit b673642412
10 changed files with 234 additions and 3 deletions
+21 -1
View File
@@ -1,9 +1,10 @@
#+private
package os2
import "base:runtime"
import "core:strings"
import "core:strconv"
import "base:runtime"
import "core:sys/linux"
_Path_Separator :: '/'
@@ -171,6 +172,25 @@ _set_working_directory :: proc(dir: string) -> Error {
return _get_platform_error(linux.chdir(dir_cstr))
}
_get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) {
TEMP_ALLOCATOR_GUARD()
buf := make([dynamic]byte, 1024, temp_allocator()) or_return
for {
n, errno := linux.readlink("/proc/self/exe", buf[:])
if errno != .NONE {
err = _get_platform_error(errno)
return
}
if n < len(buf) {
return clone_string(string(buf[:n]), allocator)
}
resize(&buf, len(buf)*2) or_return
}
}
_get_full_path :: proc(fd: linux.Fd, allocator: runtime.Allocator) -> (fullpath: string, err: Error) {
PROC_FD_PATH :: "/proc/self/fd/"