Source file
src/os/user/lookup_stubs.go
1
2
3
4
5
6
7 package user
8
9 import (
10 "fmt"
11 "os"
12 "runtime"
13 "strconv"
14 )
15
16 func current() (*User, error) {
17 uid := currentUID()
18
19
20 u, err := lookupUserId(uid)
21 if err == nil {
22 return u, nil
23 }
24
25 homeDir, _ := os.UserHomeDir()
26 u = &User{
27 Uid: uid,
28 Gid: currentGID(),
29 Username: os.Getenv("USER"),
30 Name: "",
31 HomeDir: homeDir,
32 }
33
34 switch runtime.GOOS {
35 case "android":
36 if u.Uid == "" {
37 u.Uid = "1"
38 }
39 if u.Username == "" {
40 u.Username = "android"
41 }
42 }
43
44
45 if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
46 return u, nil
47 }
48 var missing string
49 if u.Username == "" {
50 missing = "$USER"
51 }
52 if u.HomeDir == "" {
53 if missing != "" {
54 missing += ", "
55 }
56 missing += "$HOME"
57 }
58 return u, fmt.Errorf("user: Current requires cgo or %s set in environment", missing)
59 }
60
61 func currentUID() string {
62 if id := os.Getuid(); id >= 0 {
63 return strconv.Itoa(id)
64 }
65
66
67
68 return ""
69 }
70
71 func currentGID() string {
72 if id := os.Getgid(); id >= 0 {
73 return strconv.Itoa(id)
74 }
75 return ""
76 }
77
View as plain text