Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/distrib/utils/libhack replace previous "cut down" getpwent.c...



details:   https://anonhg.NetBSD.org/src/rev/068345f9c224
branches:  trunk
changeset: 521641:068345f9c224
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sat Feb 02 15:57:54 2002 +0000

description:
replace previous "cut down" getpwent.c with a version which is closer
to libc/gen/getpwent.c.  this version reads from master.passwd instead
of passwd  (thus removing the need to keep both in the mini file system
and having to run pwd_mkdb to generate the latter)

diffstat:

 distrib/utils/libhack/getpwent.c |  300 ++++++++++++++++++++++----------------
 1 files changed, 170 insertions(+), 130 deletions(-)

diffs (truncated from 365 to 300 lines):

diff -r bbbc360918fb -r 068345f9c224 distrib/utils/libhack/getpwent.c
--- a/distrib/utils/libhack/getpwent.c  Sat Feb 02 15:31:58 2002 +0000
+++ b/distrib/utils/libhack/getpwent.c  Sat Feb 02 15:57:54 2002 +0000
@@ -1,8 +1,8 @@
-/*     $NetBSD: getpwent.c,v 1.4 2001/06/15 17:26:51 tsutsui Exp $     */
+/*     $NetBSD: getpwent.c,v 1.5 2002/02/02 15:57:54 lukem Exp $       */
 
 /*
- * Copyright (c) 1995 Gordon W. Ross
- * All rights reserved.
+ * Copyright (c) 1987, 1988, 1989, 1993, 1994, 1995
+ *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -12,27 +12,31 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- * 4. All advertising materials mentioning features or use of this software
+ * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
- *      This product includes software developed by Gordon W. Ross
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 /*
- * Smaller replacement for: libc/gen/getpwent.c
- * Needed by programs like: rsh, rlogin
+ * Copied from:  lib/libc/gen/getpwent.c
+ *     NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp
+ * and then gutted, leaving only /etc/master.passwd support.
  */
 
 #include <sys/cdefs.h>
@@ -40,154 +44,190 @@
 #ifdef __weak_alias
 #define endpwent               _endpwent
 #define getpwent               _getpwent
+#define getpwuid               _getpwuid
 #define getpwnam               _getpwnam
-#define getpwuid               _getpwuid
+#define setpwent               _setpwent
 #define setpassent             _setpassent
-#define setpwent               _setpwent
-#endif
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <pwd.h>
-
-#ifdef __weak_alias
 __weak_alias(endpwent,_endpwent)
 __weak_alias(getpwent,_getpwent)
+__weak_alias(getpwuid,_getpwuid)
 __weak_alias(getpwnam,_getpwnam)
-__weak_alias(getpwuid,_getpwuid)
+__weak_alias(setpwent,_setpwent)
 __weak_alias(setpassent,_setpassent)
-__weak_alias(setpwent,_setpwent)
 #endif
 
-#define        PWNULL  (struct passwd *)0
-#define MAXFIELD 8
-
-static char *pw_file = "/etc/passwd";
-static FILE *pw_fp;
-static char  pw_line[128];
-static struct passwd pw_ent;
+#include <sys/param.h>
 
-/*
- * Open passwd file if necessary, and
- * get the next entry.
- */
-struct passwd *
-getpwent()
-{
-       char *fv[MAXFIELD];
-       char *p;
-       int   fc;
+#include <limits.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 
-       /* Open passwd file if not already. */
-       if (pw_fp == NULL)
-               pw_fp = fopen(pw_file, "r");
-       /* Still NULL.  No passwd file? */
-       if (pw_fp == NULL)
-               return PWNULL;
+static int             pwstart(void);
+static int             pwscan(int, uid_t, const char *);
+static int             pwmatchline(int, uid_t, const char *);
 
-readnext:
-       /* Read the next line... */
-       if (fgets(pw_line, sizeof(pw_line), pw_fp) == NULL)
-               return PWNULL;
+static FILE            *_pw_fp;
+static struct passwd   _pw_passwd;     /* password structure */
+static int             _pw_stayopen;   /* keep fd's open */
+static int             _pw_filesdone;
 
-       /* ...and parse it. */
-       p = pw_line;
-       fc = 0;
-       while (fc < MAXFIELD) {
-               fv[fc] = strsep(&p, ":\n");
-               if (fv[fc] == NULL)
-                       break;
-               fc++;
-       }
+#define        MAXLINELENGTH   1024
 
-       /* Need at least 0..5 */
-       if (fc < 6)
-               goto readnext;
-       while (fc < MAXFIELD)
-               fv[fc++] = "";
+static char            pwline[MAXLINELENGTH];
 
-       /* Build the pw entry... */
-       pw_ent.pw_name   = fv[0];
-       pw_ent.pw_passwd = fv[1];
-       pw_ent.pw_uid = atoi(fv[2]);
-       pw_ent.pw_gid = atoi(fv[3]);
-       pw_ent.pw_gecos = fv[4];
-       pw_ent.pw_dir   = fv[5];
-       pw_ent.pw_shell = fv[6];
+struct passwd *
+getpwent(void)
+{
 
-       return (&pw_ent);
+       if ((!_pw_fp && !pwstart()) || !pwscan(0, 0, NULL))
+               return (NULL);
+       return (&_pw_passwd);
 }
 
-/* internal for setpwent() */
+struct passwd *
+getpwnam(const char *name)
+{
+       int rval;
+
+       if (!pwstart())
+               return NULL;
+       rval = pwscan(1, 0, name);
+       if (!_pw_stayopen)
+               endpwent();
+       return (rval) ? &_pw_passwd : NULL;
+}
+
+struct passwd *
+getpwuid(uid_t uid)
+{
+       int rval;
+
+       if (!pwstart())
+               return NULL;
+       rval = pwscan(1, uid, NULL);
+       if (!_pw_stayopen)
+               endpwent();
+       return (rval) ? &_pw_passwd : NULL;
+}
+
+void
+setpwent(void)
+{
+
+       (void) setpassent(0);
+}
+
 int
-setpassent(stayopen)
-       int stayopen;
+setpassent(int stayopen)
 {
-       if (pw_fp)
-               rewind(pw_fp);
+
+       if (!pwstart())
+               return 0;
+       _pw_stayopen = stayopen;
        return 1;
 }
 
-/* rewind to the beginning. */
 void
-setpwent()
+endpwent(void)
 {
-       (void) setpassent(0);
-}
 
-/* done with the passwd file */
-void
-endpwent()
-{
-       if (pw_fp) {
-               fclose(pw_fp);
-               pw_fp = NULL;
+       _pw_filesdone = 0;
+       if (_pw_fp) {
+               (void)fclose(_pw_fp);
+               _pw_fp = NULL;
        }
 }
 
-struct passwd *
-getpwnam(name)
-       const char *name;
+static int
+pwstart(void)
 {
-       struct passwd *pw;
+
+       _pw_filesdone = 0;
+       if (_pw_fp) {
+               rewind(_pw_fp);
+               return 1;
+       }
+       return (_pw_fp = fopen(_PATH_MASTERPASSWD, "r")) ? 1 : 0;
+}
+
+
+static int
+pwscan(int search, uid_t uid, const char *name)
+{
 
-       setpwent();
-       while ((pw = getpwent()) != PWNULL)
-               if (!strcmp(pw->pw_name, name))
-                       break;
+       if (_pw_filesdone)
+               return 0;
+       for (;;) {
+               if (!fgets(pwline, sizeof(pwline), _pw_fp)) {
+                       if (!search)
+                               _pw_filesdone = 1;
+                       return 0;
+               }
+               /* skip lines that are too big */
+               if (!strchr(pwline, '\n')) {
+                       int ch;
 
-       endpwent();
-       return(pw);
+                       while ((ch = getc(_pw_fp)) != '\n' && ch != EOF)
+                               ;
+                       continue;
+               }
+               if (pwmatchline(search, uid, name))
+                       return 1;
+       }
+       /* NOTREACHED */
 }
 
-struct passwd *
-getpwuid(uid)
-       uid_t uid;
+static int
+pwmatchline(int search, uid_t uid, const char *name)
 {
-       struct passwd *pw;
+       unsigned long   id;
+       char            *cp, *bp, *ep;
+



Home | Main Index | Thread Index | Old Index