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