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