Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 201 | 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 | 3 |
| 4684 | 4 #include "Python.h" |
| 201 | 5 |
| 6 #include <sys/types.h> | |
| 7 #include <pwd.h> | |
| 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 | 20 static PyObject * |
|
13505
0edeb4efe6db
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
21 mkpwent(struct passwd *p) |
| 201 | 22 { |
| 4786 | 23 return Py_BuildValue( |
| 24 "(ssllsss)", | |
| 25 p->pw_name, | |
| 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 | 30 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */ |
| 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 | 33 (long)p->pw_uid, |
| 34 (long)p->pw_gid, | |
|
2297
9717f6a8f5fa
patches for NeXT weirdness
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
35 #endif |
| 4786 | 36 p->pw_gecos, |
| 37 p->pw_dir, | |
| 38 p->pw_shell); | |
| 201 | 39 } |
| 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 | 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 | 48 { |
| 49 int uid; | |
| 50 struct passwd *p; | |
| 4684 | 51 if (!PyArg_Parse(args, "i", &uid)) |
| 201 | 52 return NULL; |
| 53 if ((p = getpwuid(uid)) == NULL) { | |
| 4684 | 54 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); |
| 201 | 55 return NULL; |
| 56 } | |
| 57 return mkpwent(p); | |
| 58 } | |
| 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 | 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 | 67 { |
|
643
02d3f161b7f5
Use getargs() function.
Guido van Rossum <guido@python.org>
parents:
513
diff
changeset
|
68 char *name; |
| 201 | 69 struct passwd *p; |
| 4684 | 70 if (!PyArg_Parse(args, "s", &name)) |
| 201 | 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 | 73 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); |
| 201 | 74 return NULL; |
| 75 } | |
| 76 return mkpwent(p); | |
| 77 } | |
| 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 | 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 | 88 { |
| 4684 | 89 PyObject *d; |
| 201 | 90 struct passwd *p; |
| 4684 | 91 if (!PyArg_NoArgs(args)) |
| 201 | 92 return NULL; |
| 4684 | 93 if ((d = PyList_New(0)) == NULL) |
| 201 | 94 return NULL; |
| 95 setpwent(); | |
| 96 while ((p = getpwent()) != NULL) { | |
| 4684 | 97 PyObject *v = mkpwent(p); |
| 98 if (v == NULL || PyList_Append(d, v) != 0) { | |
| 99 Py_XDECREF(v); | |
| 100 Py_DECREF(d); | |
| 201 | 101 return NULL; |
| 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 | 104 } |
| 105 return d; | |
| 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 | 108 |
| 4684 | 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 | 115 {NULL, NULL} /* sentinel */ |
| 116 }; | |
| 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 | 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 | 123 } |
