path_unix.odin 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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:strings"
  9. SEPARATOR :: '/'
  10. SEPARATOR_STRING :: `/`
  11. LIST_SEPARATOR :: ':'
  12. is_reserved_name :: proc(path: string) -> bool {
  13. return false
  14. }
  15. is_abs :: proc(path: string) -> bool {
  16. return strings.has_prefix(path, "/")
  17. }
  18. abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
  19. rel := path
  20. if rel == "" {
  21. rel = "."
  22. }
  23. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  24. path_ptr := realpath(rel_cstr, nil)
  25. if path_ptr == nil {
  26. return "", __error()^ == 0
  27. }
  28. defer _unix_free(path_ptr)
  29. path_cstr := cstring(path_ptr)
  30. path_str := strings.clone(string(path_cstr), allocator)
  31. return path_str, true
  32. }
  33. join :: proc(elems: ..string, allocator := context.allocator) -> string {
  34. for e, i in elems {
  35. if e != "" {
  36. p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator)
  37. return clean(p, allocator)
  38. }
  39. }
  40. return ""
  41. }
  42. @(private)
  43. foreign libc {
  44. realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  45. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  46. }
  47. when ODIN_OS == .Darwin {
  48. @(private)
  49. foreign libc {
  50. @(link_name="__error") __error :: proc() -> ^i32 ---
  51. }
  52. } else when ODIN_OS == .OpenBSD {
  53. @(private)
  54. foreign libc {
  55. @(link_name="__errno") __error :: proc() -> ^i32 ---
  56. }
  57. } else {
  58. @(private)
  59. foreign libc {
  60. @(link_name="__errno_location") __error :: proc() -> ^i32 ---
  61. }
  62. }