about summary refs log tree commit diff
path: root/nis
diff options
context:
space:
mode:
Diffstat (limited to 'nis')
-rw-r--r--nis/nisplus-parser.h10
-rw-r--r--nis/nss_compat/compat-grp.c155
-rw-r--r--nis/nss_compat/compat-pwd.c236
-rw-r--r--nis/nss_compat/compat-spwd.c161
-rw-r--r--nis/nss_nis/nis-alias.c36
-rw-r--r--nis/nss_nis/nis-ethers.c31
-rw-r--r--nis/nss_nis/nis-grp.c34
-rw-r--r--nis/nss_nis/nis-hosts.c46
-rw-r--r--nis/nss_nis/nis-netgrp.c7
-rw-r--r--nis/nss_nis/nis-network.c32
-rw-r--r--nis/nss_nis/nis-proto.c31
-rw-r--r--nis/nss_nis/nis-publickey.c11
-rw-r--r--nis/nss_nis/nis-pwd.c34
-rw-r--r--nis/nss_nis/nis-rpc.c24
-rw-r--r--nis/nss_nis/nis-service.c38
-rw-r--r--nis/nss_nis/nis-spwd.c27
-rw-r--r--nis/nss_nisplus/nisplus-alias.c73
-rw-r--r--nis/nss_nisplus/nisplus-ethers.c96
-rw-r--r--nis/nss_nisplus/nisplus-grp.c59
-rw-r--r--nis/nss_nisplus/nisplus-hosts.c131
-rw-r--r--nis/nss_nisplus/nisplus-netgrp.c16
-rw-r--r--nis/nss_nisplus/nisplus-network.c125
-rw-r--r--nis/nss_nisplus/nisplus-parser.c96
-rw-r--r--nis/nss_nisplus/nisplus-proto.c93
-rw-r--r--nis/nss_nisplus/nisplus-publickey.c60
-rw-r--r--nis/nss_nisplus/nisplus-pwd.c78
-rw-r--r--nis/nss_nisplus/nisplus-rpc.c103
-rw-r--r--nis/nss_nisplus/nisplus-service.c99
-rw-r--r--nis/nss_nisplus/nisplus-spwd.c50
29 files changed, 1173 insertions, 819 deletions
diff --git a/nis/nisplus-parser.h b/nis/nisplus-parser.h
index b24ca46507..2d54edba28 100644
--- a/nis/nisplus-parser.h
+++ b/nis/nisplus-parser.h
@@ -17,18 +17,18 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#ifndef __NISPLUS_PARSER_H_
-#define __NISPLUS_PARSER_H_ 1
+#ifndef __NISPLUS_PARSER_H
+#define __NISPLUS_PARSER_H	1
 
 #include <pwd.h>
 #include <grp.h>
 #include <shadow.h>
 
 extern int _nss_nisplus_parse_pwent (nis_result *, struct passwd *,
-				     char *, size_t);
+				     char *, size_t, int *);
 extern int _nss_nisplus_parse_grent (nis_result *, u_long, struct group *,
-				     char *, size_t);
+				     char *, size_t, int *);
 extern int _nss_nisplus_parse_spent (nis_result *, struct spwd *,
-				     char *, size_t);
+				     char *, size_t, int *);
 
 #endif
diff --git a/nis/nss_compat/compat-grp.c b/nis/nss_compat/compat-grp.c
index 6d4ca25e5d..a586d1fbbf 100644
--- a/nis/nss_compat/compat-grp.c
+++ b/nis/nss_compat/compat-grp.c
@@ -225,7 +225,7 @@ _nss_compat_endgrent (void)
 
 static enum nss_status
 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
-		   size_t buflen)
+		   size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain;
@@ -286,13 +286,14 @@ getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
       while (isspace (*p))
 	++p;
 
-      if ((parse_res = _nss_files_parse_grent (p, result, data, buflen)) == -1)
+      parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (ent->oldkey);
 	  ent->oldkey = save_oldkey;
 	  ent->oldkeylen = save_oldlen;
 	  ent->nis_first = save_nis_first;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -312,7 +313,7 @@ getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
 
 static enum nss_status
 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
-                       size_t buflen)
+                       size_t buflen, int *errnop)
 {
   int parse_res;
 
@@ -347,13 +348,14 @@ getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
 	      return niserr2nss (ent->result->status);
             }
         }
-      if ((parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
-						 buffer, buflen)) == -1)
+      parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
+					    buffer, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (ent->result);
 	  ent->result = save_oldres;
 	  ent->nis_first = save_nis_first;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -374,7 +376,7 @@ getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
 /* This function handle the +group entrys in /etc/group */
 static enum nss_status
 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
-		    size_t buflen)
+		    size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   int parse_res;
@@ -393,11 +395,12 @@ getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
           nis_freeresult (res);
           return status;
         }
-      if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
+					    errnop);
+      if (parse_res == -1)
 	{
-	  __set_errno (ERANGE);
 	  nis_freeresult (res);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (res);
@@ -408,21 +411,25 @@ getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
       int outvallen;
 
       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (yp_match (domain, "group.byname", name, strlen (name),
 		    &outval, &outvallen) != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       p = strncpy (buffer, outval,
                    buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
       free (outval);
       while (isspace (*p))
         p++;
-      if ((parse_res = _nss_files_parse_grent (p, result, data, buflen)) == -1)
-	{
-	  __set_errno (ERANGE);
-	  return NSS_STATUS_TRYAGAIN;
-	}
+      parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
+	return NSS_STATUS_TRYAGAIN;
     }
 
   if (parse_res)
@@ -434,7 +441,7 @@ getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
 
 static enum nss_status
 getgrent_next_file (struct group *result, ent_t *ent,
-		    char *buffer, size_t buflen)
+		    char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   while (1)
@@ -453,7 +460,8 @@ getgrent_next_file (struct group *result, ent_t *ent,
 		return NSS_STATUS_NOTFOUND;
 	      else
 		{
-		  __set_errno (ERANGE);
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
 		  return NSS_STATUS_TRYAGAIN;
 		}
 	    }
@@ -468,13 +476,14 @@ getgrent_next_file (struct group *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
       /* Parse the line.  If it is invalid, loop to
          get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -500,14 +509,22 @@ getgrent_next_file (struct group *result, ent_t *ent,
 	     /etc/group */
 	  blacklist_store_name (&result->gr_name[1], ent);
 	  status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
-				       buflen);
+				       buflen, errnop);
           if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
             break;
           else
             if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
               continue;
             else
-              return status;
+	      {
+		if (status == NSS_STATUS_TRYAGAIN)
+		  {
+		    /* The parser ran out of space.  */
+		    fsetpos (ent->stream, &pos);
+		    *errnop = ERANGE;
+		  }
+		return status;
+	      }
 	}
 
       /* +:... */
@@ -517,9 +534,9 @@ getgrent_next_file (struct group *result, ent_t *ent,
 	  ent->nis_first = TRUE;
 
 	  if (use_nisplus)
-	    return getgrent_next_nisplus (result, ent, buffer, buflen);
+	    return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
 	  else
-	    return getgrent_next_nis (result, ent, buffer, buflen);
+	    return getgrent_next_nis (result, ent, buffer, buflen, errnop);
 	}
     }
 
@@ -529,21 +546,22 @@ getgrent_next_file (struct group *result, ent_t *ent,
 
 static enum nss_status
 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
-		     size_t buflen)
+		     size_t buflen, int *errnop)
 {
   if (ent->nis)
     {
       if (use_nisplus)
-	return getgrent_next_nisplus (gr, ent, buffer, buflen);
+	return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
       else
-	return getgrent_next_nis (gr, ent, buffer, buflen);
+	return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
     }
   else
-    return getgrent_next_file (gr, ent, buffer, buflen);
+    return getgrent_next_file (gr, ent, buffer, buflen, errnop);
 }
 
 enum nss_status
-_nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
+_nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
 
@@ -554,7 +572,7 @@ _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
     status = internal_setgrent (&ext_ent);
 
   if (status == NSS_STATUS_SUCCESS)
-    status = internal_getgrent_r (grp, &ext_ent, buffer, buflen);
+    status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -564,7 +582,7 @@ _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
 static enum nss_status
 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   while (1)
@@ -583,7 +601,8 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
 		return NSS_STATUS_NOTFOUND;
 	      else
 		{
-		  __set_errno (ERANGE);
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
 		  return NSS_STATUS_TRYAGAIN;
 		}
 	    }
@@ -598,13 +617,14 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
       /* Parse the line.  If it is invalid, loop to
          get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -618,8 +638,7 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
 	}
 
       /* -group */
-      if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
-	  && result->gr_name[1] != '@')
+      if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
 	{
 	  if (strcmp (&result->gr_name[1], name) == 0)
 	    return NSS_STATUS_NOTFOUND;
@@ -628,14 +647,14 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
 	}
 
       /* +group */
-      if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
-	  && result->gr_name[1] != '@')
+      if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
 	{
 	  if (strcmp (name, &result->gr_name[1]) == 0)
 	    {
 	      enum nss_status status;
 
-	      status = getgrnam_plusgroup (name, result, buffer, buflen);
+	      status = getgrnam_plusgroup (name, result, buffer, buflen,
+					   errnop);
 	      if (status == NSS_STATUS_RETURN)
 		/* We couldn't parse the entry */
 		continue;
@@ -648,7 +667,7 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
 	{
 	  enum nss_status status;
 
-	  status = getgrnam_plusgroup (name, result, buffer, buflen);
+	  status = getgrnam_plusgroup (name, result, buffer, buflen, errnop);
 	  if (status == NSS_STATUS_RETURN)
 	    /* We couldn't parse the entry */
 	    continue;
@@ -662,7 +681,7 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
 
 enum nss_status
 _nss_compat_getgrnam_r (const char *name, struct group *grp,
-			char *buffer, size_t buflen)
+			char *buffer, size_t buflen, int *errnop)
 {
   ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
   enum nss_status status;
@@ -679,7 +698,7 @@ _nss_compat_getgrnam_r (const char *name, struct group *grp,
   if (status != NSS_STATUS_SUCCESS)
     return status;
 
-  status = internal_getgrnam_r (name, grp, &ent, buffer, buflen);
+  status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
 
   internal_endgrent (&ent);
 
@@ -689,7 +708,7 @@ _nss_compat_getgrnam_r (const char *name, struct group *grp,
 /* This function handle the + entry in /etc/group */
 static enum nss_status
 getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
-		    size_t buflen)
+		    size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   int parse_res;
@@ -709,10 +728,10 @@ getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
           return status;
         }
       if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
-						 buflen)) == -1)
+						 buflen, errnop)) == -1)
 	{
-	  __set_errno (ERANGE);
 	  nis_freeresult (res);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (res);
@@ -724,23 +743,27 @@ getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
       int outvallen;
 
       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       snprintf (buf, sizeof (buf), "%d", gid);
 
       if (yp_match (domain, "group.bygid", buf, strlen (buf),
 		    &outval, &outvallen) != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       p = strncpy (buffer, outval,
                    buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
       free (outval);
       while (isspace (*p))
         p++;
-      if ((parse_res = _nss_files_parse_grent (p, result, data, buflen)) == -1)
-	{
-	  __set_errno (ERANGE);
-	  return NSS_STATUS_TRYAGAIN;
-	}
+      parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
+	return NSS_STATUS_TRYAGAIN;
     }
 
   if (parse_res)
@@ -753,7 +776,7 @@ getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
 static enum nss_status
 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   while (1)
@@ -772,7 +795,8 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
 		return NSS_STATUS_NOTFOUND;
 	      else
 		{
-		  __set_errno (ERANGE);
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
 		  return NSS_STATUS_TRYAGAIN;
 		}
 	    }
@@ -787,13 +811,14 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
       /* Parse the line.  If it is invalid, loop to
          get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -807,16 +832,14 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
 	}
 
       /* -group */
-      if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
-	  && result->gr_name[1] != '@')
+      if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
 	{
           blacklist_store_name (&result->gr_name[1], ent);
           continue;
 	}
 
       /* +group */
-      if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
-	  && result->gr_name[1] != '@')
+      if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
 	{
 	  enum nss_status status;
 
@@ -824,7 +847,7 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
              /etc/group */
           blacklist_store_name (&result->gr_name[1], ent);
 	  status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
-				      buflen);
+				      buflen, errnop);
 	  if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
 	    break;
 	  else
@@ -835,7 +858,7 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
 	{
 	  enum nss_status status;
 
-	  status = getgrgid_plusgroup (gid, result, buffer, buflen);
+	  status = getgrgid_plusgroup (gid, result, buffer, buflen, errnop);
 	  if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
 	    return NSS_STATUS_NOTFOUND;
 	  else
@@ -848,7 +871,7 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
 
 enum nss_status
 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
-			char *buffer, size_t buflen)
+			char *buffer, size_t buflen, int *errnop)
 {
   ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
   enum nss_status status;
@@ -862,7 +885,7 @@ _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
   if (status != NSS_STATUS_SUCCESS)
     return status;
 
-  status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen);
+  status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
 
   internal_endgrent (&ent);
 
diff --git a/nis/nss_compat/compat-pwd.c b/nis/nss_compat/compat-pwd.c
index 38a7e2f205..c8e6f9cc8d 100644
--- a/nis/nss_compat/compat-pwd.c
+++ b/nis/nss_compat/compat-pwd.c
@@ -338,7 +338,7 @@ _nss_compat_endpwent (void)
 
 static enum nss_status
 getpwent_next_nis_netgr (const char *name, struct passwd *result, ent_t *ent,
-			 char *group, char *buffer, size_t buflen)
+			 char *group, char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *ypdomain, *host, *user, *domain, *outval, *p, *p2;
@@ -367,7 +367,8 @@ getpwent_next_nis_netgr (const char *name, struct passwd *result, ent_t *ent,
 
       saved_cursor = ent->netgrdata.cursor;
       status = __internal_getnetgrent_r (&host, &user, &domain,
-					 &ent->netgrdata, buffer, buflen);
+					 &ent->netgrdata, buffer, buflen,
+					 errnop);
       if (status != 1)
 	{
 	  __internal_endnetgrent (&ent->netgrdata);
@@ -395,7 +396,7 @@ getpwent_next_nis_netgr (const char *name, struct passwd *result, ent_t *ent,
       p2len = pwd_need_buflen (&ent->pwd);
       if (p2len > buflen)
 	{
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       p2 = buffer + (buflen - p2len);
@@ -404,7 +405,8 @@ getpwent_next_nis_netgr (const char *name, struct passwd *result, ent_t *ent,
       while (isspace (*p))
 	p++;
       free (outval);
-      if ((parse_res = _nss_files_parse_pwent (p, result, data, buflen)) == -1)
+      parse_res = _nss_files_parse_pwent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  ent->netgrdata.cursor = saved_cursor;
 	  return NSS_STATUS_TRYAGAIN;
@@ -426,7 +428,7 @@ getpwent_next_nis_netgr (const char *name, struct passwd *result, ent_t *ent,
 static enum nss_status
 getpwent_next_nisplus_netgr (const char *name, struct passwd *result,
 			     ent_t *ent, char *group, char *buffer,
-			     size_t buflen)
+			     size_t buflen, int *errnop)
 {
   char *ypdomain, *host, *user, *domain, *p2;
   int status, parse_res;
@@ -456,7 +458,8 @@ getpwent_next_nisplus_netgr (const char *name, struct passwd *result,
 
       saved_cursor = ent->netgrdata.cursor;
       status = __internal_getnetgrent_r (&host, &user, &domain,
-					 &ent->netgrdata, buffer, buflen);
+					 &ent->netgrdata, buffer, buflen,
+					 errnop);
       if (status != 1)
 	{
 	  __internal_endnetgrent (&ent->netgrdata);
@@ -479,7 +482,7 @@ getpwent_next_nisplus_netgr (const char *name, struct passwd *result,
       p2len = pwd_need_buflen (&ent->pwd);
       if (p2len > buflen)
 	{
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       p2 = buffer + (buflen - p2len);
@@ -494,11 +497,13 @@ getpwent_next_nisplus_netgr (const char *name, struct passwd *result,
 	  nis_freeresult (nisres);
 	  continue;
 	}
-      if ((parse_res = _nss_nisplus_parse_pwent (nisres, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_pwent (nisres, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (nisres);
 	  ent->netgrdata.cursor = saved_cursor;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (nisres);
@@ -518,7 +523,7 @@ getpwent_next_nisplus_netgr (const char *name, struct passwd *result,
 
 static enum nss_status
 getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
-		       size_t buflen)
+		       size_t buflen, int *errnop)
 {
   int parse_res;
   size_t p2len;
@@ -527,7 +532,7 @@ getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
   p2len = pwd_need_buflen (&ent->pwd);
   if (p2len > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p2 = buffer + (buflen - p2len);
@@ -542,7 +547,7 @@ getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
 	  saved_first = TRUE;
 	  saved_res = ent->result;
 
-	  ent->result = nis_first_entry(pwdtable);
+	  ent->result = nis_first_entry (pwdtable);
           if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
 	    {
 	      ent->nis = 0;
@@ -555,7 +560,7 @@ getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
 	{
 	  nis_result *res;
 
-	  res = nis_next_entry(pwdtable, &ent->result->cookie);
+	  res = nis_next_entry (pwdtable, &ent->result->cookie);
 	  saved_res = ent->result;
 	  saved_first = FALSE;
 	  ent->result = res;
@@ -567,13 +572,14 @@ getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
 	      return niserr2nss (ent->result->status);
 	    }
 	}
-      if ((parse_res = _nss_nisplus_parse_pwent (ent->result, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_pwent (ent->result, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (ent->result);
 	  ent->result = saved_res;
 	  ent->first = saved_first;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -595,7 +601,7 @@ getpwent_next_nisplus (struct passwd *result, ent_t *ent, char *buffer,
 
 static enum nss_status
 getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
-		   size_t buflen)
+		   size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *outkey, *outval, *p, *p2;
@@ -612,7 +618,7 @@ getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
   p2len = pwd_need_buflen (&ent->pwd);
   if (p2len > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p2 = buffer + (buflen - p2len);
@@ -666,13 +672,14 @@ getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
 
       while (isspace (*p))
 	++p;
-      if ((parse_res = _nss_files_parse_pwent (p, result, data, buflen)) == -1)
+      parse_res = _nss_files_parse_pwent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (ent->oldkey);
 	  ent->oldkey = saved_oldkey;
 	  ent->oldkeylen = saved_oldlen;
 	  ent->first = saved_first;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -680,8 +687,8 @@ getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
 	  if (!saved_first)
 	    free (saved_oldkey);
 	}
-      if (parse_res &&
-	  in_blacklist (result->pw_name, strlen (result->pw_name), ent))
+      if (parse_res
+	  && in_blacklist (result->pw_name, strlen (result->pw_name), ent))
 	parse_res = 0;
     }
   while (!parse_res);
@@ -694,7 +701,7 @@ getpwent_next_nis (struct passwd *result, ent_t *ent, char *buffer,
 /* This function handle the +user entrys in /etc/passwd */
 static enum nss_status
 getpwnam_plususer (const char *name, struct passwd *result, char *buffer,
-		   size_t buflen)
+		   size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   struct passwd pwd;
@@ -709,7 +716,7 @@ getpwnam_plususer (const char *name, struct passwd *result, char *buffer,
   plen = pwd_need_buflen (&pwd);
   if (plen > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p = buffer + (buflen - plen);
@@ -729,11 +736,12 @@ getpwnam_plususer (const char *name, struct passwd *result, char *buffer,
 	  nis_freeresult (res);
 	  return status;
 	}
-      if ((parse_res = _nss_nisplus_parse_pwent (res, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_pwent (res, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (res);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (res);
@@ -744,24 +752,27 @@ getpwnam_plususer (const char *name, struct passwd *result, char *buffer,
       int outvallen;
 
       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (yp_match (domain, "passwd.byname", name, strlen (name),
 		    &outval, &outvallen)
 	  != YPERR_SUCCESS)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       ptr = strncpy (buffer, outval, buflen < (size_t) outvallen ?
 		     buflen : (size_t) outvallen);
       buffer[buflen < (size_t) outvallen ? buflen : (size_t) outvallen] = '\0';
       free (outval);
       while (isspace (*ptr))
 	ptr++;
-      if ((parse_res = _nss_files_parse_pwent (ptr, result, data, buflen))
-	  == -1)
-	{
-	  __set_errno (ERANGE);
-	  return NSS_STATUS_TRYAGAIN;
-	}
+      parse_res = _nss_files_parse_pwent (ptr, result, data, buflen, errnop);
+      if (parse_res == -1)
+	return NSS_STATUS_TRYAGAIN;
     }
 
   if (parse_res > 0)
@@ -782,7 +793,7 @@ getpwnam_plususer (const char *name, struct passwd *result, char *buffer,
 
 static enum nss_status
 getpwent_next_file (struct passwd *result, ent_t *ent,
-		    char *buffer, size_t buflen)
+		    char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   while (1)
@@ -796,7 +807,16 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
 	  fgetpos (ent->stream, &pos);
 	  p = fgets (buffer, buflen, ent->stream);
 	  if (p == NULL)
-	    return NSS_STATUS_NOTFOUND;
+	    {
+	      if (feof (ent->stream))
+		return NSS_STATUS_NOTFOUND;
+	      else
+		{
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
+		  return NSS_STATUS_TRYAGAIN;
+		}
+	    }
 
 	  /* Terminate the line for any case.  */
 	  buffer[buflen - 1] = '\0';
@@ -808,13 +828,14 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines.  */
       /* Parse the line.  If it is invalid, loop to
          get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -826,14 +847,15 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
       if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
 	  && result->pw_name[2] != '\0')
 	{
+	  /* XXX Do not use fixed length buffer.  */
 	  char buf2[1024];
 	  char *user, *host, *domain;
 	  struct __netgrent netgrdata;
 
 	  bzero (&netgrdata, sizeof (struct __netgrent));
 	  __internal_setnetgrent (&result->pw_name[2], &netgrdata);
-	  while (__internal_getnetgrent_r (&host, &user, &domain,
-					   &netgrdata, buf2, sizeof (buf2)))
+	  while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
+					   buf2, sizeof (buf2), errnop))
 	    {
 	      if (user != NULL && user[0] != '-')
 		blacklist_store_name (user, ent);
@@ -855,11 +877,11 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
 	  if (use_nisplus)
 	    status =  getpwent_next_nisplus_netgr (NULL, result, ent,
 						   &result->pw_name[2],
-						   buffer, buflen);
+						   buffer, buflen, errnop);
 	  else
 	    status =  getpwent_next_nis_netgr (NULL, result, ent,
 					       &result->pw_name[2],
-					       buffer, buflen);
+					       buffer, buflen, errnop);
 	  if (status == NSS_STATUS_RETURN)
 	    continue;
 	  else
@@ -884,14 +906,22 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
 	     /etc/passwd */
 	  blacklist_store_name (&result->pw_name[1], ent);
 	  status = getpwnam_plususer (&result->pw_name[1], result, buffer,
-				      buflen);
+				      buflen, errnop);
 	  if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
 	    break;
 	  else
 	    if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
 	      continue;
 	    else
-	      return status;
+	      {
+		if (status == NSS_STATUS_TRYAGAIN)
+		  {
+		    /* The parser ran out of space */
+		    fsetpos (ent->stream, &pos);
+		    *errnop = ERANGE;
+		  }
+		return status;
+	      }
 	}
 
       /* +:... */
@@ -902,9 +932,9 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
 	  copy_pwd_changes (&ent->pwd, result, NULL, 0);
 
 	  if (use_nisplus)
-	    return getpwent_next_nisplus (result, ent, buffer, buflen);
+	    return getpwent_next_nisplus (result, ent, buffer, buflen, errnop);
 	  else
-	    return getpwent_next_nis (result, ent, buffer, buflen);
+	    return getpwent_next_nis (result, ent, buffer, buflen, errnop);
 	}
     }
 
@@ -914,7 +944,7 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
 
 static enum nss_status
 internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
-		     size_t buflen)
+		     size_t buflen, int *errnop)
 {
   if (ent->netgroup)
     {
@@ -924,11 +954,12 @@ internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
       /* Since this is not the first call, we don't need the group name */
       if (use_nisplus)
 	status = getpwent_next_nisplus_netgr (NULL, pw, ent, NULL, buffer,
-					      buflen);
+					      buflen, errnop);
       else
-	status = getpwent_next_nis_netgr (NULL, pw, ent, NULL, buffer, buflen);
+	status = getpwent_next_nis_netgr (NULL, pw, ent, NULL, buffer, buflen,
+					  errnop);
       if (status == NSS_STATUS_RETURN)
-	return getpwent_next_file (pw, ent, buffer, buflen);
+	return getpwent_next_file (pw, ent, buffer, buflen, errnop);
       else
 	return status;
     }
@@ -936,16 +967,17 @@ internal_getpwent_r (struct passwd *pw, ent_t *ent, char *buffer,
     if (ent->nis)
       {
 	if (use_nisplus)
-	  return getpwent_next_nisplus (pw, ent, buffer, buflen);
+	  return getpwent_next_nisplus (pw, ent, buffer, buflen, errnop);
 	else
-	  return getpwent_next_nis (pw, ent, buffer, buflen);
+	  return getpwent_next_nis (pw, ent, buffer, buflen, errnop);
       }
     else
-      return getpwent_next_file (pw, ent, buffer, buflen);
+      return getpwent_next_file (pw, ent, buffer, buflen, errnop);
 }
 
 enum nss_status
-_nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
+_nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
 
@@ -962,7 +994,7 @@ _nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
     status = internal_setpwent (&ext_ent);
 
   if (status == NSS_STATUS_SUCCESS)
-    status = internal_getpwent_r (pwd, &ext_ent, buffer, buflen);
+    status = internal_getpwent_r (pwd, &ext_ent, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -972,7 +1004,7 @@ _nss_compat_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
 static enum nss_status
 internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
 
@@ -992,7 +1024,8 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
 		return NSS_STATUS_NOTFOUND;
 	      else
 		{
-		  __set_errno (ERANGE);
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
 		  return NSS_STATUS_TRYAGAIN;
 		}
 	    }
@@ -1007,13 +1040,14 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines.  */
 	     /* Parse the line.  If it is invalid, loop to
 		get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -1030,14 +1064,15 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
       if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
 	  && result->pw_name[2] != '\0')
 	{
+	  /* XXX Do not use fixed length buffers.  */
 	  char buf2[1024];
 	  char *user, *host, *domain;
 	  struct __netgrent netgrdata;
 
 	  bzero (&netgrdata, sizeof (struct __netgrent));
 	  __internal_setnetgrent (&result->pw_name[2], &netgrdata);
-	  while (__internal_getnetgrent_r (&host, &user, &domain,
-					   &netgrdata, buf2, sizeof (buf2)))
+	  while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
+					   buf2, sizeof (buf2), errnop))
 	    {
 	      if (user != NULL && user[0] != '-')
 		if (strcmp (user, name) == 0)
@@ -1063,10 +1098,10 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
 	    {
 	      if (use_nisplus)
 		status = getpwent_next_nisplus_netgr (name, result, ent, buf,
-						      buffer, buflen);
+						      buffer, buflen, errnop);
 	      else
 		status = getpwent_next_nis_netgr (name, result, ent, buf,
-						  buffer, buflen);
+						  buffer, buflen, errnop);
 	      if (status == NSS_STATUS_RETURN)
 		continue;
 
@@ -1095,7 +1130,8 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
 	    {
 	      enum nss_status status;
 
-	      status = getpwnam_plususer (name, result, buffer, buflen);
+	      status = getpwnam_plususer (name, result, buffer, buflen,
+					  errnop);
 	      if (status == NSS_STATUS_RETURN)
 		/* We couldn't parse the entry */
 		return NSS_STATUS_NOTFOUND;
@@ -1109,7 +1145,7 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
 	{
 	  enum nss_status status;
 
-	  status = getpwnam_plususer (name, result, buffer, buflen);
+	  status = getpwnam_plususer (name, result, buffer, buflen, errnop);
 	  if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
 	    break;
 	  else
@@ -1124,7 +1160,7 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
 
 enum nss_status
 _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
-			char *buffer, size_t buflen)
+			char *buffer, size_t buflen, int *errnop)
 {
   ent_t ent = {0, 0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0},
 	       {NULL, NULL, 0, 0, NULL, NULL, NULL}};
@@ -1147,7 +1183,7 @@ _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
   if (status != NSS_STATUS_SUCCESS)
     return status;
 
-  status = internal_getpwnam_r (name, pwd, &ent, buffer, buflen);
+  status = internal_getpwnam_r (name, pwd, &ent, buffer, buflen, errnop);
 
   internal_endpwent (&ent);
 
@@ -1157,7 +1193,7 @@ _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
 /* This function handle the + entry in /etc/passwd for getpwuid */
 static enum nss_status
 getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
-		   size_t buflen)
+		   size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   struct passwd pwd;
@@ -1172,7 +1208,7 @@ getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
   plen = pwd_need_buflen (&pwd);
   if (plen > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p = buffer + (buflen - plen);
@@ -1183,7 +1219,7 @@ getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
       nis_result *res;
       char buf[1024 + pwdtablelen];
 
-      sprintf(buf, "[uid=%d],%s", uid, pwdtable);
+      snprintf(buf, sizeof (buf), "[uid=%d],%s", uid, pwdtable);
       res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
       if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
 	{
@@ -1193,10 +1229,10 @@ getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
 	  return status;
 	}
       if ((parse_res = _nss_nisplus_parse_pwent (res, result, buffer,
-						 buflen)) == -1)
+						 buflen, errnop)) == -1)
 	{
 	  nis_freeresult (res);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (res);
@@ -1208,25 +1244,28 @@ getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
       int outvallen;
 
       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       sprintf (buf, "%d", uid);
       if (yp_match (domain, "passwd.byuid", buf, strlen (buf),
 		    &outval, &outvallen)
 	  != YPERR_SUCCESS)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       ptr = strncpy (buffer, outval, buflen < (size_t) outvallen ?
 		     buflen : (size_t) outvallen);
       buffer[buflen < (size_t) outvallen ? buflen : (size_t) outvallen] = '\0';
       free (outval);
       while (isspace (*ptr))
 	ptr++;
-      if ((parse_res = _nss_files_parse_pwent (ptr, result, data, buflen))
-	  == -1)
-	{
-	  __set_errno (ERANGE);
-	  return NSS_STATUS_TRYAGAIN;
-	}
+      parse_res = _nss_files_parse_pwent (ptr, result, data, buflen, errnop);
+      if (parse_res == -1)
+	return NSS_STATUS_TRYAGAIN;
     }
 
   if (parse_res > 0)
@@ -1248,7 +1287,7 @@ getpwuid_plususer (uid_t uid, struct passwd *result, char *buffer,
 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user id */
 static enum nss_status
 internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
 
@@ -1263,7 +1302,16 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
 	  fgetpos (ent->stream, &pos);
 	  p = fgets (buffer, buflen, ent->stream);
 	  if (p == NULL)
-	    return NSS_STATUS_NOTFOUND;
+	    {
+	      if (feof (ent->stream))
+		return NSS_STATUS_NOTFOUND;
+	      else
+		{
+		  fsetpos (ent->stream, &pos);
+		  *errnop = ERANGE;
+		  return NSS_STATUS_TRYAGAIN;
+		}
+	    }
 
 	  /* Terminate the line for any case.  */
 	  buffer[buflen - 1] = '\0';
@@ -1275,13 +1323,14 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines.  */
 	     /* Parse the line.  If it is invalid, loop to
 		get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_pwent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -1298,14 +1347,15 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
       if (result->pw_name[0] == '-' && result->pw_name[1] == '@'
 	  && result->pw_name[2] != '\0')
 	{
+	  /* XXX Do not use fixed length buffers.  */
 	  char buf2[1024];
 	  char *user, *host, *domain;
 	  struct __netgrent netgrdata;
 
 	  bzero (&netgrdata, sizeof (struct __netgrent));
 	  __internal_setnetgrent (&result->pw_name[2], &netgrdata);
-	  while (__internal_getnetgrent_r (&host, &user, &domain,
-					   &netgrdata, buf2, sizeof (buf2)))
+	  while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
+					   buf2, sizeof (buf2), errnop))
 	    {
               if (user != NULL && user[0] != '-')
                 blacklist_store_name (user, ent);
@@ -1330,10 +1380,10 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
 	    {
 	      if (use_nisplus)
 		status = getpwent_next_nisplus_netgr (NULL, result, ent, buf,
-						      buffer, buflen);
+						      buffer, buflen, errnop);
 	      else
 		status = getpwent_next_nis_netgr (NULL, result, ent, buf,
-						  buffer, buflen);
+						  buffer, buflen, errnop);
 	      if (status == NSS_STATUS_RETURN)
 		continue;
 
@@ -1361,7 +1411,7 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
              /etc/passwd */
           blacklist_store_name (&result->pw_name[1], ent);
 	  status = getpwnam_plususer (&result->pw_name[1], result, buffer,
-				      buflen);
+				      buflen, errnop);
 	  if (status == NSS_STATUS_SUCCESS && result->pw_uid == uid)
 	    break;
 	  else
@@ -1373,7 +1423,7 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
 	{
 	  enum nss_status status;
 
-	  status = getpwuid_plususer (uid, result, buffer, buflen);
+	  status = getpwuid_plususer (uid, result, buffer, buflen, errnop);
 	  if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
 	    break;
 	  else
@@ -1388,7 +1438,7 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
 
 enum nss_status
 _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
-			char *buffer, size_t buflen)
+			char *buffer, size_t buflen, int *errnop)
 {
   ent_t ent = {0, 0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0},
 	       {NULL, NULL, 0, 0, NULL, NULL, NULL}};
@@ -1408,7 +1458,7 @@ _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
   if (status != NSS_STATUS_SUCCESS)
     return status;
 
-  status = internal_getpwuid_r (uid, pwd, &ent, buffer, buflen);
+  status = internal_getpwuid_r (uid, pwd, &ent, buffer, buflen, errnop);
 
   internal_endpwent (&ent);
 
diff --git a/nis/nss_compat/compat-spwd.c b/nis/nss_compat/compat-spwd.c
index 5da19d3feb..92224f8ae7 100644
--- a/nis/nss_compat/compat-spwd.c
+++ b/nis/nss_compat/compat-spwd.c
@@ -291,7 +291,7 @@ _nss_compat_endspent (void)
 
 static enum nss_status
 getspent_next_nis_netgr (const char *name, struct spwd *result, ent_t *ent,
-			 char *group, char *buffer, size_t buflen)
+			 char *group, char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *ypdomain, *host, *user, *domain, *outval, *p, *p2;
@@ -320,7 +320,8 @@ getspent_next_nis_netgr (const char *name, struct spwd *result, ent_t *ent,
 
       saved_cursor = ent->netgrdata.cursor;
       status = __internal_getnetgrent_r (&host, &user, &domain,
-					 &ent->netgrdata, buffer, buflen);
+					 &ent->netgrdata, buffer, buflen,
+					 errnop);
       if (status != 1)
 	{
 	  __internal_endnetgrent (&ent->netgrdata);
@@ -348,7 +349,7 @@ getspent_next_nis_netgr (const char *name, struct spwd *result, ent_t *ent,
       p2len = spwd_need_buflen (&ent->pwd);
       if (p2len > buflen)
 	{
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       p2 = buffer + (buflen - p2len);
@@ -357,7 +358,8 @@ getspent_next_nis_netgr (const char *name, struct spwd *result, ent_t *ent,
       while (isspace (*p))
 	p++;
       free (outval);
-      if ((parse_res = _nss_files_parse_spent (p, result, data, buflen)) == -1)
+      parse_res = _nss_files_parse_spent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  ent->netgrdata.cursor = saved_cursor;
 	  return NSS_STATUS_TRYAGAIN;
@@ -379,7 +381,7 @@ getspent_next_nis_netgr (const char *name, struct spwd *result, ent_t *ent,
 static enum nss_status
 getspent_next_nisplus_netgr (const char *name, struct spwd *result,
 			     ent_t *ent, char *group, char *buffer,
-			     size_t buflen)
+			     size_t buflen, int *errnop)
 {
   char *ypdomain, *host, *user, *domain, *p2;
   int status, parse_res;
@@ -409,7 +411,8 @@ getspent_next_nisplus_netgr (const char *name, struct spwd *result,
 
       saved_cursor = ent->netgrdata.cursor;
       status = __internal_getnetgrent_r (&host, &user, &domain,
-                                         &ent->netgrdata, buffer, buflen);
+                                         &ent->netgrdata, buffer, buflen,
+					 errnop);
       if (status != 1)
         {
           __internal_endnetgrent (&ent->netgrdata);
@@ -432,25 +435,27 @@ getspent_next_nisplus_netgr (const char *name, struct spwd *result,
       p2len = spwd_need_buflen (&ent->pwd);
       if (p2len > buflen)
         {
-          __set_errno (ERANGE);
+          *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
       p2 = buffer + (buflen - p2len);
       buflen -= p2len;
       {
         char buf[strlen (user) + 30 + pwdtablelen];
-        sprintf(buf, "[name=%s],%s", user, pwdtable);
-        nisres = nis_list(buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
+        sprintf (buf, "[name=%s],%s", user, pwdtable);
+        nisres = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
       }
       if (niserr2nss (nisres->status) != NSS_STATUS_SUCCESS)
         {
           nis_freeresult (nisres);
           continue;
         }
-      if ((parse_res = _nss_nisplus_parse_spent (nisres, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_spent (nisres, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (nisres);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (nisres);
@@ -470,7 +475,7 @@ getspent_next_nisplus_netgr (const char *name, struct spwd *result,
 
 static enum nss_status
 getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
-                       size_t buflen)
+                       size_t buflen, int *errnop)
 {
   int parse_res;
   size_t p2len;
@@ -479,7 +484,7 @@ getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
   p2len = spwd_need_buflen (&ent->pwd);
   if (p2len > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p2 = buffer + (buflen - p2len);
@@ -494,7 +499,7 @@ getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
 	  saved_first = TRUE;
 	  saved_res = ent->result;
 
-          ent->result = nis_first_entry(pwdtable);
+          ent->result = nis_first_entry (pwdtable);
           if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
             {
               ent->nis = 0;
@@ -510,7 +515,7 @@ getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
 	  saved_first = FALSE;
 	  saved_res = ent->result;
 
-          res = nis_next_entry(pwdtable, &ent->result->cookie);
+          res = nis_next_entry (pwdtable, &ent->result->cookie);
           ent->result = res;
           if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
             {
@@ -520,13 +525,14 @@ getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
 	      return niserr2nss (ent->result->status);
             }
         }
-      if ((parse_res = _nss_nisplus_parse_spent (ent->result, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_spent (ent->result, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  ent->first = saved_first;
 	  nis_freeresult (ent->result);
 	  ent->result = saved_res;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -548,7 +554,7 @@ getspent_next_nisplus (struct spwd *result, ent_t *ent, char *buffer,
 
 static enum nss_status
 getspent_next_nis (struct spwd *result, ent_t *ent,
-		   char *buffer, size_t buflen)
+		   char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *outkey, *outval, *p, *p2;
@@ -565,7 +571,7 @@ getspent_next_nis (struct spwd *result, ent_t *ent,
   p2len = spwd_need_buflen (&ent->pwd);
   if (p2len > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p2 = buffer + (buflen - p2len);
@@ -618,13 +624,14 @@ getspent_next_nis (struct spwd *result, ent_t *ent,
 
       while (isspace (*p))
 	++p;
-      if ((parse_res = _nss_files_parse_spent (p, result, data, buflen)) == -1)
+      parse_res = _nss_files_parse_spent (p, result, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (ent->oldkey);
 	  ent->oldkey = saved_oldkey;
 	  ent->oldkeylen = saved_oldlen;
 	  ent->first = saved_first;
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -646,7 +653,7 @@ getspent_next_nis (struct spwd *result, ent_t *ent,
 /* This function handle the +user entrys in /etc/shadow */
 static enum nss_status
 getspnam_plususer (const char *name, struct spwd *result, char *buffer,
-		   size_t buflen)
+		   size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   struct spwd pwd;
@@ -661,7 +668,7 @@ getspnam_plususer (const char *name, struct spwd *result, char *buffer,
   plen = spwd_need_buflen (&pwd);
   if (plen > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   p = buffer + (buflen - plen);
@@ -681,10 +688,12 @@ getspnam_plususer (const char *name, struct spwd *result, char *buffer,
           nis_freeresult (res);
           return status;
         }
-      if ((parse_res = _nss_nisplus_parse_spent (res, result, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_spent (res, result, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (res);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       nis_freeresult (res);
@@ -695,24 +704,27 @@ getspnam_plususer (const char *name, struct spwd *result, char *buffer,
       int outvallen;
 
       if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (yp_match (domain, "shadow.byname", name, strlen (name),
 		    &outval, &outvallen)
           != YPERR_SUCCESS)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       ptr = strncpy (buffer, outval, buflen < (size_t) outvallen ?
 		     buflen : (size_t) outvallen);
       buffer[buflen < (size_t) outvallen ? buflen : (size_t) outvallen] = '\0';
       free (outval);
       while (isspace (*ptr))
         ptr++;
-      if ((parse_res = _nss_files_parse_spent (ptr, result, data, buflen))
-	  == -1)
-	{
-	  __set_errno (ERANGE);
-	  return NSS_STATUS_TRYAGAIN;
-	}
+      parse_res = _nss_files_parse_spent (ptr, result, data, buflen, errnop);
+      if (parse_res == -1)
+	return NSS_STATUS_TRYAGAIN;
     }
 
   if (parse_res)
@@ -733,7 +745,7 @@ getspnam_plususer (const char *name, struct spwd *result, char *buffer,
 
 static enum nss_status
 getspent_next_file (struct spwd *result, ent_t *ent,
-		    char *buffer, size_t buflen)
+		    char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   while (1)
@@ -760,13 +772,13 @@ getspent_next_file (struct spwd *result, ent_t *ent,
       /* Parse the line.  If it is invalid, loop to
          get the next line of the file to parse.  */
 	     || !(parse_res = _nss_files_parse_spent (p, result, data,
-						      buflen)));
+						      buflen, errnop)));
 
       if (parse_res == -1)
         {
           /* The parser ran out of space.  */
           fsetpos (ent->stream, &pos);
-          __set_errno (ERANGE);
+          *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
 
@@ -778,6 +790,7 @@ getspent_next_file (struct spwd *result, ent_t *ent,
       if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
 	  && result->sp_namp[2] != '\0')
 	{
+	  /* XXX Do not use fixed length buffers.  */
           char buf2[1024];
  	  char *user, *host, *domain;
           struct __netgrent netgrdata;
@@ -785,7 +798,8 @@ getspent_next_file (struct spwd *result, ent_t *ent,
           bzero (&netgrdata, sizeof (struct __netgrent));
           __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
 	  while (__internal_getnetgrent_r (&host, &user, &domain,
-					   &netgrdata, buf2, sizeof (buf2)))
+					   &netgrdata, buf2, sizeof (buf2),
+					   errnop))
 	    {
 	      if (user != NULL && user[0] != '-')
 		blacklist_store_name (user, ent);
@@ -807,11 +821,11 @@ getspent_next_file (struct spwd *result, ent_t *ent,
 	  if (use_nisplus)
 	    status = getspent_next_nisplus_netgr (NULL, result, ent,
 						  &result->sp_namp[2],
-						  buffer, buflen);
+						  buffer, buflen, errnop);
 	  else
 	    status = getspent_next_nis_netgr (NULL, result, ent,
 					      &result->sp_namp[2],
-					      buffer, buflen);
+					      buffer, buflen, errnop);
 	  if (status == NSS_STATUS_RETURN)
 	    continue;
 	  else
@@ -836,14 +850,21 @@ getspent_next_file (struct spwd *result, ent_t *ent,
 	     /etc/passwd */
 	  blacklist_store_name (&result->sp_namp[1], ent);
           status = getspnam_plususer (&result->sp_namp[1], result, buffer,
-				      buflen);
+				      buflen, errnop);
           if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
             break;
           else
             if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
               continue;
             else
-              return status;
+	      {
+		if (status == NSS_STATUS_TRYAGAIN)
+		  {
+		    fsetpos (ent->stream, &pos);
+		    *errnop = ERANGE;
+		  }
+		return status;
+	      }
 	}
 
       /* +:... */
@@ -854,9 +875,9 @@ getspent_next_file (struct spwd *result, ent_t *ent,
 	  copy_spwd_changes (&ent->pwd, result, NULL, 0);
 
 	  if (use_nisplus)
-	    return getspent_next_nisplus (result, ent, buffer, buflen);
+	    return getspent_next_nisplus (result, ent, buffer, buflen, errnop);
 	  else
-	    return getspent_next_nis (result, ent, buffer, buflen);
+	    return getspent_next_nis (result, ent, buffer, buflen, errnop);
 	}
     }
 
@@ -866,7 +887,7 @@ getspent_next_file (struct spwd *result, ent_t *ent,
 
 static enum nss_status
 internal_getspent_r (struct spwd *pw, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   if (ent->netgroup)
     {
@@ -876,11 +897,12 @@ internal_getspent_r (struct spwd *pw, ent_t *ent,
       /* Since this is not the first call, we don't need the group name */
       if (use_nisplus)
 	status = getspent_next_nisplus_netgr (NULL, pw, ent, NULL, buffer,
-					      buflen);
+					      buflen, errnop);
       else
-	status = getspent_next_nis_netgr (NULL, pw, ent, NULL, buffer, buflen);
+	status = getspent_next_nis_netgr (NULL, pw, ent, NULL, buffer, buflen,
+					  errnop);
       if (status == NSS_STATUS_RETURN)
-	return getspent_next_file (pw, ent, buffer, buflen);
+	return getspent_next_file (pw, ent, buffer, buflen, errnop);
       else
 	return status;
     }
@@ -888,16 +910,17 @@ internal_getspent_r (struct spwd *pw, ent_t *ent,
     if (ent->nis)
       {
 	if (use_nisplus)
-	  return getspent_next_nisplus (pw, ent, buffer, buflen);
+	  return getspent_next_nisplus (pw, ent, buffer, buflen, errnop);
 	else
-	  return getspent_next_nis (pw, ent, buffer, buflen);
+	  return getspent_next_nis (pw, ent, buffer, buflen, errnop);
       }
     else
-      return getspent_next_file (pw, ent, buffer, buflen);
+      return getspent_next_file (pw, ent, buffer, buflen, errnop);
 }
 
 enum nss_status
-_nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen)
+_nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
 
@@ -914,7 +937,7 @@ _nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen)
     status = internal_setspent (&ext_ent);
 
   if (status == NSS_STATUS_SUCCESS)
-    status = internal_getspent_r (pwd, &ext_ent, buffer, buflen);
+    status = internal_getspent_r (pwd, &ext_ent, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -924,7 +947,7 @@ _nss_compat_getspent_r (struct spwd *pwd, char *buffer, size_t buflen)
 /* Searches in /etc/passwd and the NIS/NIS+ map for a special user */
 static enum nss_status
 internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
 
@@ -951,13 +974,14 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
       while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines.  */
 	     /* Parse the line.  If it is invalid, loop to
 		get the next line of the file to parse.  */
-	     !(parse_res = _nss_files_parse_spent (p, result, data, buflen)));
+	     !(parse_res = _nss_files_parse_spent (p, result, data, buflen,
+						   errnop)));
 
       if (parse_res == -1)
 	{
 	  /* The parser ran out of space.  */
 	  fsetpos (ent->stream, &pos);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -974,14 +998,15 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
       if (result->sp_namp[0] == '-' && result->sp_namp[1] == '@'
 	  && result->sp_namp[2] != '\0')
 	{
+	  /* XXX Do not use fixed length buffers.  */
 	  char buf2[1024];
 	  char *user, *host, *domain;
 	  struct __netgrent netgrdata;
 
 	  bzero (&netgrdata, sizeof (struct __netgrent));
 	  __internal_setnetgrent (&result->sp_namp[2], &netgrdata);
-	  while (__internal_getnetgrent_r (&host, &user, &domain,
-					   &netgrdata, buf2, sizeof (buf2)))
+	  while (__internal_getnetgrent_r (&host, &user, &domain, &netgrdata,
+					   buf2, sizeof (buf2), errnop))
 	    {
 	      if (user != NULL && user[0] != '-')
 		if (strcmp (user, name) == 0)
@@ -995,10 +1020,9 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
       if (result->sp_namp[0] == '+' && result->sp_namp[1] == '@'
 	  && result->sp_namp[2] != '\0')
 	{
-	  char buf[strlen (result->sp_namp)];
+	  char *buf = strdupa (&result->sp_namp[2]);
 	  int status;
 
-	  strcpy (buf, &result->sp_namp[2]);
 	  ent->netgroup = TRUE;
 	  ent->first = TRUE;
 	  copy_spwd_changes (&ent->pwd, result, NULL, 0);
@@ -1007,15 +1031,15 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
 	    {
 	      if (use_nisplus)
 		status = getspent_next_nisplus_netgr (name, result, ent, buf,
-						      buffer, buflen);
+						      buffer, buflen, errnop);
 	      else
 		status = getspent_next_nis_netgr (name, result, ent, buf,
-						  buffer, buflen);
+						  buffer, buflen, errnop);
 	      if (status == NSS_STATUS_RETURN)
 		continue;
 
-	      if (status == NSS_STATUS_SUCCESS &&
-		  strcmp (result->sp_namp, name) == 0)
+	      if (status == NSS_STATUS_SUCCESS
+		  && strcmp (result->sp_namp, name) == 0)
 		return NSS_STATUS_SUCCESS;
 	    } while (status == NSS_STATUS_SUCCESS);
 	  continue;
@@ -1039,7 +1063,8 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
 	    {
 	      enum nss_status status;
 
-	      status = getspnam_plususer (name, result, buffer, buflen);
+	      status = getspnam_plususer (name, result, buffer, buflen,
+					  errnop);
 	      if (status == NSS_STATUS_RETURN)
 		/* We couldn't parse the entry */
 		return NSS_STATUS_NOTFOUND;
@@ -1053,7 +1078,7 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
 	{
 	  enum nss_status status;
 
-	  status = getspnam_plususer (name, result, buffer, buflen);
+	  status = getspnam_plususer (name, result, buffer, buflen, errnop);
 	  if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
 	    return NSS_STATUS_NOTFOUND;
 	  else
@@ -1065,7 +1090,7 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
 
 enum nss_status
 _nss_compat_getspnam_r (const char *name, struct spwd *pwd,
-			char *buffer, size_t buflen)
+			char *buffer, size_t buflen, int *errnop)
 {
   ent_t ent = {0, 0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0},
 	       {NULL, NULL, 0, 0, 0, 0, 0, 0, 0}};
@@ -1084,7 +1109,7 @@ _nss_compat_getspnam_r (const char *name, struct spwd *pwd,
   if (status != NSS_STATUS_SUCCESS)
     return status;
 
-  status = internal_getspnam_r (name, pwd, &ent, buffer, buflen);
+  status = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
 
   internal_endspent (&ent);
 
diff --git a/nis/nss_nis/nis-alias.c b/nis/nss_nis/nis-alias.c
index e187716bb0..0d0294082d 100644
--- a/nis/nss_nis/nis-alias.c
+++ b/nis/nss_nis/nis-alias.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
 
@@ -36,7 +36,7 @@ static int oldkeylen = 0;
 
 static int
 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
-			 char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer + strlen (alias) + 1;
   size_t room_left =
@@ -53,7 +53,7 @@ _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
     {
       /* The line is too long for our buffer.  */
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
@@ -138,7 +138,7 @@ _nss_nis_endaliasent (void)
 
 static enum nss_status
 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
-			    size_t buflen)
+			    size_t buflen, int *errnop)
 {
   char *domain;
   char *result;
@@ -168,14 +168,14 @@ internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
       if (retval != NSS_STATUS_SUCCESS)
 	{
 	  if (retval == NSS_STATUS_TRYAGAIN)
-            __set_errno (EAGAIN);
+            *errnop = errno;
           return retval;
         }
 
       if ((size_t) (len + 1) > buflen)
         {
 	  free (result);
-          __set_errno (ERANGE);
+          *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
       p = strncpy (buffer, result, len);
@@ -184,11 +184,12 @@ internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
         ++p;
       free (result);
 
-      if ((parse_res = _nss_nis_parse_aliasent (outkey, p, alias, 
-						buffer, buflen)) == -1)
+      parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, buflen,
+					   errnop);
+      if (parse_res == -1)
 	{
 	  free (outkey);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -203,13 +204,14 @@ internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
 }
 
 enum nss_status
-_nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen)
+_nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getaliasent_r (alias, buffer, buflen);
+  status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -218,7 +220,7 @@ _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   enum nss_status retval;
   int parse_res;
@@ -241,14 +243,14 @@ _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -259,10 +261,10 @@ _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
   free (result);
 
   alias->alias_local = 0;
-  if ((parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, 
-					    buflen)) == -1)
+  parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
-  
+
   if (parse_res)
     return NSS_STATUS_SUCCESS;
   else
diff --git a/nis/nss_nis/nis-ethers.c b/nis/nss_nis/nis-ethers.c
index e54262409c..365f9c9c1d 100644
--- a/nis/nss_nis/nis-ethers.c
+++ b/nis/nss_nis/nis-ethers.c
@@ -143,7 +143,8 @@ _nss_nis_endetherent (void)
 }
 
 static enum nss_status
-internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen)
+internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen,
+			    int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   int parse_res;
@@ -163,23 +164,25 @@ internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen)
       while (isspace (*p))
         ++p;
 
-      if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1)
+      parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
+      if (parse_res == -1)
 	return NSS_STATUS_TRYAGAIN;
       next = next->next;
     }
   while (!parse_res);
-  
+
   return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status
-_nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen)
+_nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen,
+			int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getetherent_r (result, buffer, buflen);
+  status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -188,7 +191,7 @@ _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_gethostton_r (const char *name, struct ether *eth,
-		       char *buffer, size_t buflen)
+		       char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -210,14 +213,14 @@ _nss_nis_gethostton_r (const char *name, struct ether *eth,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+        *errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -227,7 +230,8 @@ _nss_nis_gethostton_r (const char *name, struct ether *eth,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1)
+  parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (!parse_res)
@@ -238,7 +242,7 @@ _nss_nis_gethostton_r (const char *name, struct ether *eth,
 
 enum nss_status
 _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth,
-		       char *buffer, size_t buflen)
+		       char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -269,14 +273,14 @@ _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+        *errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -286,7 +290,8 @@ _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1)
+  parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (!parse_res)
diff --git a/nis/nss_nis/nis-grp.c b/nis/nss_nis/nis-grp.c
index 7897e21881..98cc4fd878 100644
--- a/nis/nss_nis/nis-grp.c
+++ b/nis/nss_nis/nis-grp.c
@@ -78,7 +78,8 @@ _nss_nis_endgrent (void)
 }
 
 static enum nss_status
-internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen)
+internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen,
+			 int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *result, *outkey;
@@ -104,14 +105,14 @@ internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen)
       if (retval != NSS_STATUS_SUCCESS)
         {
           if (retval == NSS_STATUS_TRYAGAIN)
-            __set_errno (EAGAIN);
+            *errnop = errno;
           return retval;
         }
 
       if ((size_t) (len + 1) > buflen)
         {
           free (result);
-          __set_errno (ERANGE);
+          *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
 
@@ -121,9 +122,11 @@ internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen)
         ++p;
       free (result);
 
-      if ((parse_res = _nss_files_parse_grent (p, grp, data, buflen)) == -1)
+      parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (outkey);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -138,13 +141,14 @@ internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen)
+_nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen,
+		     int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getgrent_r (result, buffer, buflen);
+  status = internal_nis_getgrent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -153,7 +157,7 @@ _nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getgrnam_r (const char *name, struct group *grp,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -175,14 +179,14 @@ _nss_nis_getgrnam_r (const char *name, struct group *grp,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+        *errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -192,7 +196,8 @@ _nss_nis_getgrnam_r (const char *name, struct group *grp,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_grent (p, grp, data, buflen)) == -1)
+  parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (parse_res)
@@ -203,7 +208,7 @@ _nss_nis_getgrnam_r (const char *name, struct group *grp,
 
 enum nss_status
 _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -222,14 +227,14 @@ _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+        *errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -239,7 +244,8 @@ _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_grent (p, grp, data, buflen)) == -1)
+  parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (parse_res)
diff --git a/nis/nss_nis/nis-hosts.c b/nis/nss_nis/nis-hosts.c
index da7cef5ada..f08a624c37 100644
--- a/nis/nss_nis/nis-hosts.c
+++ b/nis/nss_nis/nis-hosts.c
@@ -133,7 +133,7 @@ _nss_nis_endhostent (void)
 
 static enum nss_status
 internal_nis_gethostent_r (struct hostent *host, char *buffer,
-			   size_t buflen, int *h_errnop)
+			   size_t buflen, int *errnop, int *h_errnop)
 {
   char *domain;
   char *result;
@@ -148,7 +148,7 @@ internal_nis_gethostent_r (struct hostent *host, char *buffer,
 
   if (buflen < sizeof *data + 1)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       *h_errnop = NETDB_INTERNAL;
       return NSS_STATUS_TRYAGAIN;
     }
@@ -172,7 +172,7 @@ internal_nis_gethostent_r (struct hostent *host, char *buffer,
 	  switch (retval)
 	    {
 	    case NSS_STATUS_TRYAGAIN:
-	      __set_errno (EAGAIN);
+	      *errnop = errno;
 	      *h_errnop = TRY_AGAIN;
 	      break;
 	    case NSS_STATUS_NOTFOUND:
@@ -189,7 +189,7 @@ internal_nis_gethostent_r (struct hostent *host, char *buffer,
         {
           free (result);
 	  *h_errnop = NETDB_INTERNAL;
-          __set_errno (ERANGE);
+          *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
 
@@ -199,10 +199,11 @@ internal_nis_gethostent_r (struct hostent *host, char *buffer,
 	++p;
       free (result);
 
-      if ((parse_res = parse_line (p, host, data, buflen)) == -1)
+      if ((parse_res = parse_line (p, host, data, buflen, errnop)) == -1)
 	{
 	  free (outkey);
-	  *h_errnop = NETDB_INTERNAL;;
+	  *h_errnop = NETDB_INTERNAL;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       free (oldkey);
@@ -218,13 +219,13 @@ internal_nis_gethostent_r (struct hostent *host, char *buffer,
 
 int
 _nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
-		       int *h_errnop)
+		       int *errnop, int *h_errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_gethostent_r (host, buffer, buflen, h_errnop);
+  status = internal_nis_gethostent_r (host, buffer, buflen, errnop, h_errnop);
 
   __libc_lock_unlock (lock);
 
@@ -233,7 +234,8 @@ _nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
 
 enum nss_status
 _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
-			  char *buffer, size_t buflen, int *h_errnop)
+			   char *buffer, size_t buflen, int *errnop,
+			   int *h_errnop)
 {
   enum nss_status retval;
   char *domain, *result, *p;
@@ -253,7 +255,7 @@ _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
   if (buflen < sizeof *data + 1)
     {
       *h_errnop = NETDB_INTERNAL;
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
   retval = yperr2nss (yp_match (domain, "hosts.byname", name,
@@ -264,7 +266,7 @@ _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
       if (retval == NSS_STATUS_TRYAGAIN)
 	{
 	  *h_errnop = TRY_AGAIN;
-	  __set_errno (EAGAIN);
+	  *errnop = errno;
 	}
       if (retval == NSS_STATUS_NOTFOUND)
 	*h_errnop = HOST_NOT_FOUND;
@@ -275,7 +277,7 @@ _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
     {
       free (result);
       *h_errnop = NETDB_INTERNAL;
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -285,7 +287,7 @@ _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
     ++p;
   free (result);
 
-  parse_res = parse_line (p, host, data, buflen);
+  parse_res = parse_line (p, host, data, buflen, errnop);
 
   if (parse_res < 1 || host->h_addrtype != af)
     {
@@ -306,27 +308,27 @@ _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
 }
 
 enum nss_status
-_nss_nis_gethostbyname_r (const char *name, struct hostent *host,
-			  char *buffer, size_t buflen, int *h_errnop)
+_nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer,
+			  size_t buflen, int *errnop, int *h_errnop)
 {
   if (_res.options & RES_USE_INET6)
     {
       enum nss_status status;
 
       status = _nss_nis_gethostbyname2_r (name, AF_INET6, host, buffer, buflen,
-					  h_errnop);
+					  errnop, h_errnop);
       if (status == NSS_STATUS_SUCCESS)
 	return status;
     }
 
   return _nss_nis_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
-				    h_errnop);
+				    errnop, h_errnop);
 }
 
 enum nss_status
 _nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type,
 			  struct hostent *host, char *buffer, size_t buflen,
-			  int *h_errnop)
+			  int *errnop, int *h_errnop)
 {
   enum nss_status retval;
   char *domain, *result, *p;
@@ -340,7 +342,7 @@ _nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type,
 
   if (buflen < sizeof *data + 1)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       *h_errnop = NETDB_INTERNAL;
       return NSS_STATUS_TRYAGAIN;
     }
@@ -355,7 +357,7 @@ _nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type,
       if (retval == NSS_STATUS_TRYAGAIN)
 	{
 	  *h_errnop = TRY_AGAIN;
-	  __set_errno (EAGAIN);
+	  *errnop = errno;
 	}
       if (retval == NSS_STATUS_NOTFOUND)
 	*h_errnop = HOST_NOT_FOUND;
@@ -365,7 +367,7 @@ _nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type,
   if ((size_t) (len + 1) > linebuflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       *h_errnop = NETDB_INTERNAL;
       return NSS_STATUS_TRYAGAIN;
     }
@@ -376,7 +378,7 @@ _nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type,
     ++p;
   free (result);
 
-  parse_res = parse_line (p, host, data, buflen);
+  parse_res = parse_line (p, host, data, buflen, errnop);
   if (parse_res < 1)
     {
       if (parse_res == -1)
diff --git a/nis/nss_nis/nis-netgrp.c b/nis/nss_nis/nis-netgrp.c
index ce8762a1e5..d170b56f50 100644
--- a/nis/nss_nis/nis-netgrp.c
+++ b/nis/nss_nis/nis-netgrp.c
@@ -40,7 +40,7 @@ static char *cursor = NULL;;
 
 extern enum nss_status
 _nss_netgroup_parseline (char **cursor, struct __netgrent *result,
-			 char *buffer, size_t buflen);
+			 char *buffer, size_t buflen, int *errnop);
 
 enum nss_status
 _nss_nis_setnetgrent (char *group)
@@ -111,7 +111,8 @@ _nss_nis_endnetgrent (void)
 }
 
 enum nss_status
-_nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen)
+_nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status;
 
@@ -120,7 +121,7 @@ _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen)
 
   __libc_lock_lock (lock);
 
-  status = _nss_netgroup_parseline (&cursor, result, buffer, buflen);
+  status = _nss_netgroup_parseline (&cursor, result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
diff --git a/nis/nss_nis/nis-network.c b/nis/nss_nis/nis-network.c
index c17ba81a62..4fcf729b34 100644
--- a/nis/nss_nis/nis-network.c
+++ b/nis/nss_nis/nis-network.c
@@ -79,7 +79,7 @@ _nss_nis_endnetent (void)
 
 static enum nss_status
 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
-			  int *herrnop)
+			  int *errnop, int *herrnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *result, *outkey;
@@ -107,7 +107,7 @@ internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
           if (retval == NSS_STATUS_TRYAGAIN)
 	    {
 	      *herrnop = NETDB_INTERNAL;
-	      __set_errno (EAGAIN);
+	      *errnop = errno;
 	    }
           return retval;
         }
@@ -126,10 +126,12 @@ internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
         ++p;
       free (result);
 
-      if ((parse_res = _nss_files_parse_netent (p, net, data, buflen)) == -1)
+      parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (outkey);
 	  *herrnop = NETDB_INTERNAL;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -145,13 +147,13 @@ internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
 
 enum nss_status
 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
-		      int *herrnop)
+		      int *errnop, int *herrnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getnetent_r (net, buffer, buflen, herrnop);
+  status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
 
   __libc_lock_unlock (lock);
 
@@ -159,8 +161,8 @@ _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
 }
 
 enum nss_status
-_nss_nis_getnetbyname_r (const char *name, struct netent *net,
-			 char *buffer, size_t buflen, int *herrnop)
+_nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
+			 size_t buflen, int *errnop, int *herrnop)
 {
   enum nss_status retval;
   struct parser_data *data = (void *) buffer;
@@ -184,7 +186,7 @@ _nss_nis_getnetbyname_r (const char *name, struct netent *net,
     {
       if (retval == NSS_STATUS_TRYAGAIN)
 	{
-	  __set_errno (EAGAIN);
+	  *errnop = errno;
 	  *herrnop = NETDB_INTERNAL;
 	}
       return retval;
@@ -193,7 +195,7 @@ _nss_nis_getnetbyname_r (const char *name, struct netent *net,
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       *herrnop = NETDB_INTERNAL;
       return NSS_STATUS_TRYAGAIN;
     }
@@ -204,7 +206,7 @@ _nss_nis_getnetbyname_r (const char *name, struct netent *net,
     ++p;
   free (result);
 
-  parse_res = _nss_files_parse_netent (p, net, data, buflen);
+  parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
 
   if (parse_res < 1)
     {
@@ -220,7 +222,8 @@ _nss_nis_getnetbyname_r (const char *name, struct netent *net,
 
 enum nss_status
 _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
-			 char *buffer, size_t buflen, int *herrnop)
+			 char *buffer, size_t buflen, int *errnop,
+			 int *herrnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain;
@@ -264,7 +267,7 @@ _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
 	    else
 	      {
 		if (retval == NSS_STATUS_TRYAGAIN)
-		  __set_errno (EAGAIN);
+		  *errnop = errno;
 		return retval;
 	      }
 	  }
@@ -272,7 +275,7 @@ _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
       if ((size_t) (len + 1) > buflen)
 	{
 	  free (result);
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  *herrnop = NETDB_INTERNAL;
 	  return NSS_STATUS_TRYAGAIN;
 	}
@@ -283,8 +286,7 @@ _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
 	  ++p;
 	free (result);
 
-	parse_res = _nss_files_parse_netent (p, net, data, buflen);
-
+	parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
 
 	if (parse_res < 1)
 	  {
diff --git a/nis/nss_nis/nis-proto.c b/nis/nss_nis/nis-proto.c
index eb09bb4ec7..e0c976041d 100644
--- a/nis/nss_nis/nis-proto.c
+++ b/nis/nss_nis/nis-proto.c
@@ -136,7 +136,7 @@ _nss_nis_endprotoent (void)
 
 static enum nss_status
 internal_nis_getprotoent_r (struct protoent *proto,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   int parse_res;
@@ -156,24 +156,25 @@ internal_nis_getprotoent_r (struct protoent *proto,
       while (isspace (*p))
         ++p;
 
-      if ((parse_res = _nss_files_parse_protoent (p, proto, data, 
-						  buflen)) == -1)
+      parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
+      if (parse_res == -1)
         return NSS_STATUS_TRYAGAIN;
       next = next->next;
     }
   while (!parse_res);
-  
+
   return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status
-_nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
+_nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen,
+			int *errnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getprotoent_r (proto, buffer, buflen);
+  status = internal_nis_getprotoent_r (proto, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -182,7 +183,7 @@ _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -204,14 +205,14 @@ _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -221,7 +222,8 @@ _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_protoent (p, proto, data, buflen)) == -1)
+  parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (parse_res)
@@ -232,7 +234,7 @@ _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
 
 enum nss_status
 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
-			     char *buffer, size_t buflen)
+			     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -251,14 +253,14 @@ _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -268,7 +270,8 @@ _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_protoent (p, proto, data, buflen)) == -1)
+  parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (parse_res)
diff --git a/nis/nss_nis/nis-publickey.c b/nis/nss_nis/nis-publickey.c
index 4c921f4853..1c485d9d23 100644
--- a/nis/nss_nis/nis-publickey.c
+++ b/nis/nss_nis/nis-publickey.c
@@ -32,7 +32,7 @@ extern int xdecrypt (char *, char *);
 
 /* If we haven't found the entry, we give a SUCCESS and an empty key back. */
 enum nss_status
-_nss_nis_getpublickey (const char *netname, char *pkey)
+_nss_nis_getpublickey (const char *netname, char *pkey, int *errnop)
 {
   enum nss_status retval;
   char *domain, *result;
@@ -57,7 +57,7 @@ _nss_nis_getpublickey (const char *netname, char *pkey)
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
@@ -72,7 +72,8 @@ _nss_nis_getpublickey (const char *netname, char *pkey)
 }
 
 enum nss_status
-_nss_nis_getsecretkey (const char *netname, char *skey, char *passwd)
+_nss_nis_getsecretkey (const char *netname, char *skey, char *passwd,
+		       int *errnop)
 {
   enum nss_status retval;
   char buf[1024];
@@ -98,7 +99,7 @@ _nss_nis_getsecretkey (const char *netname, char *skey, char *passwd)
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
@@ -178,7 +179,7 @@ parse_netid_str (const char *s, uid_t *uidp, gid_t *gidp, int *gidlenp,
 
 enum nss_status
 _nss_nis_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
-		       gid_t *gidp, int *gidlenp, gid_t *gidlist)
+		       gid_t *gidp, int *gidlenp, gid_t *gidlist, int *errnop)
 {
   char *domain;
   int yperr;
diff --git a/nis/nss_nis/nis-pwd.c b/nis/nss_nis/nis-pwd.c
index f693db17ea..8924886e1e 100644
--- a/nis/nss_nis/nis-pwd.c
+++ b/nis/nss_nis/nis-pwd.c
@@ -78,7 +78,8 @@ _nss_nis_endpwent (void)
 }
 
 static enum nss_status
-internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
+internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
+			 int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *result, *outkey;
@@ -104,14 +105,14 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
       if (retval != NSS_STATUS_SUCCESS)
         {
           if (retval == NSS_STATUS_TRYAGAIN)
-            __set_errno (EAGAIN);
+            *errnop = errno;
           return retval;
         }
 
       if ((size_t) (len + 1) > buflen)
         {
           free (result);
-          __set_errno (ERANGE);
+	  *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
 
@@ -121,9 +122,11 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
         ++p;
       free (result);
 
-      if ((parse_res = _nss_files_parse_pwent (p, pwd, data, buflen)) == -1)
+      parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (outkey);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -138,13 +141,14 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
+_nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
+		     int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getpwent_r (result, buffer, buflen);
+  status = internal_nis_getpwent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -153,7 +157,7 @@ _nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -175,14 +179,14 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -192,8 +196,7 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
     ++p;
   free (result);
 
-  parse_res = _nss_files_parse_pwent (p, pwd, data, buflen);
-
+  parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
   if (parse_res < 1)
     {
       if (parse_res == -1)
@@ -207,7 +210,7 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
 
 enum nss_status
 _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -226,14 +229,14 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -243,8 +246,7 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
     ++p;
   free (result);
 
-  parse_res = _nss_files_parse_pwent (p, pwd, data, buflen);
-
+  parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
   if (parse_res < 1)
     {
       if (parse_res == -1)
diff --git a/nis/nss_nis/nis-rpc.c b/nis/nss_nis/nis-rpc.c
index cd1f0db9b5..274ef04b7a 100644
--- a/nis/nss_nis/nis-rpc.c
+++ b/nis/nss_nis/nis-rpc.c
@@ -154,7 +154,7 @@ _nss_nis_endrpcent (void)
 
 static enum nss_status
 internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
-			  intern_t *data)
+			  int *errnop, intern_t *data)
 {
   struct parser_data *pdata = (void *) buffer;
   int parse_res;
@@ -172,7 +172,8 @@ internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
       while (isspace (*p))
         ++p;
 
-      if ((parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen)) == -1)
+      parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop);
+      if (parse_res == -1)
 	return NSS_STATUS_TRYAGAIN;
       data->next = data->next->next;
     }
@@ -182,13 +183,14 @@ internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
 }
 
 enum nss_status
-_nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen)
+_nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen,
+		      int *errnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getrpcent_r (rpc, buffer, buflen, &intern);
+  status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop, &intern);
 
   __libc_lock_unlock (lock);
 
@@ -197,7 +199,7 @@ _nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
-			 char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   intern_t data = {NULL, NULL};
   enum nss_status status;
@@ -215,8 +217,8 @@ _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
 
   found = 0;
   while (!found &&
-         ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, &data))
-          == NSS_STATUS_SUCCESS))
+         ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, errnop,
+					      &data)) == NSS_STATUS_SUCCESS))
     {
       if (strcmp (rpc->r_name, name) == 0)
 	found = 1;
@@ -247,7 +249,7 @@ _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc,
 
 enum nss_status
 _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -266,14 +268,14 @@ _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -283,7 +285,7 @@ _nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc,
     ++p;
   free (result);
 
-  parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen);
+  parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen, errnop);
 
   if (parse_res < 1)
     {
diff --git a/nis/nss_nis/nis-service.c b/nis/nss_nis/nis-service.c
index 5ec89c8677..c55b3e0191 100644
--- a/nis/nss_nis/nis-service.c
+++ b/nis/nss_nis/nis-service.c
@@ -31,7 +31,7 @@
 
 /* The parser is defined in a different module.  */
 extern int _nss_files_parse_servent (char *line, struct servent *result,
-				     char *data, size_t datalen);
+				     char *data, size_t datalen, int *errnop);
 
 
 
@@ -155,7 +155,7 @@ _nss_nis_endservent (void)
 
 static enum nss_status
 internal_nis_getservent_r (struct servent *serv, char *buffer,
-			   size_t buflen, intern_t *data)
+			   size_t buflen, int *errnop, intern_t *data)
 {
   int parse_res;
   char *p;
@@ -172,8 +172,8 @@ internal_nis_getservent_r (struct servent *serv, char *buffer,
            while (isspace (*p))
         ++p;
 
-      if ((parse_res = _nss_files_parse_servent (p, serv, buffer, 
-						 buflen)) == -1)
+      parse_res = _nss_files_parse_servent (p, serv, buffer, buflen, errnop);
+      if (parse_res == -1)
         return NSS_STATUS_TRYAGAIN;
       data->next = data->next->next;
     }
@@ -183,13 +183,14 @@ internal_nis_getservent_r (struct servent *serv, char *buffer,
 }
 
 enum nss_status
-_nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen)
+_nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen,
+		       int *errnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getservent_r (serv, buffer, buflen, &intern);
+  status = internal_nis_getservent_r (serv, buffer, buflen, errnop, &intern);
 
   __libc_lock_unlock (lock);
 
@@ -198,7 +199,8 @@ _nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getservbyname_r (const char *name, char *protocol,
-			  struct servent *serv, char *buffer, size_t buflen)
+			  struct servent *serv, char *buffer, size_t buflen,
+			  int *errnop)
 {
   intern_t data = { NULL, NULL };
   enum nss_status status;
@@ -216,8 +218,8 @@ _nss_nis_getservbyname_r (const char *name, char *protocol,
 
   found = 0;
   while (!found &&
-         ((status = internal_nis_getservent_r (serv, buffer, buflen, &data))
-          == NSS_STATUS_SUCCESS))
+         ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
+					       &data)) == NSS_STATUS_SUCCESS))
     {
       if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
 	{
@@ -242,7 +244,7 @@ _nss_nis_getservbyname_r (const char *name, char *protocol,
 
 enum nss_status
 _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
-			  char *buffer, size_t buflen)
+			  char *buffer, size_t buflen, int *errnop)
 {
   intern_t data = { NULL, NULL };
   enum nss_status status;
@@ -260,17 +262,11 @@ _nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv,
 
   found = 0;
   while (!found &&
-         ((status = internal_nis_getservent_r (serv, buffer, buflen, &data))
-          == NSS_STATUS_SUCCESS))
-    {
-      if (htons (serv->s_port) == port)
-        {
-          if  (strcmp (serv->s_proto, protocol) == 0)
-            {
-              found = 1;
-            }
-        }
-    }
+         ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
+					       &data)) == NSS_STATUS_SUCCESS))
+    if (htons (serv->s_port) == port
+	&&  strcmp (serv->s_proto, protocol) == 0)
+      found = 1;
 
   internal_nis_endservent (&data);
 
diff --git a/nis/nss_nis/nis-spwd.c b/nis/nss_nis/nis-spwd.c
index 0263f4e313..5be010d433 100644
--- a/nis/nss_nis/nis-spwd.c
+++ b/nis/nss_nis/nis-spwd.c
@@ -78,7 +78,8 @@ _nss_nis_endspent (void)
 }
 
 static enum nss_status
-internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
+internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
+			 int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   char *domain, *result, *outkey;
@@ -104,14 +105,14 @@ internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
       if (retval != NSS_STATUS_SUCCESS)
         {
           if (retval == NSS_STATUS_TRYAGAIN)
-            __set_errno (EAGAIN);
+	    *errnop = errno;
           return retval;
         }
 
       if ((size_t) (len + 1) > buflen)
         {
           free (result);
-          __set_errno (ERANGE);
+	  *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
 
@@ -121,12 +122,14 @@ internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
         ++p;
       free (result);
 
-      if ((parse_res = _nss_files_parse_spent (p, sp, data, buflen)) == -1)
+      parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
+      if (parse_res == -1)
 	{
 	  free (outkey);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
-      
+
       free (oldkey);
       oldkey = outkey;
       oldkeylen = keylen;
@@ -138,13 +141,14 @@ internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen)
+_nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
+		     int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nis_getspent_r (result, buffer, buflen);
+  status = internal_nis_getspent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -153,7 +157,7 @@ _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   struct parser_data *data = (void *) buffer;
   enum nss_status retval;
@@ -175,14 +179,14 @@ _nss_nis_getspnam_r (const char *name, struct spwd *sp,
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-        __set_errno (EAGAIN);
+	*errnop = errno;
       return retval;
     }
 
   if ((size_t) (len + 1) > buflen)
     {
       free (result);
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
@@ -192,7 +196,8 @@ _nss_nis_getspnam_r (const char *name, struct spwd *sp,
     ++p;
   free (result);
 
-  if ((parse_res = _nss_files_parse_spent (p, sp, data, buflen)) == -1)
+  parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
+  if (parse_res == -1)
     return NSS_STATUS_TRYAGAIN;
 
   if (parse_res)
diff --git a/nis/nss_nisplus/nisplus-alias.c b/nis/nss_nisplus/nisplus-alias.c
index 76c6f25813..381ad18228 100644
--- a/nis/nss_nisplus/nisplus-alias.c
+++ b/nis/nss_nisplus/nisplus-alias.c
@@ -41,7 +41,7 @@ static u_long tablename_len = 0;
         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -52,7 +52,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -61,38 +64,38 @@ _nss_create_tablename (void)
 static int
 _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
 			     struct aliasent *alias, char *buffer,
-			     size_t buflen)
+			     size_t buflen, int *errnop)
 {
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of (&result->objects.objects_val[entry]) != NIS_ENTRY_OBJ ||
-      strcmp(result->objects.objects_val[entry].EN_data.en_type,
-	     "mail_aliases") != 0 ||
-      result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 2)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of (&result->objects.objects_val[entry]) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val[entry].EN_data.en_type,
+		 "mail_aliases") != 0
+      || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 2)
     return 0;
   else
     {
-      char *first_unused = buffer + NISENTRYLEN(0, 1, result) + 1;
+      char *first_unused = buffer + NISENTRYLEN (0, 1, result) + 1;
       size_t room_left =
 	buflen - (buflen % __alignof__ (char *)) -
-	NISENTRYLEN(0, 1, result) - 2;
+	NISENTRYLEN (0, 1, result) - 2;
       char *line;
       char *cp;
 
-      if (NISENTRYLEN(entry, 1, result) >= buflen)
+      if (NISENTRYLEN (entry, 1, result) >= buflen)
 	{
 	  /* The line is too long for our buffer.  */
 	no_more_room:
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return -1;
 	}
       else
 	{
-	  strncpy (buffer, NISENTRYVAL(entry, 1, result),
-		   NISENTRYLEN(entry, 1, result));
-	  buffer[NISENTRYLEN(entry, 1, result)] = '\0';
+	  strncpy (buffer, NISENTRYVAL (entry, 1, result),
+		   NISENTRYLEN (entry, 1, result));
+	  buffer[NISENTRYLEN (entry, 1, result)] = '\0';
 	}
 
       if (NISENTRYLEN(entry, 0, result) >= room_left)
@@ -102,8 +105,8 @@ _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
       alias->alias_members_len = 0;
       *first_unused = '\0';
       ++first_unused;
-      strcpy (first_unused, NISENTRYVAL(entry, 0, result));
-      first_unused[NISENTRYLEN(entry, 0, result)] = '\0';
+      strcpy (first_unused, NISENTRYVAL (entry, 0, result));
+      first_unused[NISENTRYLEN (entry, 0, result)] = '\0';
       alias->alias_name = first_unused;
 
       /* Terminate the line for any case.  */
@@ -153,16 +156,17 @@ static enum nss_status
 internal_setaliasent (void)
 {
   enum nss_status status;
+  int err;
 
   if (result)
     nis_freeresult (result);
   result = NULL;
 
-  if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
+  if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
     return NSS_STATUS_UNAVAIL;
 
   next_entry = 0;
-  result = nis_list(tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+  result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
   status = niserr2nss (result->status);
   if (status != NSS_STATUS_SUCCESS)
     {
@@ -203,7 +207,7 @@ _nss_nisplus_endaliasent (void)
 
 static enum nss_status
 internal_nisplus_getaliasent_r (struct aliasent *alias,
-				char *buffer, size_t buflen)
+				char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
@@ -222,8 +226,9 @@ internal_nisplus_getaliasent_r (struct aliasent *alias,
       if (next_entry >= result->objects.objects_len)
 	return NSS_STATUS_NOTFOUND;
 
-      if ((parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
-						    buffer, buflen)) == -1)
+      parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
+					       buffer, buflen, errnop);
+      if (parse_res == -1)
 	return NSS_STATUS_TRYAGAIN;
 
       ++next_entry;
@@ -234,13 +239,13 @@ internal_nisplus_getaliasent_r (struct aliasent *alias,
 
 enum nss_status
 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
-			    size_t buflen)
+			    size_t buflen, int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getaliasent_r (result, buffer, buflen);
+  status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -249,28 +254,32 @@ _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
-  if (name != NULL || strlen(name) <= 8)
+  if (name != NULL || strlen (name) <= 8)
     {
       nis_result *result;
       char buf[strlen (name) + 30 + tablename_len];
 
-      sprintf(buf, "[name=%s],%s", name, tablename_val);
+      sprintf (buf, "[name=%s],%s", name, tablename_val);
 
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	return niserr2nss (result->status);
 
-      if ((parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
-						    buffer, buflen)) == -1)
+      parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
+					       buffer, buflen, errnop);
+      if (parse_res == -1)
 	return NSS_STATUS_TRYAGAIN;
 
       if (parse_res)
diff --git a/nis/nss_nisplus/nisplus-ethers.c b/nis/nss_nisplus/nisplus-ethers.c
index 8ca6935cc7..76805f1403 100644
--- a/nis/nss_nisplus/nisplus-ethers.c
+++ b/nis/nss_nisplus/nisplus-ethers.c
@@ -53,7 +53,7 @@ struct etherent_data {};
 
 static int
 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   char *p = buffer;
   size_t room_left = buflen;
@@ -61,18 +61,18 @@ _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      result->objects.objects_len != 1 ||
-      __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ ||
-      strcmp(NIS_RES_OBJECT (result)->EN_data.en_type,
-             "ethers_tbl") != 0 ||
-      NIS_RES_OBJECT(result)->EN_data.en_cols.en_cols_len < 2)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || result->objects.objects_len != 1
+      || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
+      || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
+		 "ethers_tbl") != 0
+      || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
     return 0;
 
   /* Generate the ether entry format and use the normal parser */
   if (NISENTRYLEN (0, 0, result) +1 > room_left)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
   strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
@@ -85,7 +85,7 @@ _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -96,7 +96,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -107,6 +110,7 @@ enum nss_status
 _nss_nisplus_setetherent (void)
 {
   enum nss_status status;
+  int err;
 
   status = NSS_STATUS_SUCCESS;
 
@@ -116,7 +120,7 @@ _nss_nisplus_setetherent (void)
     nis_freeresult (result);
   result = NULL;
 
-  if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
+  if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
     status = NSS_STATUS_UNAVAIL;
 
   __libc_lock_unlock (lock);
@@ -140,13 +144,17 @@ _nss_nisplus_endetherent (void)
 
 static enum nss_status
 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
-				size_t buflen)
+				size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   /* Get the next entry until we found a correct one. */
   do
@@ -156,7 +164,7 @@ internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
       if (result == NULL)
 	{
 	  saved_result = NULL;
-	  result = nis_first_entry(tablename_val);
+	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	    return niserr2nss (result->status);
 	}
@@ -174,10 +182,12 @@ internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
-						    buflen)) == -1)
+      parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
+					       buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
+	  *errnop = ERANGE;
 	  result = saved_result;
 	  return NSS_STATUS_TRYAGAIN;
 	}
@@ -194,13 +204,13 @@ internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
 
 enum nss_status
 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
-			    size_t buflen)
+			    size_t buflen, int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getetherent_r (result, buffer, buflen);
+  status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -209,22 +219,26 @@ _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name != NULL)
     {
       nis_result *result;
       char buf[strlen (name) + 40 + tablename_len];
 
-      sprintf(buf, "[name=%s],%s", name, tablename_val);
+      sprintf (buf, "[name=%s],%s", name, tablename_val);
 
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	{
@@ -233,10 +247,12 @@ _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
 	  return status;
 	}
 
-      if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
-						    buflen)) == -1)
+      parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
+					       buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
@@ -249,15 +265,19 @@ _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
 enum nss_status
 _nss_nisplus_getntohost_r (const struct ether_addr *addr,
 			   struct etherent *eth,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (addr == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       return NSS_STATUS_UNAVAIL;
     }
   else
@@ -267,12 +287,12 @@ _nss_nisplus_getntohost_r (const struct ether_addr *addr,
       char buf[255 + tablename_len];
 
       memset (&buf, '\0', sizeof (buf));
-      sprintf(buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
-	      addr->ether_addr_octet[0], addr->ether_addr_octet[1],
-	      addr->ether_addr_octet[2], addr->ether_addr_octet[3],
-	      addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
+      sprintf (buf, "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir",
+	       addr->ether_addr_octet[0], addr->ether_addr_octet[1],
+	       addr->ether_addr_octet[2], addr->ether_addr_octet[3],
+	       addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
 
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	{
@@ -281,10 +301,12 @@ _nss_nisplus_getntohost_r (const struct ether_addr *addr,
 	  return status;
 	}
 
-      if ((parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
-						    buflen)) == -1)
+      parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
+					       buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
diff --git a/nis/nss_nisplus/nisplus-grp.c b/nis/nss_nisplus/nisplus-grp.c
index fb200321d2..4ac048ef05 100644
--- a/nis/nss_nisplus/nisplus-grp.c
+++ b/nis/nss_nisplus/nisplus-grp.c
@@ -36,7 +36,7 @@ static nis_name tablename_val = NULL;
 static u_long tablename_len = 0;
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -47,7 +47,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -57,6 +60,7 @@ static enum nss_status
 internal_setgrent (void)
 {
   enum nss_status status;
+  int err;
 
   if (result)
     nis_freeresult (result);
@@ -64,7 +68,7 @@ internal_setgrent (void)
   next_entry = 0;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
+    if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
       return NSS_STATUS_UNAVAIL;
 
   result = nis_list (tablename_val, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
@@ -106,7 +110,8 @@ _nss_nisplus_endgrent (void)
 }
 
 static enum nss_status
-internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen)
+internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
+			     int *errnop)
 {
   int parse_res;
 
@@ -125,8 +130,9 @@ internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen)
       if (next_entry >= result->objects.objects_len)
 	return NSS_STATUS_NOTFOUND;
 
-      if ((parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
-						 buffer, buflen)) == -1)
+      parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
+					    buffer, buflen, errnop);
+      if (parse_res == -1)
 	return NSS_STATUS_TRYAGAIN;
 
       ++next_entry;
@@ -137,13 +143,14 @@ internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen)
+_nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
+			 int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getgrent_r (result, buffer, buflen);
+  status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -152,13 +159,17 @@ _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
-			 char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL || strlen (name) > 8)
     return NSS_STATUS_NOTFOUND;
@@ -179,11 +190,15 @@ _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
+      parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen,
+					    errnop);
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
 
@@ -193,11 +208,15 @@ _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
 
 enum nss_status
 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
-			 char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   {
     int parse_res;
@@ -216,12 +235,16 @@ _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
 	return status;
       }
 
-    parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
+    parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen,
+					  errnop);
 
     nis_freeresult (result);
 
     if (parse_res == -1)
-      return NSS_STATUS_TRYAGAIN;
+      {
+	*errnop = ERANGE;
+	return NSS_STATUS_TRYAGAIN;
+      }
 
     if (parse_res)
       return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-hosts.c b/nis/nss_nisplus/nisplus-hosts.c
index d1aa5ef787..8514a63e8a 100644
--- a/nis/nss_nisplus/nisplus-hosts.c
+++ b/nis/nss_nisplus/nisplus-hosts.c
@@ -42,11 +42,11 @@ static u_long tablename_len = 0;
         ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
 
 /* Get implementation for some internal functions. */
-#include "../../resolv/mapv4v6addr.h"
+#include <resolv/mapv4v6addr.h>
 
 static int
 _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   unsigned int i;
   char *first_unused = buffer;
@@ -66,7 +66,7 @@ _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
   if (room_left < NISENTRYLEN (0, 2, result) + 1)
     {
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
@@ -169,7 +169,7 @@ _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -180,7 +180,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -190,6 +193,7 @@ enum nss_status
 _nss_nisplus_sethostent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -198,8 +202,7 @@ _nss_nisplus_sethostent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      status = NSS_STATUS_UNAVAIL;
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -222,7 +225,7 @@ _nss_nisplus_endhostent (void)
 
 static enum nss_status
 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
-			       size_t buflen, int *herrnop)
+			       size_t buflen, int *errnop, int *herrnop)
 {
   int parse_res;
 
@@ -235,17 +238,21 @@ internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
 	{
 	  saved_res = NULL;
 	  if (tablename_val == NULL)
-	    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-	      return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
 
-	  result = nis_first_entry(tablename_val);
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
+
+	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
             {
               enum nss_status retval = niserr2nss (result->status);
               if (retval == NSS_STATUS_TRYAGAIN)
                 {
                   *herrnop = NETDB_INTERNAL;
-                  __set_errno (EAGAIN);
+                  *errnop = errno;
                 }
               return retval;
             }
@@ -267,22 +274,23 @@ internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
               if (retval == NSS_STATUS_TRYAGAIN)
                 {
                   *herrnop = NETDB_INTERNAL;
-                  __set_errno (EAGAIN);
+		  *errnop = errno;
                 }
               return retval;
             }
 	}
 
-      parse_res = _nss_nisplus_parse_hostent (result, AF_INET6,
-					      host, buffer, buflen);
-      if (parse_res < 1 && errno != ERANGE)
+      parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
+					      buflen, errnop);
+      if (parse_res < 1 && *errnop != ERANGE)
 	parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host,
-						buffer, buflen);
-      if (parse_res < 1 && errno == ERANGE)
+						buffer, buflen, errnop);
+      if (parse_res < 1 && *errnop == ERANGE)
         {
 	  nis_freeresult (result);
 	  result = saved_res;
           *herrnop = NETDB_INTERNAL;
+	  *errnop = ERANGE;
           return NSS_STATUS_TRYAGAIN;
         }
       if (saved_res != NULL)
@@ -295,13 +303,14 @@ internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
 
 enum nss_status
 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
-			   size_t buflen, int *herrnop)
+			   size_t buflen, int *errnop, int *herrnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_gethostent_r (result, buffer, buflen, herrnop);
+  status = internal_nisplus_gethostent_r (result, buffer, buflen, errnop,
+					  herrnop);
 
   __libc_lock_unlock (lock);
 
@@ -310,20 +319,25 @@ _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
-			      char *buffer, size_t buflen, int *herrnop)
+			       char *buffer, size_t buflen, int *errnop,
+			       int *herrnop)
 {
   int parse_res, retval;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      {
-	*herrnop = NETDB_INTERNAL;
-	return NSS_STATUS_UNAVAIL;
-      }
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	{
+	  *herrnop = NETDB_INTERNAL;
+	  return NSS_STATUS_UNAVAIL;
+	}
+    }
 
   if (name == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       *herrnop = NETDB_INTERNAL;
       return NSS_STATUS_NOTFOUND;
     }
@@ -334,38 +348,38 @@ _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
 
       /* Search at first in the alias list, and use the correct name
 	 for the next search */
-      sprintf(buf, "[name=%s],%s", name, tablename_val);
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+      sprintf (buf, "[name=%s],%s", name, tablename_val);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       /* If we do not find it, try it as original name. But if the
 	 database is correct, we should find it in the first case, too */
-      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-	  __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-	  strcmp(result->objects.objects_val->EN_data.en_type,
-		 "hosts_tbl") != 0 ||
-	  result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
-	sprintf(buf, "[cname=%s],%s", name, tablename_val);
+      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+	  || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+	  || strcmp (result->objects.objects_val->EN_data.en_type,
+		     "hosts_tbl") != 0
+	  || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
+	sprintf (buf, "[cname=%s],%s", name, tablename_val);
       else
-	sprintf(buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
-		tablename_val);
+	sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
+		 tablename_val);
 
       nis_freeresult (result);
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       retval = niserr2nss (result->status);
       if (retval != NSS_STATUS_SUCCESS)
         {
           if (retval == NSS_STATUS_TRYAGAIN)
             {
-              __set_errno (EAGAIN);
+	      *errnop = errno;
               *herrnop = NETDB_INTERNAL;
             }
 	  nis_freeresult (result);
           return retval;
         }
 
-      parse_res =
-	_nss_nisplus_parse_hostent (result, af, host, buffer, buflen);
+      parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
+					      buflen, errnop);
 
       nis_freeresult (result);
 
@@ -374,7 +388,10 @@ _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
 
       *herrnop = NETDB_INTERNAL;
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       else
 	return NSS_STATUS_NOTFOUND;
     }
@@ -382,30 +399,35 @@ _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
 
 enum nss_status
 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
-			      char *buffer, size_t buflen, int *h_errnop)
+			      char *buffer, size_t buflen, int *errnop,
+			      int *h_errnop)
 {
   if (_res.options & RES_USE_INET6)
     {
       enum nss_status status;
 
       status = _nss_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer,
-					      buflen, h_errnop);
+					      buflen, errnop, h_errnop);
       if (status == NSS_STATUS_SUCCESS)
         return status;
     }
 
   return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer,
-					buflen, h_errnop);
+					buflen, errnop, h_errnop);
 }
 
 enum nss_status
 _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
 			      struct hostent *host, char *buffer,
-			      size_t buflen, int *herrnop)
+			      size_t buflen, int *errnop, int *herrnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (addr == NULL)
     return NSS_STATUS_NOTFOUND;
@@ -416,15 +438,15 @@ _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
       int retval, parse_res;
 
       sprintf (buf, "[addr=%s],%s",
-	       inet_ntoa (*(struct in_addr *)addr), tablename_val);
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+	       inet_ntoa (*(struct in_addr *) addr), tablename_val);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
       retval = niserr2nss (result->status);
       if (retval != NSS_STATUS_SUCCESS)
         {
           if (retval == NSS_STATUS_TRYAGAIN)
             {
-              __set_errno (EAGAIN);
+	      *errnop = errno;
               *herrnop = NETDB_INTERNAL;
             }
 	  nis_freeresult (result);
@@ -432,7 +454,7 @@ _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
         }
 
       parse_res = _nss_nisplus_parse_hostent (result, type, host,
-					      buffer, buflen);
+					      buffer, buflen, errnop);
       nis_freeresult (result);
 
       if (parse_res > 0)
@@ -440,7 +462,10 @@ _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
 
       *herrnop = NETDB_INTERNAL;
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       else
 	return NSS_STATUS_NOTFOUND;
     }
diff --git a/nis/nss_nisplus/nisplus-netgrp.c b/nis/nss_nisplus/nisplus-netgrp.c
index 2a2f4b5ec9..5d5b727292 100644
--- a/nis/nss_nisplus/nisplus-netgrp.c
+++ b/nis/nss_nisplus/nisplus-netgrp.c
@@ -42,7 +42,7 @@ static unsigned long position = 0;
 
 static enum nss_status
 _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
-			     size_t buflen)
+			     size_t buflen, int *errnop)
 {
   enum nss_status status;
 
@@ -60,7 +60,7 @@ _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
       result->type = group_val;
       if (NISENTRYLEN (position, 1, data) >= buflen)
 	{
-	  __set_errno (ERANGE);
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       strncpy (buffer, NISENTRYVAL (position, 1, data),
@@ -78,7 +78,7 @@ _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
   if (NISENTRYLEN (position, 2, data) + NISENTRYLEN (position, 3, data) +
       NISENTRYLEN (position, 4, data) + 6 > buflen)
     {
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       status = NSS_STATUS_TRYAGAIN;
     }
   else
@@ -94,8 +94,7 @@ _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
 	  result->val.triple.host = cp;
 	  cp = __stpncpy (cp, NISENTRYVAL (position, 2, data),
 			  NISENTRYLEN (position, 2, data));
-	  *cp = '\0';
-	  ++cp;
+	  *cp++ = '\0';
 	}
 
       if (NISENTRYLEN (position, 3, data) == 0)
@@ -105,8 +104,7 @@ _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
 	  result->val.triple.user = cp;
 	  cp = __stpncpy (cp, NISENTRYVAL (position, 3, data),
 			  NISENTRYLEN (position, 3, data));
-	  *cp = '\0';
-	  ++cp;
+	  *cp++ = '\0';
 	}
 
       if (NISENTRYLEN (position, 4, data) == 0)
@@ -190,13 +188,13 @@ _nss_nisplus_endnetgrent (void)
 
 enum nss_status
 _nss_nisplus_getnetgrent_r (struct __netgrent *result,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   enum nss_status status;
 
   __libc_lock_lock (lock);
 
-  status = _nss_nisplus_parse_netgroup (result, buffer, buflen);
+  status = _nss_nisplus_parse_netgroup (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
diff --git a/nis/nss_nisplus/nisplus-network.c b/nis/nss_nisplus/nisplus-network.c
index e548017068..b39a54fe03 100644
--- a/nis/nss_nisplus/nisplus-network.c
+++ b/nis/nss_nisplus/nisplus-network.c
@@ -43,7 +43,7 @@ static u_long tablename_len = 0;
 
 static int
 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -53,24 +53,24 @@ _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp(result->objects.objects_val[0].EN_data.en_type,
-             "networks_tbl") != 0 ||
-      result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val[0].EN_data.en_type,
+		 "networks_tbl") != 0
+      || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
     return 0;
 
   if (NISENTRYLEN(0, 0, result) >= room_left)
     {
       /* The line is too long for our buffer.  */
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
   strncpy (first_unused, NISENTRYVAL(0, 0, result),
            NISENTRYLEN (0, 0, result));
-  first_unused[NISENTRYLEN(0, 0, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 0, result)] = '\0';
   network->n_name = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
@@ -142,7 +142,7 @@ _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -153,7 +153,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -163,6 +166,7 @@ enum nss_status
 _nss_nisplus_setnetent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -171,8 +175,7 @@ _nss_nisplus_setnetent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      status = NSS_STATUS_UNAVAIL;
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -195,7 +198,7 @@ _nss_nisplus_endnetent (void)
 
 static enum nss_status
 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
-			       size_t buflen, int *herrnop)
+			       size_t buflen, int *errnop, int *herrnop)
 {
   int parse_res;
 
@@ -209,10 +212,14 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
 	  saved_res = NULL;
 
 	  if (tablename_val == NULL)
-	    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-	      return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
-	  result = nis_first_entry(tablename_val);
+	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	    {
 	      int retval;
@@ -223,7 +230,7 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
 	      if (retval == NSS_STATUS_TRYAGAIN)
 		{
 		  *herrnop = NETDB_INTERNAL;
-		  __set_errno (EAGAIN);
+		  *errnop = errno;
 		  return retval;
 		}
 	      else
@@ -234,7 +241,7 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
 	{
 	  nis_result *res;
 
-	  res = nis_next_entry(tablename_val, &result->cookie);
+	  res = nis_next_entry (tablename_val, &result->cookie);
 	  saved_res = result;
 	  result = res;
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
@@ -247,14 +254,15 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
 	      if (retval == NSS_STATUS_TRYAGAIN)
 		{
 		  *herrnop = NETDB_INTERNAL;
-		  __set_errno (EAGAIN);
+		  *errnop = errno;
 		}
 	      return retval;
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_netent (result, network, buffer,
-						  buflen)) == -1)
+      parse_res = _nss_nisplus_parse_netent (result, network, buffer,
+					     buflen, errnop);
+      if (parse_res == -1)
         {
           *herrnop = NETDB_INTERNAL;
           return NSS_STATUS_TRYAGAIN;
@@ -267,13 +275,14 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
 
 enum nss_status
 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
-			   size_t buflen, int *herrnop)
+			   size_t buflen, int *errnop, int *herrnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getnetent_r (result, buffer, buflen, herrnop);
+  status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
+					 herrnop);
 
   __libc_lock_unlock (lock);
 
@@ -282,17 +291,22 @@ _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
-			      char *buffer, size_t buflen, int *herrnop)
+			      char *buffer, size_t buflen, int *errnop,
+			     int *herrnop)
 {
   int parse_res, retval;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       *herrnop = NETDB_INTERNAL;
       return NSS_STATUS_UNAVAIL;
     }
@@ -303,38 +317,38 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
 
       /* Search at first in the alias list, and use the correct name
 	 for the next search */
-      sprintf(buf, "[name=%s],%s", name, tablename_val);
-      result = nis_list(buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
+      sprintf (buf, "[name=%s],%s", name, tablename_val);
+      result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
 
       /* If we do not find it, try it as original name. But if the
 	 database is correct, we should find it in the first case, too */
-      if ((result->status != NIS_SUCCESS &&
-	   result->status != NIS_S_SUCCESS) ||
-	  __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-	  strcmp(result->objects.objects_val[0].EN_data.en_type,
-		 "networks_tbl") != 0 ||
-	  result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
-	sprintf(buf, "[cname=%s],%s", name, tablename_val);
+      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+	  || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+	  || strcmp (result->objects.objects_val[0].EN_data.en_type,
+		     "networks_tbl") != 0
+	  || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
+	sprintf (buf, "[cname=%s],%s", name, tablename_val);
       else
-	sprintf(buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
-		tablename_val);
+	sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
+		 tablename_val);
 
       nis_freeresult (result);
-      result = nis_list(buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
+      result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
 
       retval = niserr2nss (result->status);
       if (retval != NSS_STATUS_SUCCESS)
 	{
 	  if (retval == NSS_STATUS_TRYAGAIN)
 	    {
-	      __set_errno (EAGAIN);
+	      *errnop = errno;
 	      *herrnop = NETDB_INTERNAL;
 	    }
 	  nis_freeresult (result);
 	  return retval;
 	}
 
-      parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
+      parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
+					     errnop);
 
       nis_freeresult (result);
 
@@ -343,7 +357,10 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
 
       *herrnop = NETDB_INTERNAL;
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       else
 	return NSS_STATUS_NOTFOUND;
     }
@@ -352,12 +369,16 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
 enum nss_status
 _nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type,
-			     struct netent *network,
-			     char *buffer, size_t buflen, int *herrnop)
+			     struct netent *network, char *buffer,
+			     size_t buflen, int *errnop, int *herrnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   {
     int parse_res, retval;
@@ -368,21 +389,22 @@ _nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type,
     in = inet_makeaddr (addr, 0);
     sprintf (buf, "[addr=%s],%s", inet_ntoa (in), tablename_val);
 
-    result = nis_list(buf, EXPAND_NAME, NULL, NULL);
+    result = nis_list (buf, EXPAND_NAME, NULL, NULL);
 
     retval = niserr2nss (result->status);
     if (retval != NSS_STATUS_SUCCESS)
       {
 	if (retval == NSS_STATUS_TRYAGAIN)
 	  {
-	    __set_errno (EAGAIN);
+	    *errnop = errno;
 	    *herrnop = NETDB_INTERNAL;
 	  }
 	nis_freeresult (result);
 	return retval;
       }
 
-    parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
+    parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
+					   errnop);
 
     nis_freeresult (result);
 
@@ -391,7 +413,10 @@ _nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type,
 
     *herrnop = NETDB_INTERNAL;
     if (parse_res == -1)
-      return NSS_STATUS_TRYAGAIN;
+      {
+	*errnop = ERANGE;
+	return NSS_STATUS_TRYAGAIN;
+      }
     else
       return NSS_STATUS_NOTFOUND;
   }
diff --git a/nis/nss_nisplus/nisplus-parser.c b/nis/nss_nisplus/nisplus-parser.c
index b700fa66b4..fa4073e6d8 100644
--- a/nis/nss_nisplus/nisplus-parser.c
+++ b/nis/nss_nisplus/nisplus-parser.c
@@ -34,7 +34,7 @@
 
 int
 _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
-			  char *buffer, size_t buflen)
+			  char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -42,35 +42,35 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      result->objects.objects_len != 1 ||
-      __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp(result->objects.objects_val->EN_data.en_type,
-	     "passwd_tbl") != 0 ||
-      result->objects.objects_val->EN_data.en_cols.en_cols_len < 7)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || result->objects.objects_len != 1
+      || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val->EN_data.en_type,
+		 "passwd_tbl") != 0
+      || result->objects.objects_val->EN_data.en_cols.en_cols_len < 7)
     return 0;
 
   if (NISENTRYLEN (0, 0, result) >= room_left)
     {
       /* The line is too long for our buffer.  */
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
-  strncpy (first_unused, NISENTRYVAL(0, 0, result),
+  strncpy (first_unused, NISENTRYVAL (0, 0, result),
 	   NISENTRYLEN (0, 0, result));
-  first_unused[NISENTRYLEN(0, 0, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 0, result)] = '\0';
   pw->pw_name = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
 
-  if (NISENTRYLEN(0, 1, result) >= room_left)
+  if (NISENTRYLEN (0, 1, result) >= room_left)
     goto no_more_room;
 
-  strncpy (first_unused, NISENTRYVAL(0, 1, result),
+  strncpy (first_unused, NISENTRYVAL (0, 1, result),
 	   NISENTRYLEN (0, 1, result));
-  first_unused[NISENTRYLEN(0, 1, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 1, result)] = '\0';
   pw->pw_passwd = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
@@ -80,17 +80,17 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
 
   strncpy (first_unused, NISENTRYVAL (0, 2, result),
 	   NISENTRYLEN (0, 2, result));
-  first_unused[NISENTRYLEN(0, 2, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 2, result)] = '\0';
   pw->pw_uid = atoi (first_unused);
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
 
-  if (NISENTRYLEN(0, 3, result) >= room_left)
+  if (NISENTRYLEN (0, 3, result) >= room_left)
     goto no_more_room;
 
-  strncpy (first_unused, NISENTRYVAL(0, 3, result),
+  strncpy (first_unused, NISENTRYVAL (0, 3, result),
 	   NISENTRYLEN (0, 3, result));
-  first_unused[NISENTRYLEN(0, 3, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 3, result)] = '\0';
   pw->pw_gid = atoi (first_unused);
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
@@ -98,24 +98,24 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
   if (NISENTRYLEN(0, 4, result) >= room_left)
     goto no_more_room;
 
-  strncpy (first_unused, NISENTRYVAL(0, 4, result),
+  strncpy (first_unused, NISENTRYVAL (0, 4, result),
 	   NISENTRYLEN (0, 4, result));
-  first_unused[NISENTRYLEN(0, 4, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 4, result)] = '\0';
   pw->pw_gecos = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
 
-  if (NISENTRYLEN(0, 5, result) >= room_left)
+  if (NISENTRYLEN (0, 5, result) >= room_left)
     goto no_more_room;
 
   strncpy (first_unused, NISENTRYVAL (0, 5, result),
 	   NISENTRYLEN (0, 5, result));
-  first_unused[NISENTRYLEN(0, 5, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 5, result)] = '\0';
   pw->pw_dir = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
 
-  if (NISENTRYLEN(0, 6, result) >= room_left)
+  if (NISENTRYLEN (0, 6, result) >= room_left)
     goto no_more_room;
 
   strncpy (first_unused, NISENTRYVAL (0, 6, result),
@@ -129,8 +129,8 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
 }
 
 int
-_nss_nisplus_parse_grent (nis_result *result, u_long entry,
-			  struct group *gr, char *buffer, size_t buflen)
+_nss_nisplus_parse_grent (nis_result *result, u_long entry, struct group *gr,
+			  char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -140,18 +140,18 @@ _nss_nisplus_parse_grent (nis_result *result, u_long entry,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp (result->objects.objects_val[entry].EN_data.en_type,
-	      "group_tbl") != 0 ||
-      result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 4)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val[entry].EN_data.en_type,
+		 "group_tbl") != 0
+      || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 4)
     return 0;
 
   if (NISENTRYLEN (entry, 0, result) >= room_left)
     {
       /* The line is too long for our buffer.  */
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
@@ -240,7 +240,7 @@ _nss_nisplus_parse_grent (nis_result *result, u_long entry,
 
 int
 _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
-			  char *buffer, size_t buflen)
+			  char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -248,35 +248,35 @@ _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      result->objects.objects_len != 1 ||
-      __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp (result->objects.objects_val->EN_data.en_type,
-	      "passwd_tbl") != 0 ||
-      result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 8)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || result->objects.objects_len != 1
+      || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val->EN_data.en_type,
+		 "passwd_tbl") != 0
+      || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 8)
     return 0;
 
-  if (NISENTRYLEN(0, 0, result) >= room_left)
+  if (NISENTRYLEN (0, 0, result) >= room_left)
     {
       /* The line is too long for our buffer.  */
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
 
   strncpy (first_unused, NISENTRYVAL (0, 0, result),
 	   NISENTRYLEN (0, 0, result));
-  first_unused[NISENTRYLEN(0, 0, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 0, result)] = '\0';
   sp->sp_namp = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
 
-  if (NISENTRYLEN(0, 1, result) >= room_left)
+  if (NISENTRYLEN (0, 1, result) >= room_left)
     goto no_more_room;
 
   strncpy (first_unused, NISENTRYVAL (0, 1, result),
 	   NISENTRYLEN (0, 1, result));
-  first_unused[NISENTRYLEN(0, 1, result)] = '\0';
+  first_unused[NISENTRYLEN (0, 1, result)] = '\0';
   sp->sp_pwdp = first_unused;
   room_left -= (strlen (first_unused) +1);
   first_unused += strlen (first_unused) +1;
@@ -300,40 +300,40 @@ _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
       if (cp == NULL)
 	return 0;
       *cp++ = '\0';
-      sp->sp_min = atol(line);
+      sp->sp_min = atol (line);
 
       line = cp;
       cp = strchr (line, ':');
       if (cp == NULL)
 	return 0;
       *cp++ = '\0';
-      sp->sp_max = atol(line);
+      sp->sp_max = atol (line);
 
       line = cp;
       cp = strchr (line, ':');
       if (cp == NULL)
 	return 0;
       *cp++ = '\0';
-      sp->sp_warn = atol(line);
+      sp->sp_warn = atol (line);
 
       line = cp;
       cp = strchr (line, ':');
       if (cp == NULL)
 	return 0;
       *cp++ = '\0';
-      sp->sp_inact = atol(line);
+      sp->sp_inact = atol (line);
 
       line = cp;
       cp = strchr (line, ':');
       if (cp == NULL)
 	return 0;
       *cp++ = '\0';
-      sp->sp_expire = atol(line);
+      sp->sp_expire = atol (line);
 
       line = cp;
       if (line == NULL)
 	return 0;
-      sp->sp_flag = atol(line);
+      sp->sp_flag = atol (line);
     }
 
   return 1;
diff --git a/nis/nss_nisplus/nisplus-proto.c b/nis/nss_nisplus/nisplus-proto.c
index 2cbd9fe2fa..262276f301 100644
--- a/nis/nss_nisplus/nisplus-proto.c
+++ b/nis/nss_nisplus/nisplus-proto.c
@@ -41,7 +41,7 @@ static u_long tablename_len = 0;
 
 static int
 _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
-			     char *buffer, size_t buflen)
+			     char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -51,9 +51,10 @@ _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ ||
-      strcmp (NIS_RES_OBJECT (result)->EN_data.en_type, "protocols_tbl") != 0
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
+      || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
+		 "protocols_tbl") != 0
       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
     return 0;
 
@@ -61,7 +62,7 @@ _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
     {
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
   strncpy (first_unused, NISENTRYVAL (0, 0, result),
@@ -101,7 +102,7 @@ _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
   proto->p_aliases = (char **) first_unused;
   if (room_left < sizeof (char *))
     goto no_more_room;
-  room_left -= (sizeof (char *));
+  room_left -= sizeof (char *);
   proto->p_aliases[0] = NULL;
 
   i = 0;
@@ -136,7 +137,7 @@ _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -147,7 +148,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -165,7 +169,10 @@ _nss_nisplus_setprotoent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    status = _nss_create_tablename ();
+    {
+      int err;
+      status = _nss_create_tablename (&err);
+    }
 
   __libc_lock_unlock (lock);
 
@@ -188,7 +195,7 @@ _nss_nisplus_endprotoent (void)
 
 static enum nss_status
 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
-				size_t buflen)
+				size_t buflen, int *errnop)
 {
   int parse_res;
 
@@ -201,8 +208,12 @@ internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
 	{
 	  saved_res = NULL;
 	  if (tablename_val == NULL)
-	    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-	      return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
 	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
@@ -223,11 +234,13 @@ internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
-						    buflen)) == -1)
+      parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
+					       buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
 	  result = saved_res;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -243,13 +256,13 @@ internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
 
 enum nss_status
 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
-			    size_t buflen)
+			    size_t buflen, int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getprotoent_r (result, buffer, buflen);
+  status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -258,13 +271,17 @@ _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
-			       char *buffer, size_t buflen)
+			       char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL)
     return NSS_STATUS_NOTFOUND;
@@ -280,12 +297,11 @@ _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
 
       /* If we do not find it, try it as original name. But if the
          database is correct, we should find it in the first case, too */
-      if ((result->status != NIS_SUCCESS &&
-	   result->status != NIS_S_SUCCESS) ||
-	  __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-	  strcmp (result->objects.objects_val->EN_data.en_type,
-		  "protocols_tbl") != 0 ||
-	  result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
+      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+	  || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+	  || strcmp (result->objects.objects_val->EN_data.en_type,
+		     "protocols_tbl") != 0
+	  || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
 	sprintf (buf, "[cname=%s],%s", name, tablename_val);
       else
 	sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
@@ -302,12 +318,16 @@ _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
+      parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
+					       errnop);
 
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
 
@@ -317,11 +337,16 @@ _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
 
 enum nss_status
 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
-				 char *buffer, size_t buflen)
+				 char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
+
   {
     int parse_res;
     nis_result *result;
@@ -339,12 +364,16 @@ _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
 	return status;
       }
 
-    parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
+    parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
+					     errnop);
 
     nis_freeresult (result);
 
     if (parse_res == -1)
-      return NSS_STATUS_TRYAGAIN;
+      {
+	*errnop = ERANGE;
+	return NSS_STATUS_TRYAGAIN;
+      }
 
     if (parse_res)
       return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-publickey.c b/nis/nss_nisplus/nisplus-publickey.c
index aa2a6f0c3a..24d217b804 100644
--- a/nis/nss_nisplus/nisplus-publickey.c
+++ b/nis/nss_nisplus/nisplus-publickey.c
@@ -32,7 +32,7 @@ extern int xdecrypt (char *, char *);
 
 /* If we haven't found the entry, we give a SUCCESS and an empty key back. */
 enum nss_status
-_nss_nisplus_getpublickey (const char *netname, char *pkey)
+_nss_nisplus_getpublickey (const char *netname, char *pkey, int *errnop)
 {
   nis_result *res;
   enum nss_status retval;
@@ -45,7 +45,7 @@ _nss_nisplus_getpublickey (const char *netname, char *pkey)
 
   if (netname == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       return NSS_STATUS_UNAVAIL;
     }
 
@@ -72,7 +72,7 @@ _nss_nisplus_getpublickey (const char *netname, char *pkey)
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       if (res->status == NIS_NOTFOUND)
 	retval = NSS_STATUS_SUCCESS;
       nis_freeresult (res);
@@ -103,7 +103,8 @@ _nss_nisplus_getpublickey (const char *netname, char *pkey)
 }
 
 enum nss_status
-_nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd)
+_nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd,
+			   int *errnop)
 {
   nis_result *res;
   enum nss_status retval;
@@ -116,7 +117,7 @@ _nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd)
 
   if (netname == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       return NSS_STATUS_UNAVAIL;
     }
 
@@ -143,7 +144,7 @@ _nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd)
   if (retval != NSS_STATUS_SUCCESS)
     {
       if (retval == NSS_STATUS_TRYAGAIN)
-	__set_errno (EAGAIN);
+	*errnop = errno;
       nis_freeresult (res);
       return retval;
     }
@@ -183,7 +184,8 @@ _nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd)
 /* Parse information from the passed string.
    The format of the string passed is gid,grp,grp, ...  */
 static enum nss_status
-parse_grp_str (const char *s, gid_t *gidp, int *gidlenp, gid_t *gidlist)
+parse_grp_str (const char *s, gid_t *gidp, int *gidlenp, gid_t *gidlist,
+	       int *errnop)
 {
   int gidlen;
 
@@ -209,7 +211,7 @@ parse_grp_str (const char *s, gid_t *gidp, int *gidlenp, gid_t *gidlist)
 
 enum nss_status
 _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
-		       gid_t *gidp, int *gidlenp, gid_t *gidlist)
+		       gid_t *gidp, int *gidlenp, gid_t *gidlist, int *errnop)
 {
   char *domain;
   nis_result *res;
@@ -223,7 +225,7 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
   if (! domain)
     return NSS_STATUS_UNAVAIL;
 
-  domain++;  /* skip '@' */
+  ++domain;  /* skip '@' */
 
   /* 2.  Get user's nisplus principal name.  */
   if ((strlen (netname) + strlen (domain)+45) >
@@ -260,6 +262,7 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
       syslog (LOG_ERR, _("netname2user: (nis+ lookup): %s\n"),
 	      nis_sperrno (res->status));
       nis_freeresult (res);
+      *errnop = errno;
       return NSS_STATUS_TRYAGAIN;
     default:
       syslog (LOG_ERR, _("netname2user: (nis+ lookup): %s\n"),
@@ -269,16 +272,14 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
     }
 
   if (res->objects.objects_len > 1)
-    {
-      /*
-       * A netname belonging to more than one principal?
-       * Something wrong with cred table. should be unique.
-       * Warn user and continue.
-       */
-      syslog (LOG_ALERT,
-	      _("netname2user: DES entry for %s in directory %s not unique"),
-	      netname, domain);
-    }
+    /*
+     * A netname belonging to more than one principal?
+     * Something wrong with cred table. should be unique.
+     * Warn user and continue.
+     */
+    syslog (LOG_ALERT,
+	    _("netname2user: DES entry for %s in directory %s not unique"),
+	    netname, domain);
 
   len = ENTRY_LEN (res->objects.objects_val, 0);
   strncpy (principal, ENTRY_VAL (res->objects.objects_val, 0), len);
@@ -326,6 +327,7 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
       syslog (LOG_ERR, _("netname2user: (nis+ lookup): %s\n"),
 	      nis_sperrno (res->status));
       nis_freeresult (res);
+      *errnop = errno;
       return NSS_STATUS_TRYAGAIN;
     case NIS_SUCCESS:
     case NIS_S_SUCCESS:
@@ -338,16 +340,14 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
     }
 
   if (res->objects.objects_len > 1)
-    {
-      /*
-       * A principal can have more than one LOCAL entry?
-       * Something wrong with cred table.
-       * Warn user and continue.
-       */
-      syslog (LOG_ALERT,
-	      _("netname2user: LOCAL entry for %s in directory %s not unique"),
-	      netname, domain);
-    }
+    /*
+     * A principal can have more than one LOCAL entry?
+     * Something wrong with cred table.
+     * Warn user and continue.
+     */
+    syslog (LOG_ALERT,
+	    _("netname2user: LOCAL entry for %s in directory %s not unique"),
+	    netname, domain);
   /* Fetch the uid */
   *uidp = atoi (ENTRY_VAL (res->objects.objects_val, 2));
 
@@ -358,7 +358,7 @@ _nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
     }
 
   parse_grp_str (ENTRY_VAL (res->objects.objects_val, 3),
-		 gidp, gidlenp, gidlist);
+		 gidp, gidlenp, gidlist, errnop);
 
   nis_freeresult (res);
   return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-pwd.c b/nis/nss_nisplus/nisplus-pwd.c
index 3974defa47..18114ad7e5 100644
--- a/nis/nss_nisplus/nisplus-pwd.c
+++ b/nis/nss_nisplus/nisplus-pwd.c
@@ -33,10 +33,10 @@ static nis_name tablename_val = NULL;
 static u_long tablename_len = 0;
 
 extern int _nss_nisplus_parse_pwent (nis_result *res, struct passwd *pw,
-				     char *buffer, size_t buflen);
+				     char *buffer, size_t buflen, int *errnop);
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -47,7 +47,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -58,6 +61,7 @@ enum nss_status
 _nss_nisplus_setpwent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -66,7 +70,7 @@ _nss_nisplus_setpwent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    status = _nss_create_tablename ();
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -88,7 +92,8 @@ _nss_nisplus_endpwent (void)
 }
 
 static enum nss_status
-internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
+internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen,
+			     int *errnop)
 {
   int parse_res;
 
@@ -101,10 +106,14 @@ internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
 	{
 	  saved_res = NULL;
           if (tablename_val == NULL)
-            if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-              return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
-	  result = nis_first_entry(tablename_val);
+	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	    return niserr2nss (result->status);
 	}
@@ -113,7 +122,7 @@ internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
 	  nis_result *res;
 
 	  saved_res = result;
-	  res = nis_next_entry(tablename_val, &result->cookie);
+	  res = nis_next_entry (tablename_val, &result->cookie);
 	  result = res;
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	    {
@@ -122,11 +131,13 @@ internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_pwent (result, pw, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_pwent (result, pw, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
 	  result = saved_res;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -140,13 +151,14 @@ internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
+_nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
+			 int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getpwent_r (result, buffer, buflen);
+  status = internal_nisplus_getpwent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -155,13 +167,17 @@ _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
-		     char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL || strlen (name) > 8)
     return NSS_STATUS_NOTFOUND;
@@ -170,7 +186,7 @@ _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
       nis_result *result;
       char buf[strlen (name) + 24 + tablename_len];
 
-      sprintf(buf, "[name=%s],%s", name, tablename_val);
+      sprintf (buf, "[name=%s],%s", name, tablename_val);
 
       result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
@@ -182,12 +198,16 @@ _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
+      parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen,
+					    errnop);
 
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
@@ -198,17 +218,22 @@ _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
 
 enum nss_status
 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
-		     char *buffer, size_t buflen)
+			 char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
+
   {
     int parse_res;
     nis_result *result;
     char buf[100 + tablename_len];
 
-    sprintf(buf, "[uid=%d],%s", uid, tablename_val);
+    sprintf (buf, "[uid=%d],%s", uid, tablename_val);
 
     result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
 
@@ -220,12 +245,15 @@ _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
 	return status;
       }
 
-    parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
+    parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
 
     nis_freeresult (result);
 
     if (parse_res == -1)
-      return NSS_STATUS_TRYAGAIN;
+      {
+	*errnop = ERANGE;
+	return NSS_STATUS_TRYAGAIN;
+      }
 
     if (parse_res)
       return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-rpc.c b/nis/nss_nisplus/nisplus-rpc.c
index 77a7248305..452fb4cc5e 100644
--- a/nis/nss_nisplus/nisplus-rpc.c
+++ b/nis/nss_nisplus/nisplus-rpc.c
@@ -41,7 +41,7 @@ static u_long tablename_len = 0;
 
 static int
 _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
-			   char *buffer, size_t buflen)
+			   char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -52,25 +52,25 @@ _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp(result->objects.objects_val[0].EN_data.en_type,
-             "rpc_tbl") != 0 ||
-      result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val[0].EN_data.en_type,
+		 "rpc_tbl") != 0
+      || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
     return 0;
 
   if (NISENTRYLEN (0, 0, result) >= room_left)
     {
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
   strncpy (first_unused, NISENTRYVAL (0, 0, result),
            NISENTRYLEN (0, 0, result));
   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
   rpc->r_name = first_unused;
-  room_left -= (strlen (first_unused) +1);
-  first_unused += strlen (first_unused) +1;
+  room_left -= strlen (first_unused) + 1;
+  first_unused += strlen (first_unused) + 1;
   rpc->r_number = atoi (NISENTRYVAL (0, 2, result));
   p = first_unused;
 
@@ -98,7 +98,7 @@ _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
   rpc->r_aliases = (char **) first_unused;
   if (room_left < sizeof (char *))
     goto no_more_room;
-  room_left -= (sizeof (char *));
+  room_left -= sizeof (char *);
   rpc->r_aliases[0] = NULL;
 
   i = 0;
@@ -137,7 +137,7 @@ _nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -148,7 +148,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -159,6 +162,7 @@ enum nss_status
 _nss_nisplus_setrpcent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -167,7 +171,7 @@ _nss_nisplus_setrpcent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    status = _nss_create_tablename ();
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -190,7 +194,7 @@ _nss_nisplus_endrpcent (void)
 
 static enum nss_status
 internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
-			      size_t buflen)
+			      size_t buflen, int *errnop)
 {
   int parse_res;
 
@@ -203,10 +207,14 @@ internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
 	{
 	  saved_res = NULL;
           if (tablename_val == NULL)
-            if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-              return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status =  _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
-	  result = nis_first_entry(tablename_val);
+	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	    return niserr2nss (result->status);
 	}
@@ -224,11 +232,13 @@ internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer,
-						  buflen)) == -1)
+      parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer,
+					     buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
 	  result = saved_res;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -243,13 +253,13 @@ internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
 
 enum nss_status
 _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
-			  size_t buflen)
+			  size_t buflen, int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getrpcent_r (result, buffer, buflen);
+  status = internal_nisplus_getrpcent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -258,13 +268,17 @@ _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
 
 enum nss_status
 _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
-			     char *buffer, size_t buflen)
+			     char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL)
     return NSS_STATUS_NOTFOUND;
@@ -280,19 +294,18 @@ _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
 
       /* If we do not find it, try it as original name. But if the
          database is correct, we should find it in the first case, too */
-      if ((result->status != NIS_SUCCESS &&
-           result->status != NIS_S_SUCCESS) ||
-          __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-          strcmp (result->objects.objects_val->EN_data.en_type,
-		  "rpc_tbl") != 0 ||
-          result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
+      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+	  || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+	  || strcmp (result->objects.objects_val->EN_data.en_type,
+		     "rpc_tbl") != 0
+	  || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
         sprintf (buf, "[cname=%s],%s", name, tablename_val);
       else
-        sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
+        sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
 		 tablename_val);
 
       nis_freeresult (result);
-      result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS , NULL, NULL);
+      result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS , NULL, NULL);
 
       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
 	{
@@ -302,12 +315,16 @@ _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
+      parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
+					     errnop);
 
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
@@ -318,11 +335,15 @@ _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
 
 enum nss_status
 _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
-				char *buffer, size_t buflen)
+			       char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   {
     int parse_res;
@@ -341,12 +362,16 @@ _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
 	return status;
       }
 
-    parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
+    parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen,
+					   errnop);
 
     nis_freeresult (result);
 
     if (parse_res == -1)
-      return NSS_STATUS_TRYAGAIN;
+      {
+	*errnop = ERANGE;
+	return NSS_STATUS_TRYAGAIN;
+      }
 
     if (parse_res)
       return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-service.c b/nis/nss_nisplus/nisplus-service.c
index 95a8cfa74b..1cb1aec8d7 100644
--- a/nis/nss_nisplus/nisplus-service.c
+++ b/nis/nss_nisplus/nisplus-service.c
@@ -41,7 +41,7 @@ static u_long tablename_len = 0;
 
 static int
 _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
-			    char *buffer, size_t buflen)
+			    char *buffer, size_t buflen, int *errnop)
 {
   char *first_unused = buffer;
   size_t room_left = buflen;
@@ -51,17 +51,17 @@ _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
   if (result == NULL)
     return 0;
 
-  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
-      __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-      strcmp (result->objects.objects_val->EN_data.en_type,
-	      "services_tbl") != 0 ||
-      result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
+  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+      || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+      || strcmp (result->objects.objects_val->EN_data.en_type,
+		 "services_tbl") != 0
+      || result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
     return 0;
 
   if (NISENTRYLEN (0, 0, result) >= room_left)
     {
     no_more_room:
-      __set_errno (ERANGE);
+      *errnop = ERANGE;
       return -1;
     }
   strncpy (first_unused, NISENTRYVAL (0, 0, result),
@@ -77,8 +77,8 @@ _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
            NISENTRYLEN (0, 2, result));
   first_unused[NISENTRYLEN (0, 2, result)] = '\0';
   serv->s_proto = first_unused;
-  room_left -= (strlen (first_unused) +1);
-  first_unused += strlen (first_unused) +1;
+  room_left -= strlen (first_unused) + 1;
+  first_unused += strlen (first_unused) + 1;
 
   serv->s_port = atoi (NISENTRYVAL (0, 3, result));
   p = first_unused;
@@ -143,7 +143,7 @@ _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
 }
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -154,7 +154,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -165,6 +168,7 @@ enum nss_status
 _nss_nisplus_setservent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -173,7 +177,7 @@ _nss_nisplus_setservent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    status = _nss_create_tablename ();
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -196,7 +200,7 @@ _nss_nisplus_endservent (void)
 
 static enum nss_status
 internal_nisplus_getservent_r (struct servent *serv, char *buffer,
-			       size_t buflen)
+			       size_t buflen, int *errnop)
 {
   int parse_res;
 
@@ -209,8 +213,12 @@ internal_nisplus_getservent_r (struct servent *serv, char *buffer,
 	{
 	  saved_res = NULL;
           if (tablename_val == NULL)
-            if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-              return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
 	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
@@ -230,11 +238,13 @@ internal_nisplus_getservent_r (struct servent *serv, char *buffer,
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_servent (result, serv, buffer,
-						   buflen)) == -1)
+      parse_res = _nss_nisplus_parse_servent (result, serv, buffer,
+					      buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
 	  result = saved_res;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -250,13 +260,13 @@ internal_nisplus_getservent_r (struct servent *serv, char *buffer,
 
 enum nss_status
 _nss_nisplus_getservent_r (struct servent *result, char *buffer,
-			   size_t buflen)
+			   size_t buflen, int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getservent_r (result, buffer, buflen);
+  status = internal_nisplus_getservent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -266,17 +276,21 @@ _nss_nisplus_getservent_r (struct servent *result, char *buffer,
 enum nss_status
 _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
 			      struct servent *serv,
-			      char *buffer, size_t buflen)
+			      char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL || protocol == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       return NSS_STATUS_NOTFOUND;
     }
   else
@@ -292,12 +306,11 @@ _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
 
       /* If we do not find it, try it as original name. But if the
          database is correct, we should find it in the first case, too */
-      if ((result->status != NIS_SUCCESS &&
-	   result->status != NIS_S_SUCCESS) ||
-	  __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
-	  strcmp (result->objects.objects_val->EN_data.en_type,
-		  "services_tbl") != 0 ||
-	  result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
+      if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+	  || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
+	  || strcmp (result->objects.objects_val->EN_data.en_type,
+		     "services_tbl") != 0
+	  || result->objects.objects_val->EN_data.en_cols.en_cols_len < 4)
 	sprintf (buf, "[cname=%s,proto=%s],%s", name, protocol,
 		 tablename_val);
       else
@@ -315,11 +328,15 @@ _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen);
+      parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
+					      errnop);
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
@@ -331,15 +348,19 @@ _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
 enum nss_status
 _nss_nisplus_getservbynumber_r (const int number, const char *protocol,
 				struct servent *serv,
-				char *buffer, size_t buflen)
+				char *buffer, size_t buflen, int *errnop)
 {
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (protocol == NULL)
     {
-      __set_errno (EINVAL);
+      *errnop = EINVAL;
       return NSS_STATUS_NOTFOUND;
     }
   else
@@ -361,11 +382,15 @@ _nss_nisplus_getservbynumber_r (const int number, const char *protocol,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen);
+      parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
+					      errnop);
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (parse_res)
 	return NSS_STATUS_SUCCESS;
diff --git a/nis/nss_nisplus/nisplus-spwd.c b/nis/nss_nisplus/nisplus-spwd.c
index 1b3f68325e..54bbc32f3c 100644
--- a/nis/nss_nisplus/nisplus-spwd.c
+++ b/nis/nss_nisplus/nisplus-spwd.c
@@ -34,7 +34,7 @@ static nis_name tablename_val = NULL;
 static u_long tablename_len = 0;
 
 static enum nss_status
-_nss_create_tablename (void)
+_nss_create_tablename (int *errnop)
 {
   if (tablename_val == NULL)
     {
@@ -45,7 +45,10 @@ _nss_create_tablename (void)
       p = __stpcpy (p, nis_local_directory ());
       tablename_val = __strdup (buf);
       if (tablename_val == NULL)
-        return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = errno;
+	  return NSS_STATUS_TRYAGAIN;
+	}
       tablename_len = strlen (tablename_val);
     }
   return NSS_STATUS_SUCCESS;
@@ -55,6 +58,7 @@ enum nss_status
 _nss_nisplus_setspent (void)
 {
   enum nss_status status = NSS_STATUS_SUCCESS;
+  int err;
 
   __libc_lock_lock (lock);
 
@@ -63,7 +67,7 @@ _nss_nisplus_setspent (void)
   result = NULL;
 
   if (tablename_val == NULL)
-    status = _nss_create_tablename ();
+    status = _nss_create_tablename (&err);
 
   __libc_lock_unlock (lock);
 
@@ -85,7 +89,8 @@ _nss_nisplus_endspent (void)
 }
 
 static enum nss_status
-internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
+internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
+			     int *errnop)
 {
   int parse_res;
 
@@ -99,8 +104,12 @@ internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
 	  saved_res = NULL;
 
           if (tablename_val == NULL)
-            if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-              return NSS_STATUS_UNAVAIL;
+	    {
+	      enum nss_status status = _nss_create_tablename (errnop);
+
+	      if (status != NSS_STATUS_SUCCESS)
+		return status;
+	    }
 
 	  result = nis_first_entry (tablename_val);
 	  if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
@@ -120,11 +129,13 @@ internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
 	    }
 	}
 
-      if ((parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
-						 buflen)) == -1)
+      parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
+					    buflen, errnop);
+      if (parse_res == -1)
 	{
 	  nis_freeresult (result);
 	  result = saved_res;
+	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
       else
@@ -138,13 +149,14 @@ internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
 }
 
 enum nss_status
-_nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen)
+_nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen,
+			 int *errnop)
 {
   int status;
 
   __libc_lock_lock (lock);
 
-  status = internal_nisplus_getspent_r (result, buffer, buflen);
+  status = internal_nisplus_getspent_r (result, buffer, buflen, errnop);
 
   __libc_lock_unlock (lock);
 
@@ -153,13 +165,17 @@ _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
-		     char *buffer, size_t buflen)
+		     char *buffer, size_t buflen, int *errnop)
 {
   int parse_res;
 
   if (tablename_val == NULL)
-    if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
-      return NSS_STATUS_UNAVAIL;
+    {
+      enum nss_status status = _nss_create_tablename (errnop);
+
+      if (status != NSS_STATUS_SUCCESS)
+	return status;
+    }
 
   if (name == NULL || strlen (name) > 8)
     return NSS_STATUS_NOTFOUND;
@@ -180,11 +196,15 @@ _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
 	  return status;
 	}
 
-      parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen);
+      parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen,
+					    errnop);
       nis_freeresult (result);
 
       if (parse_res == -1)
-	return NSS_STATUS_TRYAGAIN;
+	{
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
 
       if (parse_res)
 	return NSS_STATUS_SUCCESS;