about summary refs log tree commit diff
path: root/support/xgetline.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2020-04-02 17:09:36 +0200
committerFlorian Weimer <fweimer@redhat.com>2020-04-03 16:26:10 +0200
commitcea56af185eae45b1f0963351e3d4daa1cbde521 (patch)
tree2fca166caab3aaf9ce9f07bb719d0546cff3e4d2 /support/xgetline.c
parent17fd707f88c5531972c980a4f4567ba6c7f84067 (diff)
downloadglibc-cea56af185eae45b1f0963351e3d4daa1cbde521.tar.gz
glibc-cea56af185eae45b1f0963351e3d4daa1cbde521.tar.xz
glibc-cea56af185eae45b1f0963351e3d4daa1cbde521.zip
support: Change xgetline to return 0 on EOF
The advantage is that the buffer will always contain the number
of characters as returned from the function, which allows one to use
a sequence like

  /* No more audit module output.  */
  line_length = xgetline (&buffer, &buffer_length, fp);
  TEST_COMPARE_BLOB ("", 0, buffer, line_length);

to check for an expected EOF, while also reporting any unexpected
extra data encountered.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'support/xgetline.c')
-rw-r--r--support/xgetline.c22
1 files changed, 14 insertions, 8 deletions
diff --git a/support/xgetline.c b/support/xgetline.c
index 180bc2db95..d91c09ac10 100644
--- a/support/xgetline.c
+++ b/support/xgetline.c
@@ -18,16 +18,22 @@
 
 #include <support/xstdio.h>
 #include <support/check.h>
-#include <errno.h>
 
-ssize_t
+size_t
 xgetline (char **lineptr, size_t *n, FILE *stream)
 {
-  int old_errno = errno;
-  errno = 0;
-  size_t ret = getline (lineptr, n, stream);
-  if (!feof (stream) && ferror (stream))
-    FAIL_EXIT1 ("getline failed: %m");
-  errno = old_errno;
+  TEST_VERIFY (!ferror (stream));
+  ssize_t ret = getline (lineptr, n, stream);
+  if (ferror (stream))
+    {
+      TEST_VERIFY (ret < 0);
+      FAIL_EXIT1 ("getline: %m");
+    }
+  if (feof (stream))
+    {
+      TEST_VERIFY (ret <= 0);
+      return 0;
+    }
+  TEST_VERIFY (ret > 0);
   return ret;
 }