Home | History | Annotate | Line # | Download | only in libhack
getpwent.c revision 1.3.10.1
      1 /*	$NetBSD: getpwent.c,v 1.3.10.1 2002/06/06 17:04:48 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  * 4. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Gordon W. Ross
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Smaller replacement for: libc/gen/getpwent.c
     35  * Needed by programs like: rsh, rlogin
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 
     40 #ifdef __weak_alias
     41 #define endpwent		_endpwent
     42 #define getpwent		_getpwent
     43 #define getpwnam		_getpwnam
     44 #define getpwuid		_getpwuid
     45 #define setpassent		_setpassent
     46 #define setpwent		_setpwent
     47 #endif
     48 
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <pwd.h>
     53 
     54 #ifdef __weak_alias
     55 __weak_alias(endpwent,_endpwent)
     56 __weak_alias(getpwent,_getpwent)
     57 __weak_alias(getpwnam,_getpwnam)
     58 __weak_alias(getpwuid,_getpwuid)
     59 __weak_alias(setpassent,_setpassent)
     60 __weak_alias(setpwent,_setpwent)
     61 #endif
     62 
     63 #define	PWNULL	(struct passwd *)0
     64 #define MAXFIELD 8
     65 
     66 static char *pw_file = "/etc/passwd";
     67 static FILE *pw_fp;
     68 static char  pw_line[128];
     69 static struct passwd pw_ent;
     70 
     71 /*
     72  * Open passwd file if necessary, and
     73  * get the next entry.
     74  */
     75 struct passwd *
     76 getpwent()
     77 {
     78 	char *fv[MAXFIELD];
     79 	char *p;
     80 	int   fc;
     81 
     82 	/* Open passwd file if not already. */
     83 	if (pw_fp == NULL)
     84 		pw_fp = fopen(pw_file, "r");
     85 	/* Still NULL.  No passwd file? */
     86 	if (pw_fp == NULL)
     87 		return PWNULL;
     88 
     89 readnext:
     90 	/* Read the next line... */
     91 	if (fgets(pw_line, sizeof(pw_line), pw_fp) == NULL)
     92 		return PWNULL;
     93 
     94 	/* ...and parse it. */
     95 	p = pw_line;
     96 	fc = 0;
     97 	while (fc < MAXFIELD) {
     98 		fv[fc] = strsep(&p, ":\n");
     99 		if (fv[fc] == NULL)
    100 			break;
    101 		fc++;
    102 	}
    103 
    104 	/* Need at least 0..5 */
    105 	if (fc < 6)
    106 		goto readnext;
    107 	while (fc < MAXFIELD)
    108 		fv[fc++] = "";
    109 
    110 	/* Build the pw entry... */
    111 	pw_ent.pw_name   = fv[0];
    112 	pw_ent.pw_passwd = fv[1];
    113 	pw_ent.pw_uid = atoi(fv[2]);
    114 	pw_ent.pw_gid = atoi(fv[3]);
    115 	pw_ent.pw_class = "";
    116 	pw_ent.pw_gecos = fv[4];
    117 	pw_ent.pw_dir   = fv[5];
    118 	pw_ent.pw_shell = fv[6];
    119 
    120 	return (&pw_ent);
    121 }
    122 
    123 /* internal for setpwent() */
    124 int
    125 setpassent(stayopen)
    126 	int stayopen;
    127 {
    128 	if (pw_fp)
    129 		rewind(pw_fp);
    130 	return 1;
    131 }
    132 
    133 /* rewind to the beginning. */
    134 void
    135 setpwent()
    136 {
    137 	(void) setpassent(0);
    138 }
    139 
    140 /* done with the passwd file */
    141 void
    142 endpwent()
    143 {
    144 	if (pw_fp) {
    145 		fclose(pw_fp);
    146 		pw_fp = NULL;
    147 	}
    148 }
    149 
    150 struct passwd *
    151 getpwnam(name)
    152 	const char *name;
    153 {
    154 	struct passwd *pw;
    155 
    156 	setpwent();
    157 	while ((pw = getpwent()) != PWNULL)
    158 		if (!strcmp(pw->pw_name, name))
    159 			break;
    160 
    161 	endpwent();
    162 	return(pw);
    163 }
    164 
    165 struct passwd *
    166 getpwuid(uid)
    167 	uid_t uid;
    168 {
    169 	struct passwd *pw;
    170 
    171 	setpwent();
    172 	while ((pw = getpwent()) != PWNULL)
    173 		if (pw->pw_uid == uid)
    174 			break;
    175 
    176 	endpwent();
    177 	return(pw);
    178 }
    179 
    180 #ifdef	TEST_MAIN
    181 main() {
    182 	struct passwd *pw;
    183 
    184 	printf("#name, password, uid, gid, comment, dir, shell\n");
    185 
    186 	while ((pw = getpwent()) != NULL) {
    187 		printf("%s:%s:", pw->pw_name, pw->pw_passwd);
    188 		printf("%d:%d:", pw->pw_uid, pw->pw_gid);
    189 		printf("%s:", pw->pw_gecos);
    190 		printf("%s:", pw->pw_dir);
    191 		printf("%s\n", pw->pw_shell);
    192 	}
    193 }
    194 #endif
    195