1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 //go:build (!cgo && !windows && !plan9) || android || (osusergo && !windows && !plan9)
16 func current() (*User
, error
) {
18 // $USER and /etc/passwd may disagree; prefer the latter if we can get it.
19 // See issue 27524 for more information.
20 u
, err
:= lookupUserId(uid
)
25 homeDir
, _
:= os
.UserHomeDir()
29 Username
: os
.Getenv("USER"),
33 // On Android, return a dummy user instead of failing.
40 u
.Username
= "android"
43 // cgo isn't available, but if we found the minimum information
44 // without it, use it:
45 if u
.Uid
!= "" && u
.Username
!= "" && u
.HomeDir
!= "" {
58 return u
, fmt
.Errorf("user: Current requires cgo or %s set in environment", missing
)
61 func currentUID() string {
62 if id
:= os
.Getuid(); id
>= 0 {
63 return strconv
.Itoa(id
)
65 // Note: Windows returns -1, but this file isn't used on
66 // Windows anyway, so this empty return path shouldn't be
71 func currentGID() string {
72 if id
:= os
.Getgid(); id
>= 0 {
73 return strconv
.Itoa(id
)