-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgethomedir.go
More file actions
97 lines (78 loc) · 1.75 KB
/
gethomedir.go
File metadata and controls
97 lines (78 loc) · 1.75 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// gethomedir.go
// get home directory without using os/user
//
// Copyright (c) 2024-2026 jjb
// All rights reserved.
//
// This source code is licensed under the MIT license found
// in the root directory of this source tree.
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
)
// getHomeDir looks up a user's home directory from /etc/passwd.
func getHomeDir(username string) (string, error) {
file, err := os.Open("/etc/passwd")
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") || line == "" {
continue
}
// Format: username:password:UID:GID:GECOS:home:shell
parts := strings.SplitN(line, ":", 7)
if len(parts) < 7 {
continue
}
if parts[0] == username {
return parts[5], nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
// User not found
return "", nil
}
// expandHome expands ~ and ~user prefixes in paths when possible.
func expandHome(path string) string {
if path == "~" {
if home, err := os.UserHomeDir(); err == nil {
return home
}
return path
}
if strings.HasPrefix(path, "~/") {
if home, err := os.UserHomeDir(); err == nil {
return filepath.Join(home, path[2:])
}
return path
}
if strings.HasPrefix(path, "~") {
remaining := path[1:]
var userPart, relative string
if idx := strings.IndexByte(remaining, '/'); idx >= 0 {
userPart = remaining[:idx]
relative = remaining[idx+1:]
} else {
userPart = remaining
relative = ""
}
homeDir, err := getHomeDir(userPart)
if err == nil && homeDir != "" {
if relative != "" {
return filepath.Join(homeDir, relative)
}
return homeDir
}
}
return path
}
// vim: set ts=4 sw=4 noet: