Updated for 2.1b2 distribution.
[python/dscho.git] / Modules / pwdmodule.c
blob4c1b7db87892cf57044b40de3e0fbbb0a70fdd88
2 /* UNIX password file access module */
4 #include "Python.h"
6 #include <sys/types.h>
7 #include <pwd.h>
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\
12 \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.";
20 static PyObject *
21 mkpwent(struct passwd *p)
23 return Py_BuildValue(
24 "(ssllsss)",
25 p->pw_name,
26 p->pw_passwd,
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,
32 #else
33 (long)p->pw_uid,
34 (long)p->pw_gid,
35 #endif
36 p->pw_gecos,
37 p->pw_dir,
38 p->pw_shell);
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.";
46 static PyObject *
47 pwd_getpwuid(PyObject *self, PyObject *args)
49 int uid;
50 struct passwd *p;
51 if (!PyArg_Parse(args, "i", &uid))
52 return NULL;
53 if ((p = getpwuid(uid)) == NULL) {
54 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
55 return NULL;
57 return mkpwent(p);
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.";
65 static PyObject *
66 pwd_getpwnam(PyObject *self, PyObject *args)
68 char *name;
69 struct passwd *p;
70 if (!PyArg_Parse(args, "s", &name))
71 return NULL;
72 if ((p = getpwnam(name)) == NULL) {
73 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
74 return NULL;
76 return mkpwent(p);
79 #ifdef HAVE_GETPWENT
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.";
86 static PyObject *
87 pwd_getpwall(PyObject *self, PyObject *args)
89 PyObject *d;
90 struct passwd *p;
91 if (!PyArg_NoArgs(args))
92 return NULL;
93 if ((d = PyList_New(0)) == NULL)
94 return NULL;
95 setpwent();
96 while ((p = getpwent()) != NULL) {
97 PyObject *v = mkpwent(p);
98 if (v == NULL || PyList_Append(d, v) != 0) {
99 Py_XDECREF(v);
100 Py_DECREF(d);
101 return NULL;
103 Py_DECREF(v);
105 endpwent();
106 return d;
108 #endif
110 static PyMethodDef pwd_methods[] = {
111 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
112 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
113 #ifdef HAVE_GETPWENT
114 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
115 #endif
116 {NULL, NULL} /* sentinel */
119 DL_EXPORT(void)
120 initpwd(void)
122 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
123 (PyObject *)NULL, PYTHON_API_VERSION);