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