github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/os/user/lookup_stubs.go (about)

     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.
     4  
     5  // +build !cgo,!windows,!plan9,!android
     6  
     7  package user
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"os"
    13  	"runtime"
    14  	"strconv"
    15  )
    16  
    17  func init() {
    18  	userImplemented = false
    19  	groupImplemented = false
    20  }
    21  
    22  func current() (*User, error) {
    23  	u := &User{
    24  		Uid:      currentUID(),
    25  		Gid:      currentGID(),
    26  		Username: os.Getenv("USER"),
    27  		Name:     "", // ignored
    28  		HomeDir:  os.Getenv("HOME"),
    29  	}
    30  	if runtime.GOOS == "nacl" {
    31  		if u.Uid == "" {
    32  			u.Uid = "1"
    33  		}
    34  		if u.Username == "" {
    35  			u.Username = "nacl"
    36  		}
    37  		if u.HomeDir == "" {
    38  			u.HomeDir = "/home/nacl"
    39  		}
    40  	}
    41  	// cgo isn't available, but if we found the minimum information
    42  	// without it, use it:
    43  	if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
    44  		return u, nil
    45  	}
    46  	return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
    47  }
    48  
    49  func lookupUser(username string) (*User, error) {
    50  	return nil, errors.New("user: Lookup requires cgo")
    51  }
    52  
    53  func lookupUserId(uid string) (*User, error) {
    54  	return nil, errors.New("user: LookupId requires cgo")
    55  }
    56  
    57  func lookupGroup(groupname string) (*Group, error) {
    58  	return nil, errors.New("user: LookupGroup requires cgo")
    59  }
    60  
    61  func lookupGroupId(string) (*Group, error) {
    62  	return nil, errors.New("user: LookupGroupId requires cgo")
    63  }
    64  
    65  func listGroups(*User) ([]string, error) {
    66  	return nil, errors.New("user: GroupIds requires cgo")
    67  }
    68  
    69  func currentUID() string {
    70  	if id := os.Getuid(); id >= 0 {
    71  		return strconv.Itoa(id)
    72  	}
    73  	// Note: Windows returns -1, but this file isn't used on
    74  	// Windows anyway, so this empty return path shouldn't be
    75  	// used.
    76  	return ""
    77  }
    78  
    79  func currentGID() string {
    80  	if id := os.Getgid(); id >= 0 {
    81  		return strconv.Itoa(id)
    82  	}
    83  	return ""
    84  }