Bump version to 0.9.1.
[python/dscho.git] / Modules / pwdmodule.c
blob1efe81ade87f5ecf19aeda513ab38b1dc0b17535
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* UNIX password file access module */
13 #include "Python.h"
15 #include <sys/types.h>
16 #include <pwd.h>
18 static char pwd__doc__ [] = "\
19 This module provides access to the Unix password database.\n\
20 It is available on all Unix versions.\n\
21 \n\
22 Password database entries are reported as 7-tuples containing the following\n\
23 items from the password database (see `<pwd.h>'), in order:\n\
24 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
25 The uid and gid items are integers, all others are strings. An\n\
26 exception is raised if the entry asked for cannot be found.";
29 static PyObject *
30 mkpwent(struct passwd *p)
32 #ifdef __BEOS__
33 /* For faking the GECOS field. - [cjh] */
34 char *be_user = NULL;
36 be_user = getenv( "USER" );
37 #endif
39 return Py_BuildValue(
40 "(ssllsss)",
41 p->pw_name,
42 p->pw_passwd,
43 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
44 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
45 for later versions you may have to remove this */
46 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
47 (long)p->pw_short_pad2,
48 #else
49 (long)p->pw_uid,
50 (long)p->pw_gid,
51 #endif
52 #ifdef __BEOS__
53 /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
54 be_user ? be_user : "baron",
55 #else
56 p->pw_gecos,
57 #endif
58 p->pw_dir,
59 p->pw_shell);
62 static char pwd_getpwuid__doc__[] = "\
63 getpwuid(uid) -> entry\n\
64 Return the password database entry for the given numeric user ID.\n\
65 See pwd.__doc__ for more on password database entries.";
67 static PyObject *
68 pwd_getpwuid(PyObject *self, PyObject *args)
70 int uid;
71 struct passwd *p;
72 if (!PyArg_Parse(args, "i", &uid))
73 return NULL;
74 if ((p = getpwuid(uid)) == NULL) {
75 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
76 return NULL;
78 return mkpwent(p);
81 static char pwd_getpwnam__doc__[] = "\
82 getpwnam(name) -> entry\n\
83 Return the password database entry for the given user name.\n\
84 See pwd.__doc__ for more on password database entries.";
86 static PyObject *
87 pwd_getpwnam(PyObject *self, PyObject *args)
89 char *name;
90 struct passwd *p;
91 if (!PyArg_Parse(args, "s", &name))
92 return NULL;
93 if ((p = getpwnam(name)) == NULL) {
94 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
95 return NULL;
97 return mkpwent(p);
100 #ifdef HAVE_GETPWENT
101 static char pwd_getpwall__doc__[] = "\
102 getpwall() -> list_of_entries\n\
103 Return a list of all available password database entries, \
104 in arbitrary order.\n\
105 See pwd.__doc__ for more on password database entries.";
107 static PyObject *
108 pwd_getpwall(PyObject *self, PyObject *args)
110 PyObject *d;
111 struct passwd *p;
112 if (!PyArg_NoArgs(args))
113 return NULL;
114 if ((d = PyList_New(0)) == NULL)
115 return NULL;
116 setpwent();
117 while ((p = getpwent()) != NULL) {
118 PyObject *v = mkpwent(p);
119 if (v == NULL || PyList_Append(d, v) != 0) {
120 Py_XDECREF(v);
121 Py_DECREF(d);
122 return NULL;
124 Py_DECREF(v);
126 return d;
128 #endif
130 static PyMethodDef pwd_methods[] = {
131 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
132 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
133 #ifdef HAVE_GETPWENT
134 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
135 #endif
136 {NULL, NULL} /* sentinel */
139 DL_EXPORT(void)
140 initpwd(void)
142 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
143 (PyObject *)NULL, PYTHON_API_VERSION);