xref: /titanic_54/usr/src/cmd/getent/dogetgr.c (revision 34a0f871d192b33b865455a8812a3d34c1866315)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*34a0f871Svp157776  * Common Development and Distribution License (the "License").
6*34a0f871Svp157776  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*34a0f871Svp157776  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*34a0f871Svp157776  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*34a0f871Svp157776 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*34a0f871Svp157776 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <grp.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*34a0f871Svp157776 #include <errno.h>
337c478bd9Sstevel@tonic-gate #include "getent.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static int
377c478bd9Sstevel@tonic-gate putgrent(const struct group *grp, FILE *fp)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	char **mem;
407c478bd9Sstevel@tonic-gate 	int rc = 0;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	if (grp == NULL) {
437c478bd9Sstevel@tonic-gate 		return (1);
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (fprintf(fp, "%s:%s:%d:",
477c478bd9Sstevel@tonic-gate 		    grp->gr_name != NULL ? grp->gr_name : "",
487c478bd9Sstevel@tonic-gate 		    grp->gr_passwd != NULL ? grp->gr_passwd : "",
497c478bd9Sstevel@tonic-gate 		    grp->gr_gid) == EOF)
507c478bd9Sstevel@tonic-gate 		rc = 1;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	mem = grp ->gr_mem;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (mem != NULL) {
557c478bd9Sstevel@tonic-gate 		if (*mem != NULL)
567c478bd9Sstevel@tonic-gate 			if (fputs(*mem++, fp) == EOF)
577c478bd9Sstevel@tonic-gate 				rc = 1;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 		while (*mem != NULL)
607c478bd9Sstevel@tonic-gate 			if (fprintf(fp, ",%s", *mem++) == EOF)
617c478bd9Sstevel@tonic-gate 				rc = 1;
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	if (putc('\n', fp) == EOF)
647c478bd9Sstevel@tonic-gate 		rc = 1;
657c478bd9Sstevel@tonic-gate 	return (rc);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate dogetgr(const char **list)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	struct group *grp;
727c478bd9Sstevel@tonic-gate 	int rc = EXC_SUCCESS;
737c478bd9Sstevel@tonic-gate 	char *ptr;
747c478bd9Sstevel@tonic-gate 	gid_t gid;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (list == NULL || *list == NULL) {
777c478bd9Sstevel@tonic-gate 		while ((grp = getgrent()) != NULL)
787c478bd9Sstevel@tonic-gate 			(void) putgrent(grp, stdout);
797c478bd9Sstevel@tonic-gate 	} else {
807c478bd9Sstevel@tonic-gate 		for (; *list != NULL; list++) {
81*34a0f871Svp157776 			errno = 0;
82*34a0f871Svp157776 
83*34a0f871Svp157776 			/*
84*34a0f871Svp157776 			 * Here we assume that the argument passed is
85*34a0f871Svp157776 			 * a gid, if it can be completely transformed
86*34a0f871Svp157776 			 * to a long integer. So we check for gid in
87*34a0f871Svp157776 			 * the database and if we fail then we check
88*34a0f871Svp157776 			 * for the group name.
89*34a0f871Svp157776 			 * If the argument passed is not numeric, then
90*34a0f871Svp157776 			 * we take it as the group name and proceed.
91*34a0f871Svp157776 			 */
927c478bd9Sstevel@tonic-gate 			gid = strtol(*list, &ptr, 10);
93*34a0f871Svp157776 			if (!(*ptr == '\0' && errno == 0) ||
94*34a0f871Svp157776 			    ((grp = getgrgid(gid)) == NULL)) {
957c478bd9Sstevel@tonic-gate 				grp = getgrnam(*list);
96*34a0f871Svp157776 			}
977c478bd9Sstevel@tonic-gate 			if (grp == NULL)
987c478bd9Sstevel@tonic-gate 				rc = EXC_NAME_NOT_FOUND;
997c478bd9Sstevel@tonic-gate 			else
1007c478bd9Sstevel@tonic-gate 				(void) putgrent(grp, stdout);
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	return (rc);
1057c478bd9Sstevel@tonic-gate }
106