[Pkg-shadow-devel] fflush() after prompts patch

Alexander Gattin xrgtn at yandex.ru
Sun Oct 23 16:28:02 UTC 2005


Hi!

Finally I have found a time to try whether the shadow
builds OK with extended fflush() patch applied. It
does.

So, here it is attached.

Few comments:
1) I extracted yes_or_no() routine which is common to
   both pwck.c and grpck.c, into separate file
   libmisc/yesno.c
2) I added read_only as parameter to the routine,
   because IMHO it's not good to declare it as "extern int"
   in library in order to link later to real read_only,
   which is declared in pwck.c and grpck.c (must be
   declared as non-static then). Global variables are
   evil.
3) the patch includes modifications to libmisc/Makefile.am
   in order to cope with added libmisc/yesno.c file
4) you should not forget to `cvs add libmisc/yesno.c`

Actually I didn't like introducing new file, but rationale
for doing this is as usual -- if smth. gets fixed
inside the yes_or_no() routine in e.g. pwck.c, there's a
chance that the second copy of it (in grpck.c) can miss the
fix.

-- 
WBR,
xrgtn
-------------- next part --------------
--- /dev/null	2005-07-03 15:30:00.000000000 +0300
+++ libmisc/yesno.c	2005-10-23 17:37:50.000000000 +0300
@@ -0,0 +1,41 @@
+/*
+ * Common code for yes/no prompting
+ * 
+ * Used by pwck.c and grpck.c
+ */
+
+#include <config.h>	/* configuration parameters like e.g. ENABLE_NLS */
+
+#ident "$Id$"
+
+#include <stdio.h>	/* printf(), fflush() & fgets() */
+#include "defines.h"	/* _() macro */
+
+/*
+ * yes_or_no - get answer to question from the user
+ */
+int yes_or_no (int read_only)
+{
+	char buf[80];
+
+	/*
+	 * In read-only mode all questions are answered "no".
+	 */
+	if (read_only) {
+		printf (_("No\n"));
+		return 0;
+	}
+
+	/*
+	 * Typically, there's a prompt on stdout, sometimes unflushed.
+	 */
+	fflush (stdout);
+
+	/*
+	 * Get a line and see what the first character is.
+	 */
+	if (fgets (buf, sizeof buf, stdin))
+		return buf[0] == 'y' || buf[0] == 'Y';
+
+	return 0;
+}
Index: src/grpck.c
===================================================================
RCS file: /cvsroot/shadow/src/grpck.c,v
retrieving revision 1.28
diff -u -r1.28 grpck.c
--- src/grpck.c	7 Sep 2005 15:00:45 -0000	1.28
+++ src/grpck.c	23 Oct 2005 14:59:24 -0000
@@ -49,6 +49,8 @@
 extern struct commonio_entry *__sgr_get_head (void);
 #endif
 
+extern int yes_or_no (int);
+
 /*
  * Exit codes
  */
@@ -73,7 +75,6 @@
 
 /* local function prototypes */
 static void usage (void);
-static int yes_or_no (void);
 static void delete_member (char **, const char *);
 
 /*
@@ -90,30 +91,6 @@
 }
 
 /*
- * yes_or_no - get answer to question from the user
- */
-static int yes_or_no (void)
-{
-	char buf[80];
-
-	/*
-	 * In read-only mode all questions are answered "no".
-	 */
-	if (read_only) {
-		printf (_("No\n"));
-		return 0;
-	}
-
-	/*
-	 * Get a line and see what the first character is.
-	 */
-	if (fgets (buf, sizeof buf, stdin))
-		return buf[0] == 'y' || buf[0] == 'Y';
-
-	return 0;
-}
-
-/*
  * delete_member - delete an entry in a list of members
  */
 static void delete_member (char **list, const char *member)
@@ -295,7 +272,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			/*
@@ -351,7 +328,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_gr;
 		}
 
@@ -387,7 +364,7 @@
 				grp->gr_name, grp->gr_mem[i]);
 			printf (_("delete member `%s'? "), grp->gr_mem[i]);
 
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			SYSLOG ((LOG_INFO, "delete member `%s' group `%s'",
@@ -426,7 +403,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			/*
@@ -482,7 +459,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_sg;
 		}
 
@@ -493,7 +470,7 @@
 			printf (_("no matching group file entry\n"));
 			printf (_("delete line `%s'? "), sge->line);
 			errors++;
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_sg;
 		}
 
@@ -514,7 +491,7 @@
 			printf (_("delete administrative member `%s'? "),
 				sgr->sg_adm[i]);
 
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			SYSLOG ((LOG_INFO,
@@ -541,7 +518,7 @@
 				sgr->sg_name, sgr->sg_mem[i]);
 			printf (_("delete member `%s'? "), sgr->sg_mem[i]);
 
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			SYSLOG ((LOG_INFO,
Index: src/pwck.c
===================================================================
RCS file: /cvsroot/shadow/src/pwck.c,v
retrieving revision 1.32
diff -u -r1.32 pwck.c
--- src/pwck.c	7 Sep 2005 15:00:45 -0000	1.32
+++ src/pwck.c	23 Oct 2005 14:59:25 -0000
@@ -47,6 +47,8 @@
 extern void __spw_del_entry (const struct commonio_entry *);
 extern struct commonio_entry *__spw_get_head (void);
 
+extern int yes_or_no (int);
+
 /*
  * Exit codes
  */
@@ -71,7 +73,6 @@
 
 /* local function prototypes */
 static void usage (void);
-static int yes_or_no (void);
 
 /*
  * usage - print syntax message and exit
@@ -84,31 +85,6 @@
 }
 
 /*
- * yes_or_no - get answer to question from the user
- */
-static int yes_or_no (void)
-{
-	char buf[80];
-
-	/*
-	 * In read-only mode all questions are answered "no".
-	 */
-
-	if (read_only) {
-		printf (_("No\n"));
-		return 0;
-	}
-
-	/*
-	 * Get a line and see what the first character is.
-	 */
-	if (fgets (buf, sizeof buf, stdin))
-		return buf[0] == 'y' || buf[0] == 'Y';
-
-	return 0;
-}
-
-/*
  * pwck - verify password file integrity
  */
 int main (int argc, char **argv)
@@ -259,7 +235,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			/*
@@ -314,7 +290,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_pw;
 		}
 
@@ -399,7 +375,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (!yes_or_no ())
+			if (!yes_or_no (read_only))
 				continue;
 
 			/*
@@ -454,7 +430,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_spw;
 		}
 
@@ -474,7 +450,7 @@
 			/*
 			 * prompt the user to delete the entry or not
 			 */
-			if (yes_or_no ())
+			if (yes_or_no (read_only))
 				goto delete_spw;
 		}
 
Index: libmisc/Makefile.am
===================================================================
RCS file: /cvsroot/shadow/libmisc/Makefile.am,v
retrieving revision 1.25
diff -u -r1.25 Makefile.am
--- libmisc/Makefile.am	5 Sep 2005 16:21:37 -0000	1.25
+++ libmisc/Makefile.am	23 Oct 2005 14:59:25 -0000
@@ -49,4 +49,5 @@
 	ulimit.c \
 	utmp.c \
 	valid.c \
-	xmalloc.c
+	xmalloc.c \
+	yesno.c
Index: libmisc/fields.c
===================================================================
RCS file: /cvsroot/shadow/libmisc/fields.c,v
retrieving revision 1.7
diff -u -r1.7 fields.c
--- libmisc/fields.c	31 Aug 2005 17:24:57 -0000	1.7
+++ libmisc/fields.c	23 Oct 2005 14:59:25 -0000
@@ -71,6 +71,7 @@
 		maxsize = sizeof (newf);
 
 	printf ("\t%s [%s]: ", prompt, buf);
+	fflush (stdout);
 	if (fgets (newf, maxsize, stdin) != newf)
 		return;
 


More information about the Pkg-shadow-devel mailing list