Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* UNIX group file access module */ |
| 33 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <grp.h> |
| 38 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 39 | static PyObject *mkgrent(p) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 40 | struct group *p; |
| 41 | { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 42 | PyObject *v, *w; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 43 | char **member; |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 44 | if ((w = PyList_New(0)) == NULL) { |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 45 | return NULL; |
| 46 | } |
| 47 | for (member = p->gr_mem; *member != NULL; member++) { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 48 | PyObject *x = PyString_FromString(*member); |
| 49 | if (x == NULL || PyList_Append(w, x) != 0) { |
| 50 | Py_XDECREF(x); |
| 51 | Py_DECREF(w); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 52 | return NULL; |
| 53 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 54 | Py_DECREF(x); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 55 | } |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 56 | v = Py_BuildValue("(sslO)", |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 57 | p->gr_name, |
| 58 | p->gr_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 59 | #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 Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 64 | (long)p->gr_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 65 | #endif |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 66 | w); |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 67 | Py_DECREF(w); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 68 | return v; |
| 69 | } |
| 70 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 71 | static PyObject *grp_getgrgid(self, args) |
| 72 | PyObject *self, *args; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 73 | { |
| 74 | int gid; |
| 75 | struct group *p; |
Roger E. Masse | ec0b4af | 1996-12-18 20:07:39 +0000 | [diff] [blame] | 76 | if (!PyArg_Parse((args),"i",(&gid))) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 77 | return NULL; |
| 78 | if ((p = getgrgid(gid)) == NULL) { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 79 | PyErr_SetString(PyExc_KeyError, "getgrgid(): gid not found"); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | return mkgrent(p); |
| 83 | } |
| 84 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 85 | static PyObject *grp_getgrnam(self, args) |
| 86 | PyObject *self, *args; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 87 | { |
| 88 | char *name; |
| 89 | struct group *p; |
Roger E. Masse | ec0b4af | 1996-12-18 20:07:39 +0000 | [diff] [blame] | 90 | if (!PyArg_Parse((args),"s",(&name))) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | if ((p = getgrnam(name)) == NULL) { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 93 | PyErr_SetString(PyExc_KeyError, "getgrnam(): name not found"); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
| 96 | return mkgrent(p); |
| 97 | } |
| 98 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 99 | static PyObject *grp_getgrall(self, args) |
| 100 | PyObject *self, *args; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 101 | { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 102 | PyObject *d; |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 103 | struct group *p; |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 104 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 105 | return NULL; |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 106 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | setgrent(); |
| 109 | while ((p = getgrent()) != NULL) { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 110 | PyObject *v = mkgrent(p); |
| 111 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 112 | Py_XDECREF(v); |
| 113 | Py_DECREF(d); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 116 | Py_DECREF(v); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 117 | } |
| 118 | return d; |
| 119 | } |
| 120 | |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 121 | static PyMethodDef grp_methods[] = { |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 122 | {"getgrgid", grp_getgrgid}, |
| 123 | {"getgrnam", grp_getgrnam}, |
| 124 | {"getgrall", grp_getgrall}, |
| 125 | {NULL, NULL} /* sentinel */ |
| 126 | }; |
| 127 | |
| 128 | void |
| 129 | initgrp() |
| 130 | { |
Roger E. Masse | b2b44e5 | 1996-12-18 19:37:32 +0000 | [diff] [blame] | 131 | Py_InitModule("grp", grp_methods); |
Guido van Rossum | 20882d5 | 1994-06-23 11:15:44 +0000 | [diff] [blame] | 132 | } |