1994-06-23 08:15:44 -03:00
|
|
|
/***********************************************************
|
2000-06-30 20:50:40 -03:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
1994-06-23 08:15:44 -03:00
|
|
|
|
2000-06-30 20:50:40 -03:00
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
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;
|
|
|
|
}
|
1997-01-09 18:22:05 -04:00
|
|
|
Py_DECREF(x);
|
1994-06-23 08:15:44 -03:00
|
|
|
}
|
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;
|
|
|
|
}
|
1997-01-09 18:22:05 -04:00
|
|
|
Py_DECREF(v);
|
1994-06-23 08:15:44 -03:00
|
|
|
}
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
1998-12-04 14:50:17 -04:00
|
|
|
DL_EXPORT(void)
|
1994-06-23 08:15:44 -03:00
|
|
|
initgrp()
|
|
|
|
{
|
1996-12-18 15:37:32 -04:00
|
|
|
Py_InitModule("grp", grp_methods);
|
1994-06-23 08:15:44 -03:00
|
|
|
}
|