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.
17 #include <sys/types.h>
21 static int mygetpwuid_r(int uid, struct passwd *pwd,
22 char *buf, size_t buflen, struct passwd **result) {
23 return getpwuid_r(uid, pwd, buf, buflen, result);
27 func libc_getpwnam_r(name *byte, pwd *syscall.Passwd, buf *byte, buflen syscall.Size_t, result **syscall.Passwd) int __asm__ ("getpwnam_r")
28 func libc_getpwuid_r(uid syscall.Uid_t, pwd *syscall.Passwd, buf *byte, buflen syscall.Size_t, result **syscall.Passwd) int __asm__ ("getpwuid_r")
30 // Lookup looks up a user by username. If the user cannot be found,
31 // the returned error is of type UnknownUserError.
32 func Lookup(username string) (*User, os.Error) {
33 return lookup(-1, username, true)
36 // LookupId looks up a user by userid. If the user cannot be found,
37 // the returned error is of type UnknownUserIdError.
38 func LookupId(uid int) (*User, os.Error) {
39 return lookup(uid, "", false)
42 func lookup(uid int, username string, lookupByName bool) (*User, os.Error) {
43 var pwd syscall.Passwd
44 var result *syscall.Passwd
46 // FIXME: Should let buf grow if necessary.
48 buf := make([]byte, bufSize)
50 rv := libc_getpwnam_r(syscall.StringBytePtr(username),
56 return nil, fmt.Errorf("user: lookup username %s: %s", username, os.Errno(syscall.GetErrno()))
59 return nil, UnknownUserError(username)
62 rv := libc_getpwuid_r(syscall.Uid_t(uid),
68 return nil, fmt.Errorf("user: lookup userid %d: %s", uid, os.Errno(syscall.GetErrno()))
71 return nil, UnknownUserIdError(uid)
77 Username: syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_name))),
78 Name: syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_gecos))),
79 HomeDir: syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_dir))),
81 // The pw_gecos field isn't quite standardized. Some docs
82 // say: "It is expected to be a comma separated list of
83 // personal data where the first item is the full name of the
85 if i := strings.Index(u.Name, ","); i >= 0 {