Home | History | Annotate | Line # | Download | only in libhack
getpwent.c revision 1.1.1.1.2.1
      1 /*	$NetBSD: getpwent.c,v 1.1.1.1.2.1 1995/10/11 23:52:08 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 	char *fv[MAXFIELD];
     58 	char *p;
     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 readnext:
     69 	/* Read the next line... */
     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 		fv[fc] = strsep(&p, ":\n");
     78 		if (fv[fc] == NULL)
     79 			break;
     80 		fc++;
     81 	}
     82 
     83 	/* Need at least 0..5 */
     84 	if (fc < 6)
     85 		goto readnext;
     86 	while (fc < MAXFIELD)
     87 		fv[fc++] = "";
     88 
     89 	/* Build the pw entry... */
     90 	pw_ent.pw_name   = fv[0];
     91 	pw_ent.pw_passwd = fv[1];
     92 	pw_ent.pw_uid = atoi(fv[2]);
     93 	pw_ent.pw_gid = atoi(fv[3]);
     94 	pw_ent.pw_gecos = fv[4];
     95 	pw_ent.pw_dir   = fv[5];
     96 	pw_ent.pw_shell = fv[6];
     97 
     98 	return (&pw_ent);
     99 }
    100 
    101 /* internal for setpwent() */
    102 int
    103 setpassent(stayopen)
    104 	int stayopen;
    105 {
    106 	if (pw_fp)
    107 		rewind(pw_fp);
    108 }
    109 
    110 /* rewind to the beginning. */
    111 void
    112 setpwent()
    113 {
    114 	(void) setpassent(0);
    115 }
    116 
    117 /* done with the passwd file */
    118 void
    119 endpwent()
    120 {
    121 	if (pw_fp) {
    122 		fclose(pw_fp);
    123 		pw_fp = NULL;
    124 	}
    125 }
    126 
    127 struct passwd *
    128 getpwnam(name)
    129 	const char *name;
    130 {
    131 	struct passwd *pw;
    132 
    133 	setpwent();
    134 	while ((pw = getpwent()) != PWNULL)
    135 		if (!strcmp(pw->pw_name, name))
    136 			break;
    137 
    138 	endpwent();
    139 	return(pw);
    140 }
    141 
    142 struct passwd *
    143 getpwuid(uid)
    144 	uid_t uid;
    145 {
    146 	struct passwd *pw;
    147 
    148 	setpwent();
    149 	while ((pw = getpwent()) != PWNULL)
    150 		if (pw->pw_uid == uid)
    151 			break;
    152 
    153 	endpwent();
    154 	return(pw);
    155 }
    156 
    157 #ifdef	TEST_MAIN
    158 main() {
    159 	struct passwd *pw;
    160 
    161 	printf("#name, password, uid, gid, comment, dir, shell\n");
    162 
    163 	while ((pw = getpwent()) != NULL) {
    164 		printf("%s:%s:", pw->pw_name, pw->pw_passwd);
    165 		printf("%d:%d:", pw->pw_uid, pw->pw_gid);
    166 		printf("%s:", pw->pw_gecos);
    167 		printf("%s:", pw->pw_dir);
    168 		printf("%s\n", pw->pw_shell);
    169 	}
    170 }
    171 #endif
    172