1994-06-23 08:15:44 -03:00
|
|
|
/***********************************************************
|
1995-01-04 15:10:35 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1994-06-23 08:15:44 -03:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1994-06-23 08:15:44 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1994-06-23 08:15:44 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1994-06-23 08:15:44 -03:00
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1994-06-23 08:15:44 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* UNIX group file access module */
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
#include "Python.h"
|
1994-06-23 08:15:44 -03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
static PyObject *mkgrent(p)
|
1994-06-23 08:15:44 -03:00
|
|
|
struct group *p;
|
|
|
|
{
|
1996-12-18 15:37:32 -04:00
|
|
|
PyObject *v, *w;
|
1994-06-23 08:15:44 -03:00
|
|
|
char **member;
|
1996-12-18 15:37:32 -04:00
|
|
|
if ((w = PyList_New(0)) == NULL) {
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (member = p->gr_mem; *member != NULL; member++) {
|
1996-12-18 15:37:32 -04:00
|
|
|
PyObject *x = PyString_FromString(*member);
|
|
|
|
if (x == NULL || PyList_Append(w, x) != 0) {
|
|
|
|
Py_XDECREF(x);
|
|
|
|
Py_DECREF(w);
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1996-12-18 15:37:32 -04:00
|
|
|
v = Py_BuildValue("(sslO)",
|
1994-06-23 08:15:44 -03:00
|
|
|
p->gr_name,
|
|
|
|
p->gr_passwd,
|
1995-02-07 11:38:56 -04:00
|
|
|
#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
|
|
|
|
/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
|
|
|
|
for later versions you may have to remove this */
|
|
|
|
(long)p->gr_short_pad, /* ugh-NeXT broke the padding */
|
|
|
|
#else
|
1994-06-23 08:15:44 -03:00
|
|
|
(long)p->gr_gid,
|
1995-02-07 11:38:56 -04:00
|
|
|
#endif
|
1994-06-23 08:15:44 -03:00
|
|
|
w);
|
1996-12-18 15:37:32 -04:00
|
|
|
Py_DECREF(w);
|
1994-06-23 08:15:44 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
static PyObject *grp_getgrgid(self, args)
|
|
|
|
PyObject *self, *args;
|
1994-06-23 08:15:44 -03:00
|
|
|
{
|
|
|
|
int gid;
|
|
|
|
struct group *p;
|
1996-12-18 16:07:39 -04:00
|
|
|
if (!PyArg_Parse((args),"i",(&gid)))
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
if ((p = getgrgid(gid)) == NULL) {
|
1996-12-18 15:37:32 -04:00
|
|
|
PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mkgrent(p);
|
|
|
|
}
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
static PyObject *grp_getgrnam(self, args)
|
|
|
|
PyObject *self, *args;
|
1994-06-23 08:15:44 -03:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
struct group *p;
|
1996-12-18 16:07:39 -04:00
|
|
|
if (!PyArg_Parse((args),"s",(&name)))
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
if ((p = getgrnam(name)) == NULL) {
|
1996-12-18 15:37:32 -04:00
|
|
|
PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mkgrent(p);
|
|
|
|
}
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
static PyObject *grp_getgrall(self, args)
|
|
|
|
PyObject *self, *args;
|
1994-06-23 08:15:44 -03:00
|
|
|
{
|
1996-12-18 15:37:32 -04:00
|
|
|
PyObject *d;
|
1994-06-23 08:15:44 -03:00
|
|
|
struct group *p;
|
1996-12-18 15:37:32 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
1996-12-18 15:37:32 -04:00
|
|
|
if ((d = PyList_New(0)) == NULL)
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
setgrent();
|
|
|
|
while ((p = getgrent()) != NULL) {
|
1996-12-18 15:37:32 -04:00
|
|
|
PyObject *v = mkgrent(p);
|
|
|
|
if (v == NULL || PyList_Append(d, v) != 0) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_DECREF(d);
|
1994-06-23 08:15:44 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1996-12-18 15:37:32 -04:00
|
|
|
static PyMethodDef grp_methods[] = {
|
1994-06-23 08:15:44 -03:00
|
|
|
{"getgrgid", grp_getgrgid},
|
|
|
|
{"getgrnam", grp_getgrnam},
|
|
|
|
{"getgrall", grp_getgrall},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
initgrp()
|
|
|
|
{
|
1996-12-18 15:37:32 -04:00
|
|
|
Py_InitModule("grp", grp_methods);
|
1994-06-23 08:15:44 -03:00
|
|
|
}
|