446cf2
From ee1c062be09da006e82ab34c1c9b5c82dd2af92c Mon Sep 17 00:00:00 2001
446cf2
From: Florian Weimer <fweimer@redhat.com>
446cf2
Date: Thu, 16 Jul 2020 17:34:19 +0200
446cf2
Subject: [PATCH 09/11] pwd: Implement fgetpwent_r using __nss_fgetent_r
446cf2
446cf2
Tested-by: Carlos O'Donell <carlos@redhat.com>
446cf2
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
---
446cf2
 pwd/fgetpwent_r.c | 43 ++++++-------------------------------------
446cf2
 1 file changed, 6 insertions(+), 37 deletions(-)
446cf2
446cf2
diff -rup a/pwd/fgetpwent_r.c b/pwd/fgetpwent_r.c
446cf2
--- a/pwd/fgetpwent_r.c	2018-08-01 01:10:47.000000000 -0400
446cf2
+++ b/pwd/fgetpwent_r.c	2020-09-14 19:03:41.277514743 -0400
446cf2
@@ -20,9 +20,6 @@
446cf2
 #include <stdio.h>
446cf2
 #include <pwd.h>
446cf2
 
446cf2
-#define flockfile(s) _IO_flockfile (s)
446cf2
-#define funlockfile(s) _IO_funlockfile (s)
446cf2
-
446cf2
 /* Define a line parsing function using the common code
446cf2
    used in the nss_files module.  */
446cf2
 
446cf2
@@ -72,39 +69,11 @@ int
446cf2
 __fgetpwent_r (FILE *stream, struct passwd *resbuf, char *buffer,
446cf2
 	       size_t buflen, struct passwd **result)
446cf2
 {
446cf2
-  char *p;
446cf2
-
446cf2
-  flockfile (stream);
446cf2
-  do
446cf2
-    {
446cf2
-      buffer[buflen - 1] = '\xff';
446cf2
-      p = fgets_unlocked (buffer, buflen, stream);
446cf2
-      if (p == NULL && feof_unlocked (stream))
446cf2
-	{
446cf2
-	  funlockfile (stream);
446cf2
-	  *result = NULL;
446cf2
-	  __set_errno (ENOENT);
446cf2
-	  return errno;
446cf2
-	}
446cf2
-      if (p == NULL || buffer[buflen - 1] != '\xff')
446cf2
-	{
446cf2
-	  funlockfile (stream);
446cf2
-	  *result = NULL;
446cf2
-	  __set_errno (ERANGE);
446cf2
-	  return errno;
446cf2
-	}
446cf2
-
446cf2
-      /* Skip leading blanks.  */
446cf2
-      while (isspace (*p))
446cf2
-	++p;
446cf2
-    } while (*p == '\0' || *p == '#' ||	/* Ignore empty and comment lines.  */
446cf2
-	     /* Parse the line.  If it is invalid, loop to
446cf2
-		get the next line of the file to parse.  */
446cf2
-	     ! parse_line (p, resbuf, (void *) buffer, buflen, &errno));
446cf2
-
446cf2
-  funlockfile (stream);
446cf2
-
446cf2
-  *result = resbuf;
446cf2
-  return 0;
446cf2
+  int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
446cf2
+  if (ret == 0)
446cf2
+    *result = resbuf;
446cf2
+  else
446cf2
+    *result = NULL;
446cf2
+  return ret;
446cf2
 }
446cf2
 weak_alias (__fgetpwent_r, fgetpwent_r)