From 6503705696e9e778fb51b1be354bc879f18ba61c Mon Sep 17 00:00:00 2001
From: Christopher Faylor <me@cgf.cx>
Date: Sun, 26 Jan 2003 05:38:38 +0000
Subject: [PATCH] * include/sys/strace.h (paranoid_printf): Define as not being
 part of "all" output. * pwdgrp.h (pwdgrp::next_num): Rename from next_int. 
 Returns true/false if parse operation succeeded. (pwdgrp::reparse): Remove.
 (pwdgrp::raw_ptr): New function.  Returns pointer in line.
 (pwdgrp::next_num): New functions for parsing other than unsigned long. *
 grp.cc (pwdgrp::parse_group): Reinstate previous parsing behavior.  Don't
 fill in fields with NULL and assign empty gr_mem to known pointer rather than
 doing a pointless calloc.  Streamline gr_mem parsing.  Don't increment
 curr_lines here. * passwd.cc (pwdgrp::parse_passwd): Use new behavior of
 next_num.  Don't increment curr_lines here. * uinfo.cc (pwdgrp::next_str):
 Keep returning EOL if out of data. (pwdgrp::reparse): Remove.
 (pwdgrp::next_num): Rename from next_int.  Return bool indicating success of
 parse, argument returns value parsed. (pwdgrp::add_line): Increment
 curr_lines here on successful parse. (pwdgrp::load): (from Pierre Humblet)
 Don't return status.  Just report it here.

---
 winsup/cygwin/ChangeLog            | 27 ++++++++++++++++
 winsup/cygwin/grp.cc               | 45 +++++++++++++--------------
 winsup/cygwin/include/sys/strace.h |  2 +-
 winsup/cygwin/passwd.cc            | 25 ++++++---------
 winsup/cygwin/pwdgrp.h             | 20 ++++++++++--
 winsup/cygwin/uinfo.cc             | 49 +++++++++++++++---------------
 6 files changed, 99 insertions(+), 69 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index d15895e82..0b2cd7ef0 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,30 @@
+
+2003-01-26  Christopher Faylor  <cgf@redhat.com>
+
+	* include/sys/strace.h (paranoid_printf): Define as not being part of
+	"all" output.
+
+2003-01-25  Christopher Faylor  <cgf@redhat.com>
+
+	* pwdgrp.h (pwdgrp::next_num): Rename from next_int.  Returns
+	true/false if parse operation succeeded.
+	(pwdgrp::reparse): Remove.
+	(pwdgrp::raw_ptr): New function.  Returns pointer in line.
+	(pwdgrp::next_num): New functions for parsing other than unsigned long.
+	* grp.cc (pwdgrp::parse_group): Reinstate previous parsing behavior.
+	Don't fill in fields with NULL and assign empty gr_mem to known pointer
+	rather than doing a pointless calloc.  Streamline gr_mem parsing.
+	Don't increment curr_lines here.
+	* passwd.cc (pwdgrp::parse_passwd): Use new behavior of next_num.
+	Don't increment curr_lines here.
+	* uinfo.cc (pwdgrp::next_str): Keep returning EOL if out of data.
+	(pwdgrp::reparse): Remove.
+	(pwdgrp::next_num): Rename from next_int.  Return bool indicating
+	success of parse, argument returns value parsed.
+	(pwdgrp::add_line): Increment curr_lines here on successful parse.
+	(pwdgrp::load): (from Pierre Humblet) Don't return status.  Just report
+	it here.
+
 2003-01-25  Christopher Faylor  <cgf@redhat.com>
 
 	* pwdgrp.cc (pwdgrp::reparse): Declare.
diff --git a/winsup/cygwin/grp.cc b/winsup/cygwin/grp.cc
index b4b590ba9..c16d925f5 100644
--- a/winsup/cygwin/grp.cc
+++ b/winsup/cygwin/grp.cc
@@ -39,38 +39,36 @@ pwdgrp::parse_group ()
   char *dp;
 
 # define grp (*group_buf)[curr_lines]
+
   memset (&grp, 0, sizeof (grp));
+
   grp.gr_name = next_str ();
-  if (!grp.gr_name)
+  if (!*grp.gr_name)
     return false;
 
   grp.gr_passwd = next_str ();
-  int n = next_int ();
-  if (n >= 0)
+
+  if (!next_num (grp.gr_gid))
+    return false;
+
+  int n;
+  dp = raw_ptr ();
+  for (n = 0; *next_str (','); n++)
+    continue;
+
+  grp.gr_mem = &null_ptr;
+  if (n)
     {
-      grp.gr_gid = n;
-      dp = next_str ();
-      if (!dp)
-	{
-	  static char empty[] = "";
-	  dp = empty;
-	}
-      int i = 0;
-      for (char *cp = dp; (cp = strchr (cp, ',')) != NULL; cp++)
-	i++;
-      char **namearray = (char **) calloc (i + 2, sizeof (char *));
+      char **namearray = (char **) calloc (n + 2, sizeof (char *));
       if (namearray)
 	{
-	  reparse (dp);
-	  for (i = 0; (dp = next_str (',')); i++)
+	  for (int i = 0; i < n; i++, dp = strchr (dp, '\0') + 1)
 	    namearray[i] = dp;
-	  namearray[i] = NULL;
 	  grp.gr_mem = namearray;
 	}
-      curr_lines++;
-      return true;
     }
-  return false;
+
+  return true;
 # undef grp
 }
 
@@ -85,8 +83,7 @@ pwdgrp::read_group ()
     if ((*group_buf)[i].gr_mem != &null_ptr)
       free ((*group_buf)[i].gr_mem);
 
-  if (!gr.load ("/etc/group"))
-    debug_printf ("gr.load failed");
+  load ("/etc/group");
 
   /* Complete /etc/group in memory if needed */
   if (!internal_getgrgid (myself->gid))
@@ -106,11 +103,11 @@ pwdgrp::read_group ()
       snprintf (linebuf, sizeof (linebuf), "%s:%s:%lu:%s",
 		group_name, strbuf, myself->gid, cygheap->user.name ());
       debug_printf ("Completing /etc/group: %s", linebuf);
-      gr.add_line (linebuf);
+      add_line (linebuf);
     }
   static char NO_COPY pretty_ls[] = "????????::-1:";
   if (wincap.has_security ())
-    gr.add_line (pretty_ls);
+    add_line (pretty_ls);
   return;
 }
 
diff --git a/winsup/cygwin/include/sys/strace.h b/winsup/cygwin/include/sys/strace.h
index b43ab886f..7d69b8401 100644
--- a/winsup/cygwin/include/sys/strace.h
+++ b/winsup/cygwin/include/sys/strace.h
@@ -142,7 +142,7 @@ void strace_printf (unsigned, const char *func, const char *, ...);
     }))
 
 #define debug_printf(fmt, args...) strace_printf_wrap(DEBUG, fmt , ## args)
-#define paranoid_printf(fmt, args...) strace_printf_wrap(PARANOID, fmt , ## args)
+#define paranoid_printf(fmt, args...) strace_printf_wrap1(PARANOID, fmt , ## args)
 #define select_printf(fmt, args...) strace_printf_wrap(SELECT, fmt , ## args)
 #define sigproc_printf(fmt, args...) strace_printf_wrap(SIGP, fmt , ## args)
 #define syscall_printf(fmt, args...) strace_printf_wrap(SYSCALL, fmt , ## args)
diff --git a/winsup/cygwin/passwd.cc b/winsup/cygwin/passwd.cc
index 0b85f95bf..fe21299b2 100644
--- a/winsup/cygwin/passwd.cc
+++ b/winsup/cygwin/passwd.cc
@@ -37,28 +37,22 @@ static pwdgrp pr (passwd_buf);
 bool
 pwdgrp::parse_passwd ()
 {
-  int n;
 # define res (*passwd_buf)[curr_lines]
-  /* Allocate enough room for the passwd struct and all the strings
-     in it in one go */
+
   memset (&res, 0, sizeof (res));
+
   res.pw_name = next_str ();
   res.pw_passwd = next_str ();
 
-  n = next_int ();
-  if (n < 0)
+  if (res.pw_name == res.pw_passwd)
     return false;
-  res.pw_uid = n;
 
-  n = next_int ();
-  if (n < 0)
-    return false;
-  res.pw_gid = n;
-  res.pw_comment = 0;
+  (void) next_num (res.pw_uid);
+  (void) next_num (res.pw_gid);
+  res.pw_comment = NULL;
   res.pw_gecos = next_str ();
   res.pw_dir =  next_str ();
   res.pw_shell = next_str ();
-  curr_lines++;
   return true;
 # undef res
 }
@@ -69,8 +63,7 @@ pwdgrp::parse_passwd ()
 void
 pwdgrp::read_passwd ()
 {
-  if (!load ("/etc/passwd"))
-    debug_printf ("load failed");
+  load ("/etc/passwd");
 
   char strbuf[128] = "";
   bool searchentry = true;
@@ -79,7 +72,7 @@ pwdgrp::read_passwd ()
   if (wincap.has_security ())
     {
       static char NO_COPY pretty_ls[] = "????????:*:-1:-1:";
-      pr.add_line (pretty_ls);
+      add_line (pretty_ls);
       cygsid tu = cygheap->user.sid ();
       tu.string (strbuf);
       if (myself->uid == ILLEGAL_UID)
@@ -101,7 +94,7 @@ pwdgrp::read_passwd ()
 		myself->gid,
 		strbuf, getenv ("HOME") ?: "");
       debug_printf ("Completing /etc/passwd: %s", linebuf);
-      pr.add_line (linebuf);
+      add_line (linebuf);
     }
   return;
 }
diff --git a/winsup/cygwin/pwdgrp.h b/winsup/cygwin/pwdgrp.h
index aa7ab38c6..a5fdc1953 100644
--- a/winsup/cygwin/pwdgrp.h
+++ b/winsup/cygwin/pwdgrp.h
@@ -45,14 +45,28 @@ class pwdgrp
   void read_passwd ();
   void read_group ();
   char *add_line (char *);
+  char *raw_ptr () const {return lptr;}
   char *next_str (char = 0);
-  int next_int (char = 0);
-  void reparse (char *);
+  bool next_num (unsigned long&);
+  bool next_num (unsigned int& i)
+  {
+    unsigned long x;
+    bool res = next_num (x);
+    i = (unsigned int) x;
+    return res;
+  }
+  bool next_num (int& i)
+  {
+    unsigned long x;
+    bool res = next_num (x);
+    i = (int) x;
+    return res;
+  }
 
 public:
   int curr_lines;
 
-  bool load (const char *);
+  void load (const char *);
   void refresh (bool check = true)
   {
     if (!check && initialized)
diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc
index dcc9376ee..bc1680374 100644
--- a/winsup/cygwin/uinfo.cc
+++ b/winsup/cygwin/uinfo.cc
@@ -400,33 +400,23 @@ pwdgrp::next_str (char c)
   search[2] = c;
   char *res = lptr;
   char *p = strpbrk (lptr, search);
-  if (!p)
-    lptr = NULL;
-  else
+  if (p)
     {
-      lptr = (*p == '\n') ? NULL : p + 1;
+      lptr = (*p == '\n') ? p : p + 1;
       *p = '\0';
     }
   return res;
 }
 
-void
-pwdgrp::reparse (char *in_lptr)
+bool
+pwdgrp::next_num (unsigned long& n)
 {
-  lptr = in_lptr;
-}
-
-int
-pwdgrp::next_int (char c)
-{
-  char *p = next_str (c);
+  char *p = next_str ();
   if (!p)
     return -1;
   char *cp;
-  unsigned n = strtoul (p, &cp, 10);
-  if (p == cp)
-    return -1;
-  return n;
+  n = strtoul (p, &cp, 10);
+  return p != cp && !*cp;
 }
 
 char *
@@ -447,14 +437,19 @@ pwdgrp::add_line (char *eptr)
 	  max_lines += 10;
 	  *pwdgrp_buf = realloc (*pwdgrp_buf, max_lines * pwdgrp_buf_elem_size);
 	}
-      (void) (this->*parse) ();
+      if ((this->*parse) ())
+	curr_lines++;
     }
   return eptr;
 }
 
-bool
+void
 pwdgrp::load (const char *posix_fname)
 {
+  const char *res;
+  static const char failed[] = "failed";
+  static const char succeeded[] = "succeeded";
+
   if (buf)
     free (buf);
   buf = NULL;
@@ -464,26 +459,29 @@ pwdgrp::load (const char *posix_fname)
 
   paranoid_printf ("%s", posix_fname);
 
-  bool res;
   if (pc.error || !pc.exists () || !pc.isdisk () || pc.isdir ())
-    res = false;
+    {
+      paranoid_printf ("strange path_conv problem");
+      res = failed;
+    }
   else
     {
       HANDLE fh = CreateFile (pc, GENERIC_READ, wincap.shared (), NULL,
 			      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
       if (fh == INVALID_HANDLE_VALUE)
-	res = false;
+	res = failed;
       else
 	{
 	  DWORD size = GetFileSize (fh, NULL), read_bytes;
 	  buf = (char *) malloc (size + 1);
 	  if (!ReadFile (fh, buf, size, &read_bytes, NULL))
 	    {
+	      paranoid_printf ("ReadFile failed, %E");
 	      CloseHandle (fh);
 	      if (buf)
 		free (buf);
 	      buf = NULL;
-	      res = false;
+	      res = failed;
 	    }
 	  else
 	    {
@@ -494,11 +492,12 @@ pwdgrp::load (const char *posix_fname)
 	      while ((eptr = add_line (eptr)))
 		continue;
 	      debug_printf ("%s curr_lines %d", posix_fname, curr_lines);
-	      res = true;
+	      res = succeeded;
 	    }
 	}
     }
 
+  debug_printf ("load of %s %s", posix_fname, res);
   initialized = true;
-  return res;
+  return;
 }