xref: /titanic_50/usr/src/cmd/getent/dogetpw.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  */
2134a0f871Svp157776 
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 <pwd.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  * getpwnam - get entries from password database
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate int
dogetpw(const char ** list)377c478bd9Sstevel@tonic-gate dogetpw(const char **list)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
407c478bd9Sstevel@tonic-gate 	int rc = EXC_SUCCESS;
417c478bd9Sstevel@tonic-gate 	char *ptr;
427c478bd9Sstevel@tonic-gate 	uid_t uid;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	if (list == NULL || *list == NULL) {
467c478bd9Sstevel@tonic-gate 		while ((pwp = getpwent()) != NULL)
477c478bd9Sstevel@tonic-gate 			(void) putpwent(pwp, stdout);
487c478bd9Sstevel@tonic-gate 	} else {
497c478bd9Sstevel@tonic-gate 		for (; *list != NULL; list++) {
5034a0f871Svp157776 			errno = 0;
5134a0f871Svp157776 
5234a0f871Svp157776 			/*
5334a0f871Svp157776 			 * Here we assume that the argument passed is
5434a0f871Svp157776 			 * a uid, if it can be completely transformed
5534a0f871Svp157776 			 * to a long integer. So we check for uid in
5634a0f871Svp157776 			 * the database and if we fail then we check
5734a0f871Svp157776 			 * for the user name.
5834a0f871Svp157776 			 * If the argument passed is not numeric, then
5934a0f871Svp157776 			 * we take it as the user name and proceed.
6034a0f871Svp157776 			 */
61*2b4a7802SBaban Kenkre 			uid = strtoul(*list, &ptr, 10);
6234a0f871Svp157776 			if (!(*ptr == '\0' && errno == 0) ||
6334a0f871Svp157776 			    ((pwp = getpwuid(uid)) == NULL)) {
647c478bd9Sstevel@tonic-gate 				pwp = getpwnam(*list);
6534a0f871Svp157776 			}
6634a0f871Svp157776 
677c478bd9Sstevel@tonic-gate 			if (pwp == NULL)
687c478bd9Sstevel@tonic-gate 				rc = EXC_NAME_NOT_FOUND;
697c478bd9Sstevel@tonic-gate 			else
707c478bd9Sstevel@tonic-gate 				(void) putpwent(pwp, stdout);
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	return (rc);
757c478bd9Sstevel@tonic-gate }
76