path_unix.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris
  5. package os
  6. const (
  7. PathSeparator = '/' // OS-specific path separator
  8. PathListSeparator = ':' // OS-specific path list separator
  9. )
  10. // IsPathSeparator reports whether c is a directory separator character.
  11. func IsPathSeparator(c uint8) bool {
  12. return PathSeparator == c
  13. }
  14. // basename removes trailing slashes and the leading directory name from path name.
  15. func basename(name string) string {
  16. i := len(name) - 1
  17. // Remove trailing slashes
  18. for ; i > 0 && name[i] == '/'; i-- {
  19. name = name[:i]
  20. }
  21. // Remove leading directory name
  22. for i--; i >= 0; i-- {
  23. if name[i] == '/' {
  24. name = name[i+1:]
  25. break
  26. }
  27. }
  28. return name
  29. }
  30. // splitPath returns the base name and parent directory.
  31. func splitPath(path string) (string, string) {
  32. // if no better parent is found, the path is relative from "here"
  33. dirname := "."
  34. // Remove all but one leading slash.
  35. for len(path) > 1 && path[0] == '/' && path[1] == '/' {
  36. path = path[1:]
  37. }
  38. i := len(path) - 1
  39. // Remove trailing slashes.
  40. for ; i > 0 && path[i] == '/'; i-- {
  41. path = path[:i]
  42. }
  43. // if no slashes in path, base is path
  44. basename := path
  45. // Remove leading directory path
  46. for i--; i >= 0; i-- {
  47. if path[i] == '/' {
  48. if i == 0 {
  49. dirname = path[:1]
  50. } else {
  51. dirname = path[:i]
  52. }
  53. basename = path[i+1:]
  54. break
  55. }
  56. }
  57. return dirname, basename
  58. }
  59. func fixRootDirectory(p string) string {
  60. return p
  61. }