libgo: update to go1.9
[official-gcc.git] / libgo / go / os / user / lookup_stubs.go
blobd23870fda883b73948c1529322990fe0bf3e3635
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 // +build !cgo,!windows,!plan9 android
7 package user
9 import (
10 "errors"
11 "fmt"
12 "os"
13 "runtime"
14 "strconv"
17 func init() {
18 groupImplemented = false
21 func current() (*User, error) {
22 u := &User{
23 Uid: currentUID(),
24 Gid: currentGID(),
25 Username: os.Getenv("USER"),
26 Name: "", // ignored
27 HomeDir: os.Getenv("HOME"),
29 // On NaCL and Android, return a dummy user instead of failing.
30 switch runtime.GOOS {
31 case "nacl":
32 if u.Uid == "" {
33 u.Uid = "1"
35 if u.Username == "" {
36 u.Username = "nacl"
38 if u.HomeDir == "" {
39 u.HomeDir = "/"
41 case "android":
42 if u.Uid == "" {
43 u.Uid = "1"
45 if u.Username == "" {
46 u.Username = "android"
48 if u.HomeDir == "" {
49 u.HomeDir = "/sdcard"
52 // cgo isn't available, but if we found the minimum information
53 // without it, use it:
54 if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
55 return u, nil
57 return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
60 func listGroups(*User) ([]string, error) {
61 if runtime.GOOS == "android" {
62 return nil, errors.New("user: GroupIds not implemented on Android")
64 return nil, errors.New("user: GroupIds requires cgo")
67 func currentUID() string {
68 if id := os.Getuid(); id >= 0 {
69 return strconv.Itoa(id)
71 // Note: Windows returns -1, but this file isn't used on
72 // Windows anyway, so this empty return path shouldn't be
73 // used.
74 return ""
77 func currentGID() string {
78 if id := os.Getgid(); id >= 0 {
79 return strconv.Itoa(id)
81 return ""