If the call to gethostname(3) fails, the name of the local host is
set to localhost:
#include
#include
#include
* Prior to V8 sendmail, the canonical name was stored in the $w macro (?§21.9.101 on page 850) and sendmail
initialized only the $j macro (?§21.9.59 on page 830). Beginning withV8, sendmail initializes bothof those
variables, among others (?§21.1 on page 785).
This is the Title of the Book, eMatter Edition
Copyright ?© 2007 O??™Reilly & Associates, Inc. All rights reserved.
326 | Chapter 9: DNS and sendmail
#include
#include
main( )
{
char hostbuf[MAXHOSTNAMELEN];
struct hostent *hp;
/* Get the local host name */
if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
{
strcpy(hostbuf, "localhost");
}
printf("hostname = "%s"\n", hostbuf);
/* canonicalize it and get aliases */
if((hp = gethostbyname(hostbuf)) = = NULL)
{
perror("gethostbyname");
exit(2);
}
printf("canonical = "%s"\n", hp->h_name);
while (*hp->h_aliases != NULL)
{
printf("alias: "%s"\n", *hp->h_aliases);
++hp->h_aliases;
}
}
The local hostname is then given to the gethostbyname routine to obtain the canonical
name for the local host. That same routine also returns any aliases (other names
for the local host). Note that, if you defined NETINET6 (?§3.4.32 on page 126) when
compiling (for IPv6 support), you must use getipnodebyname(3) in place of gethostbyname(
3).
Pages:
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593