2
0

path_openbsd.odin 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package os2
  2. import "base:runtime"
  3. import "core:strings"
  4. import "core:sys/posix"
  5. _get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) {
  6. // OpenBSD does not have an API for this, we do our best below.
  7. if len(runtime.args__) <= 0 {
  8. err = .Invalid_Path
  9. return
  10. }
  11. real :: proc(path: cstring, allocator: runtime.Allocator) -> (out: string, err: Error) {
  12. real := posix.realpath(path)
  13. if real == nil {
  14. err = _get_platform_error()
  15. return
  16. }
  17. defer posix.free(real)
  18. return clone_string(string(real), allocator)
  19. }
  20. arg := runtime.args__[0]
  21. sarg := string(arg)
  22. if len(sarg) == 0 {
  23. err = .Invalid_Path
  24. return
  25. }
  26. if sarg[0] == '.' || sarg[0] == '/' {
  27. return real(arg, allocator)
  28. }
  29. temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
  30. buf := strings.builder_make(temp_allocator)
  31. paths := get_env("PATH", temp_allocator)
  32. for dir in strings.split_iterator(&paths, ":") {
  33. strings.builder_reset(&buf)
  34. strings.write_string(&buf, dir)
  35. strings.write_string(&buf, "/")
  36. strings.write_string(&buf, sarg)
  37. cpath := strings.to_cstring(&buf) or_return
  38. if posix.access(cpath, {.X_OK}) == .OK {
  39. return real(cpath, allocator)
  40. }
  41. }
  42. err = .Invalid_Path
  43. return
  44. }