+\f/*
+** MAPHOSTNAME -- turn a hostname into canonical form
+**
+** Parameters:
+** map -- a pointer to this map (unused).
+** hbuf -- a buffer containing a hostname.
+** hbsize -- the size of hbuf.
+** avp -- unused -- for compatibility with other mapping
+** functions.
+**
+** Returns:
+** The mapping, if found.
+** NULL if no mapping found.
+**
+** Side Effects:
+** Looks up the host specified in hbuf. If it is not
+** the canonical name for that host, return the canonical
+** name.
+*/
+
+char *
+maphostname(map, hbuf, hbsize, avp)
+ MAP *map;
+ char *hbuf;
+ int hbsize;
+ char **avp;
+{
+ register struct hostent *hp;
+ u_long in_addr;
+ char *cp;
+ struct hostent *gethostbyaddr();
+
+ /* allow room for null */
+ hbsize--;
+
+ /*
+ * If first character is a bracket, then it is an address
+ * lookup. Address is copied into a temporary buffer to
+ * strip the brackets and to preserve hbuf if address is
+ * unknown.
+ */
+
+ if (*hbuf != '[')
+ {
+ extern bool getcanonname();
+
+ if (getcanonname(hbuf, hbsize))
+ return hbuf;
+ else
+ return NULL;
+ }
+ if ((cp = strchr(hbuf, ']')) == NULL)
+ return (NULL);
+ *cp = '\0';
+ in_addr = inet_addr(&hbuf[1]);
+ hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
+ if (hp == NULL)
+ return (NULL);
+
+ /* found a match -- copy and dot terminate */
+ if (strlen(hp->h_name) > hbsize)
+ hp->h_name[hbsize] = '\0';
+ (void) strcpy(hbuf, hp->h_name);
+ return hbuf;
+}