blob: 0199b6b033a448c5a51eb3eb2fcdd442f23071c3 [file] [log] [blame]
Guido van Rossum20882d51994-06-23 11:15:44 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum20882d51994-06-23 11:15:44 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum20882d51994-06-23 11:15:44 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum20882d51994-06-23 11:15:44 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum20882d51994-06-23 11:15:44 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum20882d51994-06-23 11:15:44 +000029
30******************************************************************/
31
32/* UNIX group file access module */
33
Roger E. Masseb2b44e51996-12-18 19:37:32 +000034#include "Python.h"
Guido van Rossum20882d51994-06-23 11:15:44 +000035
36#include <sys/types.h>
37#include <grp.h>
38
Roger E. Masseb2b44e51996-12-18 19:37:32 +000039static PyObject *mkgrent(p)
Guido van Rossum20882d51994-06-23 11:15:44 +000040 struct group *p;
41{
Roger E. Masseb2b44e51996-12-18 19:37:32 +000042 PyObject *v, *w;
Guido van Rossum20882d51994-06-23 11:15:44 +000043 char **member;
Roger E. Masseb2b44e51996-12-18 19:37:32 +000044 if ((w = PyList_New(0)) == NULL) {
Guido van Rossum20882d51994-06-23 11:15:44 +000045 return NULL;
46 }
47 for (member = p->gr_mem; *member != NULL; member++) {
Roger E. Masseb2b44e51996-12-18 19:37:32 +000048 PyObject *x = PyString_FromString(*member);
49 if (x == NULL || PyList_Append(w, x) != 0) {
50 Py_XDECREF(x);
51 Py_DECREF(w);
Guido van Rossum20882d51994-06-23 11:15:44 +000052 return NULL;
53 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +000054 Py_DECREF(x);
Guido van Rossum20882d51994-06-23 11:15:44 +000055 }
Roger E. Masseb2b44e51996-12-18 19:37:32 +000056 v = Py_BuildValue("(sslO)",
Guido van Rossum20882d51994-06-23 11:15:44 +000057 p->gr_name,
58 p->gr_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000059#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
60/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
61 for later versions you may have to remove this */
62 (long)p->gr_short_pad, /* ugh-NeXT broke the padding */
63#else
Guido van Rossum20882d51994-06-23 11:15:44 +000064 (long)p->gr_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000065#endif
Guido van Rossum20882d51994-06-23 11:15:44 +000066 w);
Roger E. Masseb2b44e51996-12-18 19:37:32 +000067 Py_DECREF(w);
Guido van Rossum20882d51994-06-23 11:15:44 +000068 return v;
69}
70
Roger E. Masseb2b44e51996-12-18 19:37:32 +000071static PyObject *grp_getgrgid(self, args)
72 PyObject *self, *args;
Guido van Rossum20882d51994-06-23 11:15:44 +000073{
74 int gid;
75 struct group *p;
Roger E. Masseec0b4af1996-12-18 20:07:39 +000076 if (!PyArg_Parse((args),"i",(&gid)))
Guido van Rossum20882d51994-06-23 11:15:44 +000077 return NULL;
78 if ((p = getgrgid(gid)) == NULL) {
Roger E. Masseb2b44e51996-12-18 19:37:32 +000079 PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
Guido van Rossum20882d51994-06-23 11:15:44 +000080 return NULL;
81 }
82 return mkgrent(p);
83}
84
Roger E. Masseb2b44e51996-12-18 19:37:32 +000085static PyObject *grp_getgrnam(self, args)
86 PyObject *self, *args;
Guido van Rossum20882d51994-06-23 11:15:44 +000087{
88 char *name;
89 struct group *p;
Roger E. Masseec0b4af1996-12-18 20:07:39 +000090 if (!PyArg_Parse((args),"s",(&name)))
Guido van Rossum20882d51994-06-23 11:15:44 +000091 return NULL;
92 if ((p = getgrnam(name)) == NULL) {
Roger E. Masseb2b44e51996-12-18 19:37:32 +000093 PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
Guido van Rossum20882d51994-06-23 11:15:44 +000094 return NULL;
95 }
96 return mkgrent(p);
97}
98
Roger E. Masseb2b44e51996-12-18 19:37:32 +000099static PyObject *grp_getgrall(self, args)
100 PyObject *self, *args;
Guido van Rossum20882d51994-06-23 11:15:44 +0000101{
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000102 PyObject *d;
Guido van Rossum20882d51994-06-23 11:15:44 +0000103 struct group *p;
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000104 if (!PyArg_NoArgs(args))
Guido van Rossum20882d51994-06-23 11:15:44 +0000105 return NULL;
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000106 if ((d = PyList_New(0)) == NULL)
Guido van Rossum20882d51994-06-23 11:15:44 +0000107 return NULL;
108 setgrent();
109 while ((p = getgrent()) != NULL) {
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000110 PyObject *v = mkgrent(p);
111 if (v == NULL || PyList_Append(d, v) != 0) {
112 Py_XDECREF(v);
113 Py_DECREF(d);
Guido van Rossum20882d51994-06-23 11:15:44 +0000114 return NULL;
115 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000116 Py_DECREF(v);
Guido van Rossum20882d51994-06-23 11:15:44 +0000117 }
118 return d;
119}
120
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000121static PyMethodDef grp_methods[] = {
Guido van Rossum20882d51994-06-23 11:15:44 +0000122 {"getgrgid", grp_getgrgid},
123 {"getgrnam", grp_getgrnam},
124 {"getgrall", grp_getgrall},
125 {NULL, NULL} /* sentinel */
126};
127
128void
129initgrp()
130{
Roger E. Masseb2b44e51996-12-18 19:37:32 +0000131 Py_InitModule("grp", grp_methods);
Guido van Rossum20882d51994-06-23 11:15:44 +0000132}