about summary refs log tree commit diff
path: root/grp
diff options
context:
space:
mode:
Diffstat (limited to 'grp')
-rw-r--r--grp/getgrgid.c26
-rw-r--r--grp/getgrnam.c26
-rw-r--r--grp/grp.h7
-rw-r--r--grp/grpread.c33
4 files changed, 50 insertions, 42 deletions
diff --git a/grp/getgrgid.c b/grp/getgrgid.c
index 1375f5ff56..ef3860fe40 100644
--- a/grp/getgrgid.c
+++ b/grp/getgrgid.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1995 Free Software Foundation, Inc.
 This file is part of the GNU C Library.
 
 The GNU C Library is free software; you can redistribute it and/or
@@ -24,27 +24,13 @@ Cambridge, MA 02139, USA.  */
 
 /* Search for an entry with a matching group ID.  */
 struct group *
-DEFUN(getgrgid, (gid), register gid_t gid)
+DEFUN(getgrgid, (gid), gid_t gid)
 {
-  static PTR info = NULL;
-  register FILE *stream;
-  register struct group *g;
-
-  if (info == NULL)
+  int match (struct group *p)
     {
-      info = __grpalloc();
-      if (info == NULL)
-	return NULL;
+      return p->gr_gid == gid;
     }
+  static void *info;
 
-  stream = __grpopen();
-  if (stream == NULL)
-    return NULL;
-
-  while ((g = __grpread(stream, info)) != NULL)
-    if (g->gr_gid == (gid_t) gid)
-      break;
-
-  (void) fclose(stream);
-  return g;
+  return __grpscan (&info, &match);
 }
diff --git a/grp/getgrnam.c b/grp/getgrnam.c
index 1f88ea3ff3..841677070d 100644
--- a/grp/getgrnam.c
+++ b/grp/getgrnam.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1995 Free Software Foundation, Inc.
 This file is part of the GNU C Library.
 
 The GNU C Library is free software; you can redistribute it and/or
@@ -24,27 +24,13 @@ Cambridge, MA 02139, USA.  */
 
 /* Search for an entry with a matching name.  */
 struct group *
-DEFUN(getgrnam, (name), register CONST char *name)
+DEFUN(getgrnam, (name), const char *name)
 {
-  static PTR info = NULL;
-  register FILE *stream;
-  register struct group *g;
-
-  if (info == NULL)
+  int match (struct group *p)
     {
-      info = __grpalloc();
-      if (info == NULL)
-	return NULL;
+      return ! strcmp (name, p->gr_name);
     }
+  static void *info;
 
-  stream = __grpopen();
-  if (stream == NULL)
-    return NULL;
-
-  while ((g = __grpread(stream, info)) != NULL)
-    if (!strcmp(g->gr_name, name))
-      break;
-
-  (void) fclose(stream);
-  return g;
+  return __grpscan (&info, &match);
 }
diff --git a/grp/grp.h b/grp/grp.h
index 2562671885..722e00f8ba 100644
--- a/grp/grp.h
+++ b/grp/grp.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
 This file is part of the GNU C Library.
 
 The GNU C Library is free software; you can redistribute it and/or
@@ -55,6 +55,11 @@ extern struct group *__grpread __P ((FILE * __stream, __ptr_t __g));
 
 /* Return a chunk of memory containing pre-initialized data for __grpread.  */
 extern __ptr_t __grpalloc __P ((void));
+
+/* Scan the group file, filling in G, until SELECTOR returns nonzero for an
+   entry.  Return the `struct group' of G if successful, NULL on failure.  */
+extern struct group *__grpscan __P ((__ptr_t *__p,
+				     int (*__selector) (struct group *)));
 #endif
 
 
diff --git a/grp/grpread.c b/grp/grpread.c
index b7bac4c192..1fed32f2e3 100644
--- a/grp/grpread.c
+++ b/grp/grpread.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
 This file is part of the GNU C Library.
 
 The GNU C Library is free software; you can redistribute it and/or
@@ -133,3 +133,34 @@ DEFUN(__grpread, (stream, g), FILE *stream AND PTR CONST g)
 
   return &info->g;
 }
+
+
+struct group *
+__grpscan (void **info, int (*selector) (struct group *))
+{
+  FILE *stream;
+  struct group *p;
+
+  if (*info == NULL)
+    {
+      *info = __grpalloc ();
+      if (info == NULL)
+	return NULL;
+    }
+
+  stream = __grpopen ();
+  if (stream == NULL)
+    return NULL;
+
+  p = NULL;
+  while (! feof (stream))
+    {
+      p = __grpread (stream, *info);
+      if (p && (*selector) (p))
+	break;
+      p = NULL;
+    }
+
+  (void) fclose (stream);
+  return p;
+}