improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Modules / pwdmodule.c
bloba9253c2d365542b680f1e419a4cba57bb70ae0e2
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* UNIX password file access module */
27 #include "allobjects.h"
28 #include "modsupport.h"
30 #include <sys/types.h>
31 #include <pwd.h>
33 static object *mkpwent(p)
34 struct passwd *p;
36 return mkvalue("(ssllsss)",
37 p->pw_name,
38 p->pw_passwd,
39 (long)p->pw_uid,
40 (long)p->pw_gid,
41 p->pw_gecos,
42 p->pw_dir,
43 p->pw_shell);
46 static object *pwd_getpwuid(self, args)
47 object *self, *args;
49 int uid;
50 struct passwd *p;
51 if (!getintarg(args, &uid))
52 return NULL;
53 if ((p = getpwuid(uid)) == NULL) {
54 err_setstr(KeyError, "getpwuid(): uid not found");
55 return NULL;
57 return mkpwent(p);
60 static object *pwd_getpwnam(self, args)
61 object *self, *args;
63 char *name;
64 struct passwd *p;
65 if (!getstrarg(args, &name))
66 return NULL;
67 if ((p = getpwnam(name)) == NULL) {
68 err_setstr(KeyError, "getpwnam(): name not found");
69 return NULL;
71 return mkpwent(p);
74 static object *pwd_getpwall(self, args)
75 object *self, *args;
77 object *d;
78 struct passwd *p;
79 if (!getnoarg(args))
80 return NULL;
81 if ((d = newlistobject(0)) == NULL)
82 return NULL;
83 setpwent();
84 while ((p = getpwent()) != NULL) {
85 object *v = mkpwent(p);
86 if (v == NULL || addlistitem(d, v) != 0) {
87 XDECREF(v);
88 DECREF(d);
89 return NULL;
92 return d;
95 static struct methodlist pwd_methods[] = {
96 {"getpwuid", pwd_getpwuid},
97 {"getpwnam", pwd_getpwnam},
98 {"getpwall", pwd_getpwall},
99 {NULL, NULL} /* sentinel */
102 void
103 initpwd()
105 initmodule("pwd", pwd_methods);