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 >= 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
26           _SVID_SOURCE || _POSIX_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.
88
89       EIO    I/O error.
90
91       EMFILE The  maximum  number (OPEN_MAX) of files was open already in the
92              calling process.
93
94       ENFILE The maximum number of files was open already in the system.
95
96       ENOMEM Insufficient memory to allocate passwd structure.
97
98       ERANGE Insufficient buffer space supplied.
99

FILES

101       /etc/passwd
102              local password database file
103

ATTRIBUTES

105   Multithreading (see pthreads(7))
106       The getpwnam() and getpwuid() functions are not thread-safe.
107
108       The getpwnam_r() and getpwuid_r() functions are thread-safe.
109

CONFORMING TO

111       SVr4, 4.3BSD, POSIX.1-2001.  The pw_gecos field  is  not  specified  in
112       POSIX, but is present on most implementations.
113

NOTES

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

EXAMPLE

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

SEE ALSO

184       endpwent(3),  fgetpwent(3),  getgrnam(3), getpw(3), getpwent(3), getsp‐
185       nam(3), putpwent(3), setpwent(3), passwd(5)
186

COLOPHON

188       This page is part of release 3.53 of the Linux  man-pages  project.   A
189       description  of  the project, and information about reporting bugs, can
190       be found at http://www.kernel.org/doc/man-pages/.
191
192
193
194GNU                               2013-07-22                       GETPWNAM(3)
Impressum