annotate Modules/pwdmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 6f6f965c5e2d
children 971ff6b58edb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1351
diff changeset
2 /* UNIX password file access module */
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include <sys/types.h>
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include <pwd.h>
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
7530
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
9 static char pwd__doc__ [] = "\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
10 This module provides access to the Unix password database.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
11 It is available on all Unix versions.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
12 \n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
13 Password database entries are reported as 7-tuples containing the following\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
14 items from the password database (see `<pwd.h>'), in order:\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
15 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
16 The uid and gid items are integers, all others are strings. An\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
17 exception is raised if the entry asked for cannot be found.";
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
18
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
19
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
20 static PyObject *
13505
0edeb4efe6db ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
21 mkpwent(struct passwd *p)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 {
4786
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
23 return Py_BuildValue(
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
24 "(ssllsss)",
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
25 p->pw_name,
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
26 p->pw_passwd,
2297
9717f6a8f5fa patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
27 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
9717f6a8f5fa patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
28 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
9717f6a8f5fa patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
29 for later versions you may have to remove this */
4786
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
30 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
31 (long)p->pw_short_pad2,
2297
9717f6a8f5fa patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
32 #else
4786
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
33 (long)p->pw_uid,
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
34 (long)p->pw_gid,
2297
9717f6a8f5fa patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
35 #endif
4786
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
36 p->pw_gecos,
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
37 p->pw_dir,
0948f39ee960 Minor formatting changes.
Barry Warsaw <barry@python.org>
parents: 4684
diff changeset
38 p->pw_shell);
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
7530
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
41 static char pwd_getpwuid__doc__[] = "\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
42 getpwuid(uid) -> entry\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
43 Return the password database entry for the given numeric user ID.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
44 See pwd.__doc__ for more on password database entries.";
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
45
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
46 static PyObject *
13505
0edeb4efe6db ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
47 pwd_getpwuid(PyObject *self, PyObject *args)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 {
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 int uid;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 struct passwd *p;
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
51 if (!PyArg_Parse(args, "i", &uid))
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 return NULL;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if ((p = getpwuid(uid)) == NULL) {
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
54 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return NULL;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return mkpwent(p);
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
7530
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
60 static char pwd_getpwnam__doc__[] = "\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
61 getpwnam(name) -> entry\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
62 Return the password database entry for the given user name.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
63 See pwd.__doc__ for more on password database entries.";
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
64
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
65 static PyObject *
13505
0edeb4efe6db ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
66 pwd_getpwnam(PyObject *self, PyObject *args)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 {
643
02d3f161b7f5 Use getargs() function.
Guido van Rossum <guido@python.org>
parents: 513
diff changeset
68 char *name;
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 struct passwd *p;
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
70 if (!PyArg_Parse(args, "s", &name))
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 return NULL;
643
02d3f161b7f5 Use getargs() function.
Guido van Rossum <guido@python.org>
parents: 513
diff changeset
72 if ((p = getpwnam(name)) == NULL) {
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
73 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 return NULL;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return mkpwent(p);
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
6142
144db635b49d Added configuration tests for presence of alarm(), pause(), and getpwent()
Guido van Rossum <guido@python.org>
parents: 4917
diff changeset
79 #ifdef HAVE_GETPWENT
7530
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
80 static char pwd_getpwall__doc__[] = "\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
81 getpwall() -> list_of_entries\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
82 Return a list of all available password database entries, \
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
83 in arbitrary order.\n\
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
84 See pwd.__doc__ for more on password database entries.";
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
85
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
86 static PyObject *
13505
0edeb4efe6db ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
87 pwd_getpwall(PyObject *self, PyObject *args)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 {
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
89 PyObject *d;
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 struct passwd *p;
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
91 if (!PyArg_NoArgs(args))
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return NULL;
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
93 if ((d = PyList_New(0)) == NULL)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return NULL;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 setpwent();
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 while ((p = getpwent()) != NULL) {
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
97 PyObject *v = mkpwent(p);
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
98 if (v == NULL || PyList_Append(d, v) != 0) {
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
99 Py_XDECREF(v);
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
100 Py_DECREF(d);
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return NULL;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4786
diff changeset
103 Py_DECREF(v);
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 return d;
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
6142
144db635b49d Added configuration tests for presence of alarm(), pause(), and getpwent()
Guido van Rossum <guido@python.org>
parents: 4917
diff changeset
107 #endif
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
4684
d800841b57a8 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
109 static PyMethodDef pwd_methods[] = {
13820
8bd82cf361aa Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
110 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
8bd82cf361aa Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
111 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
6142
144db635b49d Added configuration tests for presence of alarm(), pause(), and getpwent()
Guido van Rossum <guido@python.org>
parents: 4917
diff changeset
112 #ifdef HAVE_GETPWENT
13820
8bd82cf361aa Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13651
diff changeset
113 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
6142
144db635b49d Added configuration tests for presence of alarm(), pause(), and getpwent()
Guido van Rossum <guido@python.org>
parents: 4917
diff changeset
114 #endif
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 {NULL, NULL} /* sentinel */
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 };
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
118 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13505
diff changeset
119 initpwd(void)
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 {
7530
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
121 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
891105dac9b4 Doc strings added by Mitch Chapman.
Guido van Rossum <guido@python.org>
parents: 6142
diff changeset
122 (PyObject *)NULL, PYTHON_API_VERSION);
201
310f8c924073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 }