about summary refs log tree commit diff
path: root/nss/nss_files
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2020-07-15 13:41:31 +0200
committerFlorian Weimer <fweimer@redhat.com>2020-07-21 07:33:50 +0200
commitbdee910e88006ae33dc83ac3d2c0708adb6627d0 (patch)
treec80cd50103ca8a413cccd589511abea88720e6b2 /nss/nss_files
parentd4b4586315974d2471486d41891aa9463a5838ad (diff)
downloadglibc-bdee910e88006ae33dc83ac3d2c0708adb6627d0.tar.gz
glibc-bdee910e88006ae33dc83ac3d2c0708adb6627d0.tar.xz
glibc-bdee910e88006ae33dc83ac3d2c0708adb6627d0.zip
nss: Add __nss_fgetent_r
And helper functions __nss_readline, __nss_readline_seek,
 __nss_parse_line_result.

This consolidates common code for handling overlong lines and
parse files.  Use the new functionality in internal_getent
in nss/nss_files/files-XXX.c.

Tested-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'nss/nss_files')
-rw-r--r--nss/nss_files/files-XXX.c79
1 files changed, 27 insertions, 52 deletions
diff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
index 9cc5137953..1db9e46127 100644
--- a/nss/nss_files/files-XXX.c
+++ b/nss/nss_files/files-XXX.c
@@ -135,10 +135,9 @@ internal_getent (FILE *stream, struct STRUCTURE *result,
 		 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
 		 EXTRA_ARGS_DECL)
 {
-  char *p;
   struct parser_data *data = (void *) buffer;
   size_t linebuflen = buffer + buflen - data->linebuffer;
-  int parse_result;
+  int saved_errno = errno;	/* Do not clobber errno on success.  */
 
   if (buflen < sizeof *data + 2)
     {
@@ -149,66 +148,42 @@ internal_getent (FILE *stream, struct STRUCTURE *result,
 
   while (true)
     {
-      ssize_t r = __libc_readline_unlocked
-	(stream, data->linebuffer, linebuflen);
-      if (r < 0)
-	{
-	  *errnop = errno;
-	  H_ERRNO_SET (NETDB_INTERNAL);
-	  if (*errnop == ERANGE)
-	    /* Request larger buffer.  */
-	    return NSS_STATUS_TRYAGAIN;
-	  else
-	    /* Other read failure.  */
-	    return NSS_STATUS_UNAVAIL;
-	}
-      else if (r == 0)
+      off64_t original_offset;
+      int ret = __nss_readline (stream, data->linebuffer, linebuflen,
+				&original_offset);
+      if (ret == ENOENT)
 	{
 	  /* End of file.  */
 	  H_ERRNO_SET (HOST_NOT_FOUND);
+	  __set_errno (saved_errno);
 	  return NSS_STATUS_NOTFOUND;
 	}
-
-      /* Everything OK.  Now skip leading blanks.  */
-      p = data->linebuffer;
-      while (isspace (*p))
-	++p;
-
-      /* Ignore empty and comment lines.  */
-      if (*p == '\0' || *p == '#')
-	continue;
-
-      /* Parse the line.   */
-      *errnop = EINVAL;
-      parse_result = parse_line (p, result, data, buflen, errnop EXTRA_ARGS);
-
-      if (parse_result == -1)
+      else if (ret == 0)
 	{
-	  if (*errnop == ERANGE)
+	  ret = __nss_parse_line_result (stream, original_offset,
+					 parse_line (data->linebuffer,
+						     result, data, buflen,
+						     errnop EXTRA_ARGS));
+	  if (ret == 0)
 	    {
-	      /* Return to the original file position at the beginning
-		 of the line, so that the next call can read it again
-		 if necessary.  */
-	      if (__fseeko64 (stream, -r, SEEK_CUR) != 0)
-		{
-		  if (errno == ERANGE)
-		    *errnop = EINVAL;
-		  else
-		    *errnop = errno;
-		  H_ERRNO_SET (NETDB_INTERNAL);
-		  return NSS_STATUS_UNAVAIL;
-		}
+	      /* Line has been parsed successfully.  */
+	      __set_errno (saved_errno);
+	      return NSS_STATUS_SUCCESS;
 	    }
-	  H_ERRNO_SET (NETDB_INTERNAL);
-	  return NSS_STATUS_TRYAGAIN;
+	  else if (ret == EINVAL)
+	    /* If it is invalid, loop to get the next line of the file
+	       to parse.  */
+	    continue;
 	}
 
-      /* Return the data if parsed successfully.  */
-      if (parse_result != 0)
-	return NSS_STATUS_SUCCESS;
-
-      /* If it is invalid, loop to get the next line of the file to
-	 parse.  */
+      *errnop = ret;
+      H_ERRNO_SET (NETDB_INTERNAL);
+      if (ret == ERANGE)
+	/* Request larger buffer.  */
+	return NSS_STATUS_TRYAGAIN;
+      else
+	/* Other read failure.  */
+	return NSS_STATUS_UNAVAIL;
     }
 }