aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/godbus/dbus/homedir_static.go
blob: b9d9cb5525a397feb0214f077afbc24d790a3280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// +build static_build

package dbus

import (
	"bufio"
	"os"
	"strconv"
	"strings"
)

func lookupHomeDir() string {
	myUid := os.Getuid()

	f, err := os.Open("/etc/passwd")
	if err != nil {
		return "/"
	}
	defer f.Close()

	s := bufio.NewScanner(f)

	for s.Scan() {
		if err := s.Err(); err != nil {
			break
		}

		line := strings.TrimSpace(s.Text())
		if line == "" {
			continue
		}

		parts := strings.Split(line, ":")

		if len(parts) >= 6 {
			uid, err := strconv.Atoi(parts[2])
			if err == nil && uid == myUid {
				return parts[5]
			}
		}
	}

	// Default to / if we can't get a better value
	return "/"
}