xref: /illumos-gate/usr/src/cmd/getent/dogetpw.c (revision 2b4a7802)
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
534a0f871Svp  * Common Development and Distribution License (the "License").
634a0f871Svp  * 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  */
2134a0f871Svp 
227c478bd9Sstevel@tonic-gate /*
23*2b4a7802SBaban Kenkre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2434a0f871Svp  * 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>
3034a0f871Svp #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++) {
5034a0f871Svp 			errno = 0;
5134a0f871Svp 
5234a0f871Svp 			/*
5334a0f871Svp 			 * Here we assume that the argument passed is
5434a0f871Svp 			 * a uid, if it can be completely transformed
5534a0f871Svp 			 * to a long integer. So we check for uid in
5634a0f871Svp 			 * the database and if we fail then we check
5734a0f871Svp 			 * for the user name.
5834a0f871Svp 			 * If the argument passed is not numeric, then
5934a0f871Svp 			 * we take it as the user name and proceed.
6034a0f871Svp 			 */
61*2b4a7802SBaban Kenkre 			uid = strtoul(*list, &ptr, 10);
6234a0f871Svp 			if (!(*ptr == '\0' && errno == 0) ||
6334a0f871Svp 			    ((pwp = getpwuid(uid)) == NULL)) {
647c478bd9Sstevel@tonic-gate 				pwp = getpwnam(*list);
6534a0f871Svp 			}
6634a0f871Svp 
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