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