* New version 2.21.999
[alpine.git] / pith / osdep / canonicl.c
blob9610d069f3916abb4a3e2e6ad018393058fe4cb2
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: canonicl.c 764 2007-10-23 23:44:49Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../../c-client/mail.h"
20 extern unsigned char *lcase(unsigned char *);
22 #ifdef _WINDOWS
23 /* wingdi.h uses ERROR (!) and we aren't using the c-client ERROR so... */
24 #undef ERROR
25 #endif
27 #include <system.h>
29 #include "canonicl.h"
32 /*----------------------------------------------------------------------
33 Return canonical form of host name ala c-client (UNIX version).
35 Args: host -- The host name
37 Result: Canonical form, or input argument (worst case)
39 You can call it twice without worrying about copying
40 the results, but not more than twice.
41 ----*/
42 char *
43 canonical_name(char *host)
45 struct hostent *hent;
46 char tmp[MAILTMPLEN];
47 static int whichbuf = 0;
48 static char buf[2][NETMAXHOST+1];
49 char *b;
51 whichbuf = (whichbuf + 1) % 2;
52 b = buf[whichbuf];
54 /* domain literal is easy */
55 if (host[0] == '[' && host[(strlen (host))-1] == ']')
56 strncpy(b, host, NETMAXHOST);
57 else{
58 strncpy(tmp, host, sizeof(tmp)-1);
59 tmp[sizeof(tmp)-1] = '\0';
61 hent = gethostbyname((char *) lcase((unsigned char *) tmp));
62 if(hent && hent->h_name)
63 strncpy(b, hent->h_name, NETMAXHOST);
64 else
65 strncpy(b, host, NETMAXHOST);
68 b[NETMAXHOST] = '\0';
69 return(b);