xref: /titanic_50/usr/src/cmd/getent/dogetpw.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  */
21*34a0f871Svp157776 
22*34a0f871Svp157776 #pragma ident	"%Z%%M%	%I%	%E% SMI"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
25*34a0f871Svp157776  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
26*34a0f871Svp157776  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <pwd.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  * getpwnam - get entries from password database
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate int
397c478bd9Sstevel@tonic-gate dogetpw(const char **list)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
427c478bd9Sstevel@tonic-gate 	int rc = EXC_SUCCESS;
437c478bd9Sstevel@tonic-gate 	char *ptr;
447c478bd9Sstevel@tonic-gate 	uid_t uid;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (list == NULL || *list == NULL) {
487c478bd9Sstevel@tonic-gate 		while ((pwp = getpwent()) != NULL)
497c478bd9Sstevel@tonic-gate 			(void) putpwent(pwp, stdout);
507c478bd9Sstevel@tonic-gate 	} else {
517c478bd9Sstevel@tonic-gate 		for (; *list != NULL; list++) {
52*34a0f871Svp157776 			errno = 0;
53*34a0f871Svp157776 
54*34a0f871Svp157776 			/*
55*34a0f871Svp157776 			 * Here we assume that the argument passed is
56*34a0f871Svp157776 			 * a uid, if it can be completely transformed
57*34a0f871Svp157776 			 * to a long integer. So we check for uid in
58*34a0f871Svp157776 			 * the database and if we fail then we check
59*34a0f871Svp157776 			 * for the user name.
60*34a0f871Svp157776 			 * If the argument passed is not numeric, then
61*34a0f871Svp157776 			 * we take it as the user name and proceed.
62*34a0f871Svp157776 			 */
637c478bd9Sstevel@tonic-gate 			uid = strtol(*list, &ptr, 10);
64*34a0f871Svp157776 			if (!(*ptr == '\0' && errno == 0) ||
65*34a0f871Svp157776 			    ((pwp = getpwuid(uid)) == NULL)) {
667c478bd9Sstevel@tonic-gate 				pwp = getpwnam(*list);
67*34a0f871Svp157776 			}
68*34a0f871Svp157776 
697c478bd9Sstevel@tonic-gate 			if (pwp == NULL)
707c478bd9Sstevel@tonic-gate 				rc = EXC_NAME_NOT_FOUND;
717c478bd9Sstevel@tonic-gate 			else
727c478bd9Sstevel@tonic-gate 				(void) putpwent(pwp, stdout);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (rc);
777c478bd9Sstevel@tonic-gate }
78