2 /* UNIX password file access module */
9 static char pwd__doc__
[] = "\
10 This module provides access to the Unix password database.\n\
11 It is available on all Unix versions.\n\
13 Password database entries are reported as 7-tuples containing the following\n\
14 items from the password database (see `<pwd.h>'), in order:\n\
15 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
16 The uid and gid items are integers, all others are strings. An\n\
17 exception is raised if the entry asked for cannot be found.";
21 mkpwent(struct passwd
*p
)
27 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
28 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
29 for later versions you may have to remove this */
30 (long)p
->pw_short_pad1
, /* ugh-NeXT broke the padding */
31 (long)p
->pw_short_pad2
,
41 static char pwd_getpwuid__doc__
[] = "\
42 getpwuid(uid) -> entry\n\
43 Return the password database entry for the given numeric user ID.\n\
44 See pwd.__doc__ for more on password database entries.";
47 pwd_getpwuid(PyObject
*self
, PyObject
*args
)
51 if (!PyArg_Parse(args
, "i", &uid
))
53 if ((p
= getpwuid(uid
)) == NULL
) {
54 PyErr_SetString(PyExc_KeyError
, "getpwuid(): uid not found");
60 static char pwd_getpwnam__doc__
[] = "\
61 getpwnam(name) -> entry\n\
62 Return the password database entry for the given user name.\n\
63 See pwd.__doc__ for more on password database entries.";
66 pwd_getpwnam(PyObject
*self
, PyObject
*args
)
70 if (!PyArg_Parse(args
, "s", &name
))
72 if ((p
= getpwnam(name
)) == NULL
) {
73 PyErr_SetString(PyExc_KeyError
, "getpwnam(): name not found");
80 static char pwd_getpwall__doc__
[] = "\
81 getpwall() -> list_of_entries\n\
82 Return a list of all available password database entries, \
83 in arbitrary order.\n\
84 See pwd.__doc__ for more on password database entries.";
87 pwd_getpwall(PyObject
*self
, PyObject
*args
)
91 if (!PyArg_NoArgs(args
))
93 if ((d
= PyList_New(0)) == NULL
)
96 while ((p
= getpwent()) != NULL
) {
97 PyObject
*v
= mkpwent(p
);
98 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
110 static PyMethodDef pwd_methods
[] = {
111 {"getpwuid", pwd_getpwuid
, METH_OLDARGS
, pwd_getpwuid__doc__
},
112 {"getpwnam", pwd_getpwnam
, METH_OLDARGS
, pwd_getpwnam__doc__
},
114 {"getpwall", pwd_getpwall
, METH_OLDARGS
, pwd_getpwall__doc__
},
116 {NULL
, NULL
} /* sentinel */
122 Py_InitModule4("pwd", pwd_methods
, pwd__doc__
,
123 (PyObject
*)NULL
, PYTHON_API_VERSION
);