A.6 Relationship Between errno and h_errno

Since the routines that set h_errno can also set errno, this section explains the relationship between the two variables.

A given routine failure does not set both h_errno and errno for the same failure. A routine failure sets either h_errno or errno depending on which is appropriate for the failing condition.

For example, consider the following program:

#include   <errno.h>
#include   <stdio.h>
#include   <netdb.h>

main()
 {
   static char hostname [256];
   struct hostent *hostentptr;

   errno = 0;
   h_errno = 0;
   if ((hostentptr = gethostbyname ("hndy")) == NULL)
   {
     printf ("unknown host name errno is: %d h_errno is: %d\n",errno, h_errno);
     perror ("p_gethostbyname");
     herror ("h_gethostbyname");
   }

   errno = 0;
   h_errno = 0;
   if ((hostentptr = gethostbyname (0)) == NULL)
   {
     printf ("illegal host name errno is: %d h_errno is: %d\n", errno, h_errno);
     perror ("p_gethostbyname");
     herror ("h_gethostbyname");
   }
 }

In the first call to gethostbyname, the host name parameter "hndy" does not represent a known host. In this case, gethostbyname sets h_errno to HOST_NOT_FOUND, but does not set errno.

The call to perror in this example would output:

    p_gethostbyname: Error 0

The call to herror in this example would print a message describing the failure:

    h_gethostbyname: Unknown

In the second call to gethostbyname, the host name parameter is 0 (zero), an invalid argument. In this case, gethostbyname sets errno to EINVAL, but does not set h_errno.

A call to perror would print a message describing the failure:

    p_gethostbyname: Invalid

A call to herror would print:

    h_gethostbyname: Error 0


Previous Page | Next Page | Table of Contents | Index