1GETPWNAM(3)                Linux Programmer's Manual               GETPWNAM(3)
2
3
4

NAME

6       getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <pwd.h>
11
12       struct passwd *getpwnam(const char *name);
13
14       struct passwd *getpwuid(uid_t uid);
15
16       int getpwnam_r(const char *name, struct passwd *pwd,
17                      char *buf, size_t buflen, struct passwd **result);
18
19       int getpwuid_r(uid_t uid, struct passwd *pwd,
20                      char *buf, size_t buflen, struct passwd **result);
21
22   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24       getpwnam_r(), getpwuid_r():
25           _POSIX_C_SOURCE
26               || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
27

DESCRIPTION

29       The getpwnam() function returns a pointer to a structure containing the
30       broken-out fields of the record in the  password  database  (e.g.,  the
31       local  password file /etc/passwd, NIS, and LDAP) that matches the user‐
32       name name.
33
34       The getpwuid() function returns a pointer to a structure containing the
35       broken-out  fields  of the record in the password database that matches
36       the user ID uid.
37
38       The passwd structure is defined in <pwd.h> as follows:
39
40           struct passwd {
41               char   *pw_name;       /* username */
42               char   *pw_passwd;     /* user password */
43               uid_t   pw_uid;        /* user ID */
44               gid_t   pw_gid;        /* group ID */
45               char   *pw_gecos;      /* user information */
46               char   *pw_dir;        /* home directory */
47               char   *pw_shell;      /* shell program */
48           };
49
50       See passwd(5) for more information about these fields.
51
52       The getpwnam_r() and getpwuid_r() functions obtain the same information
53       as  getpwnam() and getpwuid(), but store the retrieved passwd structure
54       in the space pointed to by pwd.  The string fields pointed  to  by  the
55       members  of  the  passwd structure are stored in the buffer buf of size
56       buflen.  A pointer to the result (in case of success) or NULL (in  case
57       no entry was found or an error occurred) is stored in *result.
58
59       The call
60
61           sysconf(_SC_GETPW_R_SIZE_MAX)
62
63       returns either -1, without changing errno, or an initial suggested size
64       for buf.  (If this size is too small, the call fails  with  ERANGE,  in
65       which case the caller can retry with a larger buffer.)
66

RETURN VALUE

68       The  getpwnam()  and  getpwuid() functions return a pointer to a passwd
69       structure, or NULL if the matching entry  is  not  found  or  an  error
70       occurs.   If an error occurs, errno is set appropriately.  If one wants
71       to check errno after the call, it should be  set  to  zero  before  the
72       call.
73
74       The  return value may point to a static area, and may be overwritten by
75       subsequent calls to getpwent(3), getpwnam(), or  getpwuid().   (Do  not
76       pass the returned pointer to free(3).)
77
78       On  success, getpwnam_r() and getpwuid_r() return zero, and set *result
79       to pwd.  If no matching password  record  was  found,  these  functions
80       return  0 and store NULL in *result.  In case of error, an error number
81       is returned, and NULL is stored in *result.
82

ERRORS

84       0 or ENOENT or ESRCH or EBADF or EPERM or ...
85              The given name or uid was not found.
86
87       EINTR  A signal was caught; see signal(7).
88
89       EIO    I/O error.
90
91       EMFILE The per-process limit on the number of open file descriptors has
92              been reached.
93
94       ENFILE The system-wide limit on the total number of open files has been
95              reached.
96
97       ENOMEM Insufficient memory to allocate passwd structure.
98
99       ERANGE Insufficient buffer space supplied.
100

FILES

102       /etc/passwd
103              local password database file
104

ATTRIBUTES

106       For  an  explanation  of  the  terms  used   in   this   section,   see
107       attributes(7).
108
109       ┌──────────────┬───────────────┬─────────────────────────────┐
110Interface     Attribute     Value                       
111       ├──────────────┼───────────────┼─────────────────────────────┤
112getpwnam()    │ Thread safety │ MT-Unsafe race:pwnam locale │
113       ├──────────────┼───────────────┼─────────────────────────────┤
114getpwuid()    │ Thread safety │ MT-Unsafe race:pwuid locale │
115       ├──────────────┼───────────────┼─────────────────────────────┤
116getpwnam_r(), │ Thread safety │ MT-Safe locale              │
117getpwuid_r()  │               │                             │
118       └──────────────┴───────────────┴─────────────────────────────┘

CONFORMING TO

120       POSIX.1-2001,  POSIX.1-2008,  SVr4,  4.3BSD.  The pw_gecos field is not
121       specified in POSIX, but is present on most implementations.
122

NOTES

124       The formulation given above under "RETURN VALUE" is from  POSIX.1-2001.
125       It  does not call "not found" an error, and hence does not specify what
126       value errno might have in this situation.  But that makes it impossible
127       to  recognize  errors.   One  might argue that according to POSIX errno
128       should be left unchanged if an entry is not found.  Experiments on var‐
129       ious UNIX-like systems show that lots of different values occur in this
130       situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK,  EPERM,  and  probably
131       others.
132
133       The  pw_dir field contains the name of the initial working directory of
134       the user.  Login programs use the value of this field to initialize the
135       HOME  environment  variable  for  the login shell.  An application that
136       wants to determine its user's home directory should inspect  the  value
137       of  HOME  (rather than the value getpwuid(getuid())->pw_dir) since this
138       allows the user to modify their notion of "the home directory" during a
139       login  session.   To  determine the (initial) home directory of another
140       user, it is necessary to use getpwnam("username")->pw_dir or similar.
141

EXAMPLE

143       The program below demonstrates the use of getpwnam_r() to find the full
144       username  and user ID for the username supplied as a command-line argu‐
145       ment.
146
147       #include <pwd.h>
148       #include <stdio.h>
149       #include <stdlib.h>
150       #include <unistd.h>
151       #include <errno.h>
152
153       int
154       main(int argc, char *argv[])
155       {
156           struct passwd pwd;
157           struct passwd *result;
158           char *buf;
159           size_t bufsize;
160           int s;
161
162           if (argc != 2) {
163               fprintf(stderr, "Usage: %s username\n", argv[0]);
164               exit(EXIT_FAILURE);
165           }
166
167           bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
168           if (bufsize == -1)          /* Value was indeterminate */
169               bufsize = 16384;        /* Should be more than enough */
170
171           buf = malloc(bufsize);
172           if (buf == NULL) {
173               perror("malloc");
174               exit(EXIT_FAILURE);
175           }
176
177           s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
178           if (result == NULL) {
179               if (s == 0)
180                   printf("Not found\n");
181               else {
182                   errno = s;
183                   perror("getpwnam_r");
184               }
185               exit(EXIT_FAILURE);
186           }
187
188           printf("Name: %s; UID: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid);
189           exit(EXIT_SUCCESS);
190       }
191

SEE ALSO

193       endpwent(3), fgetpwent(3), getgrnam(3), getpw(3),  getpwent(3),  getsp‐
194       nam(3), putpwent(3), setpwent(3), passwd(5)
195

COLOPHON

197       This  page  is  part of release 5.04 of the Linux man-pages project.  A
198       description of the project, information about reporting bugs,  and  the
199       latest     version     of     this    page,    can    be    found    at
200       https://www.kernel.org/doc/man-pages/.
201
202
203
204GNU                               2019-03-06                       GETPWNAM(3)
Impressum