annotate Modules/grpmodule.c @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 79a2b6a12805
children 7cb403f8a865
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* UNIX group file access module */
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4774
9e48abbee6c5 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4503
diff changeset
4 #include "Python.h"
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
5 #include "structseq.h"
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include <sys/types.h>
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #include <grp.h>
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
10 static PyStructSequence_Field struct_group_type_fields[] = {
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
11 {"gr_name", "group name"},
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
12 {"gr_passwd", "password"},
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
13 {"gr_gid", "group id"},
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
14 {"gr_mem", "group memebers"},
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
15 {0}
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
16 };
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
17
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22099
diff changeset
18 PyDoc_STRVAR(struct_group__doc__,
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
19 "grp.struct_group: Results from getgr*() routines.\n\n\
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
20 This object may be accessed either as a tuple of\n\
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
21 (gr_name,gr_passwd,gr_gid,gr_mem)\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22099
diff changeset
22 or via the object attributes as named in the above tuple.\n");
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
23
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
24 static PyStructSequence_Desc struct_group_type_desc = {
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
25 "grp.struct_group",
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
26 struct_group__doc__,
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
27 struct_group_type_fields,
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
28 4,
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
29 };
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
30
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
31
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
32 static int initialized;
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
33 static PyTypeObject StructGrpType;
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
34
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
35 static PyObject *
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
36 mkgrent(struct group *p)
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 {
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
38 int setIndex = 0;
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
39 PyObject *v = PyStructSequence_New(&StructGrpType), *w;
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
40 char **member;
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
41
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
42 if (v == NULL)
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
43 return NULL;
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
44
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
45 if ((w = PyList_New(0)) == NULL) {
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
46 Py_DECREF(v);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
47 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
48 }
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
49 for (member = p->gr_mem; *member != NULL; member++) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
50 PyObject *x = PyString_FromString(*member);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
51 if (x == NULL || PyList_Append(w, x) != 0) {
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
52 Py_XDECREF(x);
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
53 Py_DECREF(w);
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
54 Py_DECREF(v);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
55 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
56 }
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
57 Py_DECREF(x);
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
58 }
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
59
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
60 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
61 SET(setIndex++, PyString_FromString(p->gr_name));
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25302
diff changeset
62 #ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25302
diff changeset
63 SET(setIndex++, Py_None);
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25302
diff changeset
64 Py_INCREF(Py_None);
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25302
diff changeset
65 #else
25302
eb28dcddd34d Deal with a NULL gr_passwd. Reported by Anders Qvist.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
66 if (p->gr_passwd)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
67 SET(setIndex++, PyString_FromString(p->gr_passwd));
25302
eb28dcddd34d Deal with a NULL gr_passwd. Reported by Anders Qvist.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
68 else {
eb28dcddd34d Deal with a NULL gr_passwd. Reported by Anders Qvist.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
69 SET(setIndex++, Py_None);
eb28dcddd34d Deal with a NULL gr_passwd. Reported by Anders Qvist.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
70 Py_INCREF(Py_None);
eb28dcddd34d Deal with a NULL gr_passwd. Reported by Anders Qvist.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
71 }
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25302
diff changeset
72 #endif
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
73 SET(setIndex++, PyInt_FromLong((long) p->gr_gid));
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
74 SET(setIndex++, w);
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
75 #undef SET
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
76
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
77 if (PyErr_Occurred()) {
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
78 Py_DECREF(v);
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
79 return NULL;
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
80 }
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
81
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
82 return v;
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
85 static PyObject *
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
86 grp_getgrgid(PyObject *self, PyObject *pyo_id)
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 {
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
88 PyObject *py_int_id;
35551
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31404
diff changeset
89 unsigned int gid;
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
90 struct group *p;
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
91
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
92 py_int_id = PyNumber_Int(pyo_id);
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
93 if (!py_int_id)
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
94 return NULL;
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
95 gid = PyInt_AS_LONG(py_int_id);
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
96 Py_DECREF(py_int_id);
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
97
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
98 if ((p = getgrgid(gid)) == NULL) {
31404
7407a246dbe0 grp_getgrgid(), grp_getgrnam(): Patch # 868499, improvement to the error
Barry Warsaw <barry@python.org>
parents: 28949
diff changeset
99 PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
100 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
101 }
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
102 return mkgrent(p);
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 }
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
105 static PyObject *
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
106 grp_getgrnam(PyObject *self, PyObject *pyo_name)
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 {
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
108 PyObject *py_str_name;
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
109 char *name;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
110 struct group *p;
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
111
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
112 py_str_name = PyObject_Str(pyo_name);
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
113 if (!py_str_name)
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
114 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
115 name = PyString_AS_STRING(py_str_name);
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
116
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
117 if ((p = getgrnam(name)) == NULL) {
31404
7407a246dbe0 grp_getgrgid(), grp_getgrnam(): Patch # 868499, improvement to the error
Barry Warsaw <barry@python.org>
parents: 28949
diff changeset
118 PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
119 Py_DECREF(py_str_name);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
120 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
121 }
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
122
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
123 Py_DECREF(py_str_name);
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
124 return mkgrent(p);
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 }
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
127 static PyObject *
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
128 grp_getgrall(PyObject *self, PyObject *ignore)
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 {
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
130 PyObject *d;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
131 struct group *p;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
132
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
133 if ((d = PyList_New(0)) == NULL)
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
134 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
135 setgrent();
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
136 while ((p = getgrent()) != NULL) {
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
137 PyObject *v = mkgrent(p);
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
138 if (v == NULL || PyList_Append(d, v) != 0) {
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
139 Py_XDECREF(v);
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
140 Py_DECREF(d);
54424
76f1be9d2818 Merged revisions 73016 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
141 endgrent();
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
142 return NULL;
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
143 }
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
144 Py_DECREF(v);
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
145 }
17025
971ff6b58edb Make sure we close the group and password databases when we are done with
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
146 endgrent();
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
147 return d;
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 }
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
4774
9e48abbee6c5 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4503
diff changeset
150 static PyMethodDef grp_methods[] = {
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
151 {"getgrgid", grp_getgrgid, METH_O,
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
152 "getgrgid(id) -> tuple\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
153 Return the group database entry for the given numeric group ID. If\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
154 id is not valid, raise KeyError."},
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
155 {"getgrnam", grp_getgrnam, METH_O,
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
156 "getgrnam(name) -> tuple\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
157 Return the group database entry for the given group name. If\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
158 name is not valid, raise KeyError."},
38329
ab0221811771 Move over to use of METH_O and METH_NOARGS.
Brett Cannon <bcannon@gmail.com>
parents: 37772
diff changeset
159 {"getgrall", grp_getgrall, METH_NOARGS,
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
160 "getgrall() -> list of tuples\n\
66900
79a2b6a12805 Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54424
diff changeset
161 Return a list of all available group entries, in arbitrary order.\n\
79a2b6a12805 Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54424
diff changeset
162 An entry whose name starts with '+' or '-' represents an instruction\n\
79a2b6a12805 Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54424
diff changeset
163 to use YP/NIS and may not be accessible via getgrnam or getgrgid."},
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
164 {NULL, NULL} /* sentinel */
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 };
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22099
diff changeset
167 PyDoc_STRVAR(grp__doc__,
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
168 "Access to the Unix group database.\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
169 \n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
170 Group entries are reported as 4-tuples containing the following fields\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
171 from the group database, in order:\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
172 \n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
173 name - name of the group\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
174 passwd - group password (encrypted); often empty\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
175 gid - numeric ID of the group\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
176 mem - list of members\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
177 \n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
178 The gid is an integer, name and password are strings. (Note that most\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
179 users are not explicitly listed as members of the groups they are in\n\
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
180 according to the password database. Check both databases to get\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22099
diff changeset
181 complete membership information.)");
13433
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
182
fc78e5a7812a ANSI-fication of the sources, convert to 4-space indents.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
183
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
184 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13433
diff changeset
185 initgrp(void)
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 {
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
187 PyObject *m, *d;
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
188 m = Py_InitModule3("grp", grp_methods, grp__doc__);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35551
diff changeset
189 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35551
diff changeset
190 return;
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
191 d = PyModule_GetDict(m);
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
192 if (!initialized)
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
193 PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
22099
d0acb3cb011d Patch #523268, #522027: return enhanced tuples.
Martin v. Löwis <martin@v.loewis.de>
parents: 21859
diff changeset
194 PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
195 initialized = 1;
1677
76371805d9f3 Split pwd in pwd and grp; added imgformat (by Jack)
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }