File: homedir_static.go

package info (click to toggle)
golang-dbus 3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch, stretch-backports
  • size: 368 kB
  • ctags: 487
  • sloc: sh: 41; makefile: 6
file content (45 lines) | stat: -rw-r--r-- 617 bytes parent folder | download | duplicates (8)
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 "/"
}