1ENDPWENT(3P) POSIX Programmer's Manual ENDPWENT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 endpwent, getpwent, setpwent — user database functions
13
15 #include <pwd.h>
16
17 void endpwent(void);
18 struct passwd *getpwent(void);
19 void setpwent(void);
20
22 These functions shall retrieve information about users.
23
24 The getpwent() function shall return a pointer to a structure contain‐
25 ing the broken-out fields of an entry in the user database. Each entry
26 in the user database contains a passwd structure. If the user database
27 is not already open, getpwent() shall open it and return a pointer to a
28 passwd structure containing the first entry in the database. There‐
29 after, it shall return a pointer to a passwd structure containing the
30 next entry in the user database. Successive calls can be used to search
31 the entire user database.
32
33 If an end-of-file or an error is encountered on reading, getpwent()
34 shall return a null pointer.
35
36 An implementation that provides extended security controls may impose
37 further implementation-defined restrictions on accessing the user data‐
38 base. In particular, the system may deny the existence of some or all
39 of the user database entries associated with users other than the call‐
40 er.
41
42 The setpwent() function shall rewind the user database so that the next
43 getpwent() call returns the first entry, allowing repeated searches.
44
45 The endpwent() function shall close the user database.
46
47 The setpwent() and endpwent() functions shall not change the setting of
48 errno if successful.
49
50 On error, the setpwent() and endpwent() functions shall set errno to
51 indicate the error.
52
53 Since no value is returned by the setpwent() and endpwent() functions,
54 an application wishing to check for error situations should set errno
55 to 0, then call the function, then check errno.
56
57 These functions need not be thread-safe.
58
60 On successful completion, getpwent() shall return a pointer to a passwd
61 structure. On end-of-file, getpwent() shall return a null pointer and
62 shall not change the setting of errno. On error, getpwent() shall
63 return a null pointer and errno shall be set to indicate the error.
64
65 The application shall not modify the structure to which the return
66 value points, nor any storage areas pointed to by pointers within the
67 structure. The returned pointer, and pointers within the structure,
68 might be invalidated or the structure or the storage areas might be
69 overwritten by a subsequent call to getpwuid(), getpwnam(), or getp‐
70 went(). The returned pointer, and pointers within the structure, might
71 also be invalidated if the calling thread is terminated.
72
74 These functions may fail if:
75
76 EINTR A signal was caught during the operation.
77
78 EIO An I/O error has occurred.
79
80 In addition, getpwent() and setpwent() may fail if:
81
82 EMFILE All file descriptors available to the process are currently
83 open.
84
85 ENFILE The maximum allowable number of files is currently open in the
86 system.
87
88 The following sections are informative.
89
91 Searching the User Database
92 The following example uses the getpwent() function to get successive
93 entries in the user database, returning a pointer to a passwd structure
94 that contains information about each user. The call to endpwent()
95 closes the user database and cleans up.
96
97
98 #include <pwd.h>
99 #include <stdio.h>
100
101 void printname(uid_t uid)
102 {
103 struct passwd *pwd;
104
105 setpwent();
106 while((pwd = getpwent()) != NULL) {
107 if (pwd->pw_uid == uid) {
108 printf("name=%s\n",pwd->pw_name);
109 break;
110 }
111 }
112 endpwent();
113 }
114
116 These functions are provided due to their historical usage. Applica‐
117 tions should avoid dependencies on fields in the password database,
118 whether the database is a single file, or where in the file system name
119 space the database resides. Applications should use getpwuid() whenever
120 possible because it avoids these dependencies.
121
123 None.
124
126 None.
127
129 endgrent(), getlogin(), getpwnam(), getpwuid()
130
131 The Base Definitions volume of POSIX.1‐2017, <pwd.h>
132
134 Portions of this text are reprinted and reproduced in electronic form
135 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
136 table Operating System Interface (POSIX), The Open Group Base Specifi‐
137 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
138 Electrical and Electronics Engineers, Inc and The Open Group. In the
139 event of any discrepancy between this version and the original IEEE and
140 The Open Group Standard, the original IEEE and The Open Group Standard
141 is the referee document. The original Standard can be obtained online
142 at http://www.opengroup.org/unix/online.html .
143
144 Any typographical or formatting errors that appear in this page are
145 most likely to have been introduced during the conversion of the source
146 files to man page format. To report such errors, see https://www.ker‐
147 nel.org/doc/man-pages/reporting_bugs.html .
148
149
150
151IEEE/The Open Group 2017 ENDPWENT(3P)