More installation info. Bump alpha version.
[python/dscho.git] / Modules / pwdmodule.c
blob7e3c3ae170ae88e212aab1d1a0f7e900cd9d8fb3
2 /* UNIX password file access module */
4 #include "Python.h"
5 #include "structseq.h"
7 #include <sys/types.h>
8 #include <pwd.h>
10 static PyStructSequence_Field struct_pwd_type_fields[] = {
11 {"pw_name", "user name"},
12 {"pw_passwd", "password"},
13 {"pw_uid", "user id"},
14 {"pw_gid", "group id"},
15 {"pw_gecos", "real name"},
16 {"pw_dir", "home directory"},
17 {"pw_shell", "shell program"},
18 {0}
21 PyDoc_STRVAR(struct_passwd__doc__,
22 "pwd.struct_passwd: Results from getpw*() routines.\n\n\
23 This object may be accessed either as a tuple of\n\
24 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
25 or via the object attributes as named in the above tuple.");
27 static PyStructSequence_Desc struct_pwd_type_desc = {
28 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
34 PyDoc_STRVAR(pwd__doc__,
35 "This module provides access to the Unix password database.\n\
36 It is available on all Unix versions.\n\
37 \n\
38 Password database entries are reported as 7-tuples containing the following\n\
39 items from the password database (see `<pwd.h>'), in order:\n\
40 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
41 The uid and gid items are integers, all others are strings. An\n\
42 exception is raised if the entry asked for cannot be found.");
45 static PyTypeObject StructPwdType;
47 static void
48 sets(PyObject *v, int i, char* val)
50 if (val)
51 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
52 else {
53 PyStructSequence_SET_ITEM(v, i, Py_None);
54 Py_INCREF(Py_None);
58 static PyObject *
59 mkpwent(struct passwd *p)
61 int setIndex = 0;
62 PyObject *v = PyStructSequence_New(&StructPwdType);
63 if (v == NULL)
64 return NULL;
66 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
67 #define SETS(i,val) sets(v, i, val)
69 SETS(setIndex++, p->pw_name);
70 #ifdef __VMS
71 SETS(setIndex++, "");
72 #else
73 SETS(setIndex++, p->pw_passwd);
74 #endif
75 SETI(setIndex++, p->pw_uid);
76 SETI(setIndex++, p->pw_gid);
77 #ifdef __VMS
78 SETS(setIndex++, "");
79 #else
80 SETS(setIndex++, p->pw_gecos);
81 #endif
82 SETS(setIndex++, p->pw_dir);
83 SETS(setIndex++, p->pw_shell);
85 #undef SETS
86 #undef SETI
88 if (PyErr_Occurred()) {
89 Py_XDECREF(v);
90 return NULL;
93 return v;
96 PyDoc_STRVAR(pwd_getpwuid__doc__,
97 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
98 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
99 Return the password database entry for the given numeric user ID.\n\
100 See pwd.__doc__ for more on password database entries.");
102 static PyObject *
103 pwd_getpwuid(PyObject *self, PyObject *args)
105 int uid;
106 struct passwd *p;
107 if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
108 return NULL;
109 if ((p = getpwuid(uid)) == NULL) {
110 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
111 return NULL;
113 return mkpwent(p);
116 PyDoc_STRVAR(pwd_getpwnam__doc__,
117 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
118 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
119 Return the password database entry for the given user name.\n\
120 See pwd.__doc__ for more on password database entries.");
122 static PyObject *
123 pwd_getpwnam(PyObject *self, PyObject *args)
125 char *name;
126 struct passwd *p;
127 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
128 return NULL;
129 if ((p = getpwnam(name)) == NULL) {
130 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
131 return NULL;
133 return mkpwent(p);
136 #ifdef HAVE_GETPWENT
137 PyDoc_STRVAR(pwd_getpwall__doc__,
138 "getpwall() -> list_of_entries\n\
139 Return a list of all available password database entries, \
140 in arbitrary order.\n\
141 See pwd.__doc__ for more on password database entries.");
143 static PyObject *
144 pwd_getpwall(PyObject *self)
146 PyObject *d;
147 struct passwd *p;
148 if ((d = PyList_New(0)) == NULL)
149 return NULL;
150 #if defined(PYOS_OS2) && defined(PYCC_GCC)
151 if ((p = getpwuid(0)) != NULL) {
152 #else
153 setpwent();
154 while ((p = getpwent()) != NULL) {
155 #endif
156 PyObject *v = mkpwent(p);
157 if (v == NULL || PyList_Append(d, v) != 0) {
158 Py_XDECREF(v);
159 Py_DECREF(d);
160 return NULL;
162 Py_DECREF(v);
164 endpwent();
165 return d;
167 #endif
169 static PyMethodDef pwd_methods[] = {
170 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
171 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
172 #ifdef HAVE_GETPWENT
173 {"getpwall", (PyCFunction)pwd_getpwall,
174 METH_NOARGS, pwd_getpwall__doc__},
175 #endif
176 {NULL, NULL} /* sentinel */
179 PyMODINIT_FUNC
180 initpwd(void)
182 PyObject *m;
183 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
185 PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
186 Py_INCREF((PyObject *) &StructPwdType);
187 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);