Home | History | Annotate | Line # | Download | only in libhack
getpwent.c revision 1.1.1.1
      1 /*	$NetBSD: getpwent.c,v 1.1.1.1 1995/10/08 23:08:48 gwr 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 <stdio.h>
     39 #include <string.h>
     40 #include <pwd.h>
     41 
     42 #define	PWNULL	(struct passwd *)0
     43 #define MAXFIELD 8
     44 
     45 static char *pw_file = "/etc/passwd";
     46 static FILE *pw_fp;
     47 static char  pw_line[128];
     48 static struct passwd pw_ent;
     49 
     50 /*
     51  * Open passwd file if necessary, and
     52  * get the next entry.
     53  */
     54 struct passwd *
     55 getpwent()
     56 {
     57 	register char *p;
     58 	char *fv[MAXFIELD];
     59 	int   fc;
     60 
     61 	/* Open passwd file if not already. */
     62 	if (pw_fp == NULL)
     63 		pw_fp = fopen(pw_file, "r");
     64 	/* Still NULL.  No passwd file? */
     65 	if (pw_fp == NULL)
     66 		return PWNULL;
     67 
     68 	/* Read the next line... */
     69 readnext:
     70 	if (fgets(pw_line, sizeof(pw_line), pw_fp) == NULL)
     71 		return PWNULL;
     72 
     73 	/* ...and parse it. */
     74 	p = pw_line;
     75 	fc = 0;
     76 	while (fc < MAXFIELD) {
     77 		/* end of line? */
     78 		if (*p == '\n') {
     79 			*p = '\0';
     80 			break;
     81 		}
     82 		if (*p == '\0')
     83 			break;
     84 		/* save start of field */
     85 		fv[fc++] = p;
     86 		/* find end of field */
     87 		while (*p) {
     88 			if (*p == '\n') {
     89 				*p = '\0';
     90 				goto eol;
     91 			}
     92 			if (*p == ':') {
     93 				*p++ = '\0';
     94 				break;
     95 			}
     96 			p++;
     97 		}
     98 	}
     99  eol:
    100 
    101 	/* Need at least 0..5 */
    102 	if (fc < 6)
    103 		goto readnext;
    104 	while (fc < MAXFIELD)
    105 		fv[fc++] = "";
    106 
    107 	/* Build the pw entrt... */
    108 	pw_ent.pw_name   = fv[0];
    109 	pw_ent.pw_passwd = fv[1];
    110 	pw_ent.pw_uid = atoi(fv[2]);
    111 	pw_ent.pw_gid = atoi(fv[3]);
    112 	pw_ent.pw_gecos = fv[4];
    113 	pw_ent.pw_dir   = fv[5];
    114 	pw_ent.pw_shell = fv[6];
    115 
    116 	return (&pw_ent);
    117 }
    118 
    119 /* internal for setpwent() */
    120 int
    121 setpassent(stayopen)
    122 	int stayopen;
    123 {
    124 	if (pw_fp)
    125 		rewind(pw_fp);
    126 }
    127 
    128 /* rewind to the beginning. */
    129 void
    130 setpwent()
    131 {
    132 	(void) setpassent(0);
    133 }
    134 
    135 /* done with the passwd file */
    136 void
    137 endpwent()
    138 {
    139 	if (pw_fp)
    140 		fclose(pw_fp);
    141 }
    142 
    143 struct passwd *
    144 getpwnam(name)
    145 	const char *name;
    146 {
    147 	struct passwd *pw;
    148 
    149 	setpwent();
    150 	while ((pw = getpwent()) != PWNULL)
    151 		if (!strcmp(pw->pw_name, name))
    152 			break;
    153 
    154 	endpwent();
    155 	return(pw);
    156 }
    157 
    158 struct passwd *
    159 getpwuid(uid)
    160 	uid_t uid;
    161 {
    162 	struct passwd *pw;
    163 
    164 	setpwent();
    165 	while ((pw = getpwent()) != PWNULL)
    166 		if (pw->pw_uid == uid)
    167 			break;
    168 
    169 	endpwent();
    170 	return(pw);
    171 }
    172 
    173 #ifdef	TEST_MAIN
    174 main() {
    175 	struct passwd *pw;
    176 
    177 	printf("name, password, uid, gid, comment, dir, shell\n");
    178 
    179 	while ((pw = getpwent()) != NULL) {
    180 		printf("%s:%s:", pw->pw_name, pw->pw_passwd);
    181 		printf("%d:%d:", pw->pw_uid, pw->pw_gid);
    182 		printf("%s:", pw->pw_gecos);
    183 		printf("%s:", pw->pw_dir);
    184 		printf("%s\n", pw->pw_shell);
    185 	}
    186 }
    187 #endif
    188