1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* UNIX password file access module */
36 #include <sys/types.h>
39 static char pwd__doc__
[] = "\
40 This module provides access to the Unix password database.\n\
41 It is available on all Unix versions.\n\
43 Password database entries are reported as 7-tuples containing the following\n\
44 items from the password database (see `<pwd.h>'), in order:\n\
45 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
46 The uid and gid items are integers, all others are strings. An\n\
47 exception is raised if the entry asked for cannot be found.";
55 /* For faking the GECOS field. - [cjh] */
58 be_user
= getenv( "USER" );
65 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
66 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
67 for later versions you may have to remove this */
68 (long)p
->pw_short_pad1
, /* ugh-NeXT broke the padding */
69 (long)p
->pw_short_pad2
,
75 /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
76 be_user
? be_user
: "baron",
84 static char pwd_getpwuid__doc__
[] = "\
85 getpwuid(uid) -> entry\n\
86 Return the password database entry for the given numeric user ID.\n\
87 See pwd.__doc__ for more on password database entries.";
90 pwd_getpwuid(self
, args
)
96 if (!PyArg_Parse(args
, "i", &uid
))
98 if ((p
= getpwuid(uid
)) == NULL
) {
99 PyErr_SetString(PyExc_KeyError
, "getpwuid(): uid not found");
105 static char pwd_getpwnam__doc__
[] = "\
106 getpwnam(name) -> entry\n\
107 Return the password database entry for the given user name.\n\
108 See pwd.__doc__ for more on password database entries.";
111 pwd_getpwnam(self
, args
)
117 if (!PyArg_Parse(args
, "s", &name
))
119 if ((p
= getpwnam(name
)) == NULL
) {
120 PyErr_SetString(PyExc_KeyError
, "getpwnam(): name not found");
127 static char pwd_getpwall__doc__
[] = "\
128 getpwall() -> list_of_entries\n\
129 Return a list of all available password database entries, \
130 in arbitrary order.\n\
131 See pwd.__doc__ for more on password database entries.";
134 pwd_getpwall(self
, args
)
140 if (!PyArg_NoArgs(args
))
142 if ((d
= PyList_New(0)) == NULL
)
145 while ((p
= getpwent()) != NULL
) {
146 PyObject
*v
= mkpwent(p
);
147 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
158 static PyMethodDef pwd_methods
[] = {
159 {"getpwuid", pwd_getpwuid
, 0, pwd_getpwuid__doc__
},
160 {"getpwnam", pwd_getpwnam
, 0, pwd_getpwnam__doc__
},
162 {"getpwall", pwd_getpwall
, 0, pwd_getpwall__doc__
},
164 {NULL
, NULL
} /* sentinel */
170 Py_InitModule4("pwd", pwd_methods
, pwd__doc__
,
171 (PyObject
*)NULL
, PYTHON_API_VERSION
);