446cf2
commit cea56af185eae45b1f0963351e3d4daa1cbde521
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Thu Apr 2 17:09:36 2020 +0200
446cf2
446cf2
    support: Change xgetline to return 0 on EOF
446cf2
    
446cf2
    The advantage is that the buffer will always contain the number
446cf2
    of characters as returned from the function, which allows one to use
446cf2
    a sequence like
446cf2
    
446cf2
      /* No more audit module output.  */
446cf2
      line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
      TEST_COMPARE_BLOB ("", 0, buffer, line_length);
446cf2
    
446cf2
    to check for an expected EOF, while also reporting any unexpected
446cf2
    extra data encountered.
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
diff --git a/support/support_process_state.c b/support/support_process_state.c
446cf2
index 76dc798728ece0d9..e303c78fc874b2f9 100644
446cf2
--- a/support/support_process_state.c
446cf2
+++ b/support/support_process_state.c
446cf2
@@ -59,7 +59,7 @@ support_process_state_wait (pid_t pid, enum support_process_state state)
446cf2
   for (;;)
446cf2
     {
446cf2
       char cur_state = -1;
446cf2
-      while (xgetline (&line, &linesiz, fstatus) != -1)
446cf2
+      while (xgetline (&line, &linesiz, fstatus) > 0)
446cf2
 	if (strncmp (line, "State:", strlen ("State:")) == 0)
446cf2
 	  {
446cf2
 	    TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1);
446cf2
diff --git a/support/xgetline.c b/support/xgetline.c
446cf2
index 180bc2db95a9c5d4..d91c09ac108b4c75 100644
446cf2
--- a/support/xgetline.c
446cf2
+++ b/support/xgetline.c
446cf2
@@ -18,16 +18,22 @@
446cf2
 
446cf2
 #include <support/xstdio.h>
446cf2
 #include <support/check.h>
446cf2
-#include <errno.h>
446cf2
 
446cf2
-ssize_t
446cf2
+size_t
446cf2
 xgetline (char **lineptr, size_t *n, FILE *stream)
446cf2
 {
446cf2
-  int old_errno = errno;
446cf2
-  errno = 0;
446cf2
-  size_t ret = getline (lineptr, n, stream);
446cf2
-  if (!feof (stream) && ferror (stream))
446cf2
-    FAIL_EXIT1 ("getline failed: %m");
446cf2
-  errno = old_errno;
446cf2
+  TEST_VERIFY (!ferror (stream));
446cf2
+  ssize_t ret = getline (lineptr, n, stream);
446cf2
+  if (ferror (stream))
446cf2
+    {
446cf2
+      TEST_VERIFY (ret < 0);
446cf2
+      FAIL_EXIT1 ("getline: %m");
446cf2
+    }
446cf2
+  if (feof (stream))
446cf2
+    {
446cf2
+      TEST_VERIFY (ret <= 0);
446cf2
+      return 0;
446cf2
+    }
446cf2
+  TEST_VERIFY (ret > 0);
446cf2
   return ret;
446cf2
 }
446cf2
diff --git a/support/xstdio.h b/support/xstdio.h
446cf2
index 9446b1f27b0f881e..36071cf78822ec8d 100644
446cf2
--- a/support/xstdio.h
446cf2
+++ b/support/xstdio.h
446cf2
@@ -27,7 +27,10 @@ __BEGIN_DECLS
446cf2
 FILE *xfopen (const char *path, const char *mode);
446cf2
 void xfclose (FILE *);
446cf2
 
446cf2
-ssize_t xgetline (char **lineptr, size_t *n, FILE *stream);
446cf2
+/* Read a line from FP, using getline.  *BUFFER must be NULL, or a
446cf2
+   heap-allocated pointer of *LENGTH bytes.  Return the number of
446cf2
+   bytes in the line if a line was read, or 0 on EOF.  */
446cf2
+size_t xgetline (char **lineptr, size_t *n, FILE *stream);
446cf2
 
446cf2
 __END_DECLS
446cf2