xref: /titanic_51/usr/src/cmd/getent/dogetgr.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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
534a0f871Svp157776  * Common Development and Distribution License (the "License").
634a0f871Svp157776  * 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*2b4a7802SBaban Kenkre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2434a0f871Svp157776  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <grp.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
3034a0f871Svp157776 #include <errno.h>
317c478bd9Sstevel@tonic-gate #include "getent.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static int
357c478bd9Sstevel@tonic-gate putgrent(const struct group *grp, FILE *fp)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	char **mem;
387c478bd9Sstevel@tonic-gate 	int rc = 0;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if (grp == NULL) {
417c478bd9Sstevel@tonic-gate 		return (1);
427c478bd9Sstevel@tonic-gate 	}
437c478bd9Sstevel@tonic-gate 
44*2b4a7802SBaban Kenkre 	if (fprintf(fp, "%s:%s:%u:",
457c478bd9Sstevel@tonic-gate 	    grp->gr_name != NULL ? grp->gr_name : "",
467c478bd9Sstevel@tonic-gate 	    grp->gr_passwd != NULL ? grp->gr_passwd : "",
477c478bd9Sstevel@tonic-gate 	    grp->gr_gid) == EOF)
487c478bd9Sstevel@tonic-gate 		rc = 1;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	mem = grp ->gr_mem;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (mem != NULL) {
537c478bd9Sstevel@tonic-gate 		if (*mem != NULL)
547c478bd9Sstevel@tonic-gate 			if (fputs(*mem++, fp) == EOF)
557c478bd9Sstevel@tonic-gate 				rc = 1;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 		while (*mem != NULL)
587c478bd9Sstevel@tonic-gate 			if (fprintf(fp, ",%s", *mem++) == EOF)
597c478bd9Sstevel@tonic-gate 				rc = 1;
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 	if (putc('\n', fp) == EOF)
627c478bd9Sstevel@tonic-gate 		rc = 1;
637c478bd9Sstevel@tonic-gate 	return (rc);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
677c478bd9Sstevel@tonic-gate dogetgr(const char **list)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	struct group *grp;
707c478bd9Sstevel@tonic-gate 	int rc = EXC_SUCCESS;
717c478bd9Sstevel@tonic-gate 	char *ptr;
727c478bd9Sstevel@tonic-gate 	gid_t gid;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (list == NULL || *list == NULL) {
757c478bd9Sstevel@tonic-gate 		while ((grp = getgrent()) != NULL)
767c478bd9Sstevel@tonic-gate 			(void) putgrent(grp, stdout);
777c478bd9Sstevel@tonic-gate 	} else {
787c478bd9Sstevel@tonic-gate 		for (; *list != NULL; list++) {
7934a0f871Svp157776 			errno = 0;
8034a0f871Svp157776 
8134a0f871Svp157776 			/*
8234a0f871Svp157776 			 * Here we assume that the argument passed is
8334a0f871Svp157776 			 * a gid, if it can be completely transformed
8434a0f871Svp157776 			 * to a long integer. So we check for gid in
8534a0f871Svp157776 			 * the database and if we fail then we check
8634a0f871Svp157776 			 * for the group name.
8734a0f871Svp157776 			 * If the argument passed is not numeric, then
8834a0f871Svp157776 			 * we take it as the group name and proceed.
8934a0f871Svp157776 			 */
90*2b4a7802SBaban Kenkre 			gid = strtoul(*list, &ptr, 10);
9134a0f871Svp157776 			if (!(*ptr == '\0' && errno == 0) ||
9234a0f871Svp157776 			    ((grp = getgrgid(gid)) == NULL)) {
937c478bd9Sstevel@tonic-gate 				grp = getgrnam(*list);
9434a0f871Svp157776 			}
957c478bd9Sstevel@tonic-gate 			if (grp == NULL)
967c478bd9Sstevel@tonic-gate 				rc = EXC_NAME_NOT_FOUND;
977c478bd9Sstevel@tonic-gate 			else
987c478bd9Sstevel@tonic-gate 				(void) putgrent(grp, stdout);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	return (rc);
1037c478bd9Sstevel@tonic-gate }
104