homedir_static.go 617 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // +build static_build
  2. package dbus
  3. import (
  4. "bufio"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. func lookupHomeDir() string {
  10. myUid := os.Getuid()
  11. f, err := os.Open("/etc/passwd")
  12. if err != nil {
  13. return "/"
  14. }
  15. defer f.Close()
  16. s := bufio.NewScanner(f)
  17. for s.Scan() {
  18. if err := s.Err(); err != nil {
  19. break
  20. }
  21. line := strings.TrimSpace(s.Text())
  22. if line == "" {
  23. continue
  24. }
  25. parts := strings.Split(line, ":")
  26. if len(parts) >= 6 {
  27. uid, err := strconv.Atoi(parts[2])
  28. if err == nil && uid == myUid {
  29. return parts[5]
  30. }
  31. }
  32. }
  33. // Default to / if we can't get a better value
  34. return "/"
  35. }