OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / user / lookup_unix.go
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 package user
6
7 import (
8         "fmt"
9         "os"
10         "strings"
11         "syscall"
12         "unsafe"
13 )
14
15 /*
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <pwd.h>
19 #include <stdlib.h>
20
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);
24 }
25 */
26
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")
29
30 func init() {
31         implemented = true
32 }
33
34 // Lookup looks up a user by username. If the user cannot be found,
35 // the returned error is of type UnknownUserError.
36 func Lookup(username string) (*User, os.Error) {
37         return lookup(-1, username, true)
38 }
39
40 // LookupId looks up a user by userid. If the user cannot be found,
41 // the returned error is of type UnknownUserIdError.
42 func LookupId(uid int) (*User, os.Error) {
43         return lookup(uid, "", false)
44 }
45
46 func lookup(uid int, username string, lookupByName bool) (*User, os.Error) {
47         var pwd syscall.Passwd
48         var result *syscall.Passwd
49
50         // FIXME: Should let buf grow if necessary.
51         const bufSize = 1024
52         buf := make([]byte, bufSize)
53         if lookupByName {
54                 rv := libc_getpwnam_r(syscall.StringBytePtr(username),
55                         &pwd,
56                         &buf[0],
57                         bufSize,
58                         &result)
59                 if rv != 0 {
60                         return nil, fmt.Errorf("user: lookup username %s: %s", username, os.Errno(syscall.GetErrno()))
61                 }
62                 if result == nil {
63                         return nil, UnknownUserError(username)
64                 }
65         } else {
66                 rv := libc_getpwuid_r(syscall.Uid_t(uid),
67                         &pwd,
68                         &buf[0],
69                         bufSize,
70                         &result)
71                 if rv != 0 {
72                         return nil, fmt.Errorf("user: lookup userid %d: %s", uid, os.Errno(syscall.GetErrno()))
73                 }
74                 if result == nil {
75                         return nil, UnknownUserIdError(uid)
76                 }
77         }
78         u := &User{
79                 Uid:      int(pwd.Pw_uid),
80                 Gid:      int(pwd.Pw_gid),
81                 Username: syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_name))),
82                 Name:     syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_gecos))),
83                 HomeDir:  syscall.BytePtrToString((*byte)(unsafe.Pointer(pwd.Pw_dir))),
84         }
85         // The pw_gecos field isn't quite standardized.  Some docs
86         // say: "It is expected to be a comma separated list of
87         // personal data where the first item is the full name of the
88         // user."
89         if i := strings.Index(u.Name, ","); i >= 0 {
90                 u.Name = u.Name[:i]
91         }
92         return u, nil
93 }