path_unix.odin 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //+build linux, darwin, freebsd, openbsd
  2. package filepath
  3. when ODIN_OS == .Darwin {
  4. foreign import libc "System.framework"
  5. } else {
  6. foreign import libc "system:c"
  7. }
  8. import "core:runtime"
  9. import "core:strings"
  10. SEPARATOR :: '/'
  11. SEPARATOR_STRING :: `/`
  12. LIST_SEPARATOR :: ':'
  13. is_reserved_name :: proc(path: string) -> bool {
  14. return false
  15. }
  16. is_abs :: proc(path: string) -> bool {
  17. return strings.has_prefix(path, "/")
  18. }
  19. abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
  20. rel := path
  21. if rel == "" {
  22. rel = "."
  23. }
  24. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  25. path_ptr := realpath(rel_cstr, nil)
  26. if path_ptr == nil {
  27. return "", __error()^ == 0
  28. }
  29. defer _unix_free(path_ptr)
  30. path_cstr := cstring(path_ptr)
  31. path_str := strings.clone(string(path_cstr), allocator)
  32. return path_str, true
  33. }
  34. join :: proc(elems: []string, allocator := context.allocator) -> string {
  35. for e, i in elems {
  36. if e != "" {
  37. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  38. p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator)
  39. return clean(p, allocator)
  40. }
  41. }
  42. return ""
  43. }
  44. @(private)
  45. foreign libc {
  46. realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  47. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  48. }
  49. when ODIN_OS == .Darwin {
  50. @(private)
  51. foreign libc {
  52. @(link_name="__error") __error :: proc() -> ^i32 ---
  53. }
  54. } else when ODIN_OS == .OpenBSD {
  55. @(private)
  56. foreign libc {
  57. @(link_name="__errno") __error :: proc() -> ^i32 ---
  58. }
  59. } else {
  60. @(private)
  61. foreign libc {
  62. @(link_name="__errno_location") __error :: proc() -> ^i32 ---
  63. }
  64. }