xref: /titanic_41/usr/src/cmd/getent/dogetgr.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1994, by Sun Microsystems, Inc.
26  */
27 
28 #include <stdio.h>
29 #include <grp.h>
30 #include <stdlib.h>
31 #include "getent.h"
32 
33 
34 static int
35 putgrent(const struct group *grp, FILE *fp)
36 {
37 	char **mem;
38 	int rc = 0;
39 
40 	if (grp == NULL) {
41 		return (1);
42 	}
43 
44 	if (fprintf(fp, "%s:%s:%d:",
45 		    grp->gr_name != NULL ? grp->gr_name : "",
46 		    grp->gr_passwd != NULL ? grp->gr_passwd : "",
47 		    grp->gr_gid) == EOF)
48 		rc = 1;
49 
50 	mem = grp ->gr_mem;
51 
52 	if (mem != NULL) {
53 		if (*mem != NULL)
54 			if (fputs(*mem++, fp) == EOF)
55 				rc = 1;
56 
57 		while (*mem != NULL)
58 			if (fprintf(fp, ",%s", *mem++) == EOF)
59 				rc = 1;
60 	}
61 	if (putc('\n', fp) == EOF)
62 		rc = 1;
63 	return (rc);
64 }
65 
66 int
67 dogetgr(const char **list)
68 {
69 	struct group *grp;
70 	int rc = EXC_SUCCESS;
71 	char *ptr;
72 	gid_t gid;
73 
74 	if (list == NULL || *list == NULL) {
75 		while ((grp = getgrent()) != NULL)
76 			(void) putgrent(grp, stdout);
77 	} else {
78 		for (; *list != NULL; list++) {
79 			gid = strtol(*list, &ptr, 10);
80 			if (ptr == *list)
81 				grp = getgrnam(*list);
82 			else
83 				grp = getgrgid(gid);
84 			if (grp == NULL)
85 				rc = EXC_NAME_NOT_FOUND;
86 			else
87 				(void) putgrent(grp, stdout);
88 		}
89 	}
90 
91 	return (rc);
92 }
93