cpython/Modules/grpmodule.c

112 lines
2.5 KiB
C
Raw Normal View History

/***********************************************************
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.
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.
******************************************************************/
/* UNIX group file access module */
1996-12-18 15:37:32 -04:00
#include "Python.h"
#include <sys/types.h>
#include <grp.h>
1996-12-18 15:37:32 -04:00
static PyObject *mkgrent(p)
struct group *p;
{
1996-12-18 15:37:32 -04:00
PyObject *v, *w;
char **member;
1996-12-18 15:37:32 -04:00
if ((w = PyList_New(0)) == NULL) {
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);
return NULL;
}
Py_DECREF(x);
}
1996-12-18 15:37:32 -04:00
v = Py_BuildValue("(sslO)",
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
(long)p->gr_gid,
1995-02-07 11:38:56 -04:00
#endif
w);
1996-12-18 15:37:32 -04:00
Py_DECREF(w);
return v;
}
1996-12-18 15:37:32 -04:00
static PyObject *grp_getgrgid(self, args)
PyObject *self, *args;
{
int gid;
struct group *p;
if (!PyArg_Parse((args),"i",(&gid)))
return NULL;
if ((p = getgrgid(gid)) == NULL) {
1996-12-18 15:37:32 -04:00
PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found");
return NULL;
}
return mkgrent(p);
}
1996-12-18 15:37:32 -04:00
static PyObject *grp_getgrnam(self, args)
PyObject *self, *args;
{
char *name;
struct group *p;
if (!PyArg_Parse((args),"s",(&name)))
return NULL;
if ((p = getgrnam(name)) == NULL) {
1996-12-18 15:37:32 -04:00
PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found");
return NULL;
}
return mkgrent(p);
}
1996-12-18 15:37:32 -04:00
static PyObject *grp_getgrall(self, args)
PyObject *self, *args;
{
1996-12-18 15:37:32 -04:00
PyObject *d;
struct group *p;
1996-12-18 15:37:32 -04:00
if (!PyArg_NoArgs(args))
return NULL;
1996-12-18 15:37:32 -04:00
if ((d = PyList_New(0)) == NULL)
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);
return NULL;
}
Py_DECREF(v);
}
return d;
}
1996-12-18 15:37:32 -04:00
static PyMethodDef grp_methods[] = {
{"getgrgid", grp_getgrgid},
{"getgrnam", grp_getgrnam},
{"getgrall", grp_getgrall},
{NULL, NULL} /* sentinel */
};
DL_EXPORT(void)
initgrp()
{
1996-12-18 15:37:32 -04:00
Py_InitModule("grp", grp_methods);
}