blob: cff9d83f35a5ab482b3d05f19c1fa032fbfef180 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* UNIX group file access module */
26
27#include "allobjects.h"
28#include "modsupport.h"
29
30#include <sys/types.h>
31#include <grp.h>
32
33static object *mkgrent(p)
34 struct group *p;
35{
36 object *v, *w;
37 char **member;
38 if ((w = newlistobject(0)) == NULL) {
39 return NULL;
40 }
41 for (member = p->gr_mem; *member != NULL; member++) {
42 object *x = newstringobject(*member);
43 if (x == NULL || addlistitem(w, x) != 0) {
44 XDECREF(x);
45 DECREF(w);
46 return NULL;
47 }
48 }
49 v = mkvalue("(sslO)",
50 p->gr_name,
51 p->gr_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000052#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
53/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
54 for later versions you may have to remove this */
55 (long)p->gr_short_pad, /* ugh-NeXT broke the padding */
56#else
Guido van Rossum20882d51994-06-23 11:15:44 +000057 (long)p->gr_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000058#endif
Guido van Rossum20882d51994-06-23 11:15:44 +000059 w);
60 DECREF(w);
61 return v;
62}
63
64static object *grp_getgrgid(self, args)
65 object *self, *args;
66{
67 int gid;
68 struct group *p;
69 if (!getintarg(args, &gid))
70 return NULL;
71 if ((p = getgrgid(gid)) == NULL) {
72 err_setstr(KeyError, "getgrgid(): gid not found");
73 return NULL;
74 }
75 return mkgrent(p);
76}
77
78static object *grp_getgrnam(self, args)
79 object *self, *args;
80{
81 char *name;
82 struct group *p;
83 if (!getstrarg(args, &name))
84 return NULL;
85 if ((p = getgrnam(name)) == NULL) {
86 err_setstr(KeyError, "getgrnam(): name not found");
87 return NULL;
88 }
89 return mkgrent(p);
90}
91
92static object *grp_getgrall(self, args)
93 object *self, *args;
94{
95 object *d;
96 struct group *p;
97 if (!getnoarg(args))
98 return NULL;
99 if ((d = newlistobject(0)) == NULL)
100 return NULL;
101 setgrent();
102 while ((p = getgrent()) != NULL) {
103 object *v = mkgrent(p);
104 if (v == NULL || addlistitem(d, v) != 0) {
105 XDECREF(v);
106 DECREF(d);
107 return NULL;
108 }
109 }
110 return d;
111}
112
113static struct methodlist grp_methods[] = {
114 {"getgrgid", grp_getgrgid},
115 {"getgrnam", grp_getgrnam},
116 {"getgrall", grp_getgrall},
117 {NULL, NULL} /* sentinel */
118};
119
120void
121initgrp()
122{
123 initmodule("grp", grp_methods);
124}