]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
support: Change xgetline to return 0 on EOF
authorFlorian Weimer <fweimer@redhat.com>
Thu, 2 Apr 2020 15:09:36 +0000 (17:09 +0200)
committerFlorian Weimer <fweimer@redhat.com>
Fri, 3 Apr 2020 14:26:10 +0000 (16:26 +0200)
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>
support/support_process_state.c
support/xgetline.c
support/xstdio.h

index 76dc798728ece0d9b20adb150c47785a83c14290..e303c78fc874b2f9b9111fe5fba6f9371228bc1f 100644 (file)
@@ -59,7 +59,7 @@ support_process_state_wait (pid_t pid, enum support_process_state state)
   for (;;)
     {
       char cur_state = -1;
-      while (xgetline (&line, &linesiz, fstatus) != -1)
+      while (xgetline (&line, &linesiz, fstatus) > 0)
        if (strncmp (line, "State:", strlen ("State:")) == 0)
          {
            TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1);
index 180bc2db95a9c5d44f560f5b8c59ace205788a5b..d91c09ac108b4c75376ea138b87615e63b5770f6 100644 (file)
 
 #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;
 }
index 143957b6a8d367e0b2315c9e190fdebcf85f7f4a..807c3afc43deeb7a3e533f769a81eb0d8136ea6f 100644 (file)
@@ -27,7 +27,10 @@ __BEGIN_DECLS
 FILE *xfopen (const char *path, const char *mode);
 void xfclose (FILE *);
 
-ssize_t xgetline (char **lineptr, size_t *n, FILE *stream);
+/* Read a line from FP, using getline.  *BUFFER must be NULL, or a
+   heap-allocated pointer of *LENGTH bytes.  Return the number of
+   bytes in the line if a line was read, or 0 on EOF.  */
+size_t xgetline (char **lineptr, size_t *n, FILE *stream);
 
 __END_DECLS