Fix bad sentence.
[platform/upstream/c-ares.git] / acountry.c
1 /*
2  * $Id$
3  *
4  * IP-address/hostname to country converter.
5  *
6  * Problem; you want to know where IP a.b.c.d is located.
7  *
8  * Use ares_gethostbyname ("d.c.b.a.zz.countries.nerd.dk")
9  * and get the CNAME (host->h_name). Result will be:
10  *   CNAME = zz<CC>.countries.nerd.dk with address 127.0.x.y (ver 1) or
11  *   CNAME = <a.b.c.d>.zz.countries.nerd.dk with address 127.0.x.y (ver 2)
12  *
13  * The 2 letter country code is in <CC> and the ISO-3166 country
14  * number is in x.y (number = x*256 + y). Version 2 of the protocol is missing
15  * the <CC> number.
16  *
17  * Ref: http://countries.nerd.dk/more.html
18  *
19  * Written by G. Vanem <gvanem@broadpark.no> 2006, 2007
20  *
21  * NB! This program may not be big-endian aware.
22  *
23  * Permission to use, copy, modify, and distribute this
24  * software and its documentation for any purpose and without
25  * fee is hereby granted, provided that the above copyright
26  * notice appear in all copies and that both that copyright
27  * notice and this permission notice appear in supporting
28  * documentation, and that the name of M.I.T. not be used in
29  * advertising or publicity pertaining to distribution of the
30  * software without specific, written prior permission.
31  * M.I.T. makes no representations about the suitability of
32  * this software for any purpose.  It is provided "as is"
33  * without express or implied warranty.
34  */
35
36 #include "setup.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <ctype.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
49
50 #if defined(WIN32) && !defined(WATT32)
51   #include <winsock.h>
52 #else
53   #include <sys/socket.h>
54   #include <arpa/inet.h>
55   #include <netinet/in.h>
56   #include <netdb.h>
57 #endif
58
59 #include "ares.h"
60 #include "ares_getopt.h"
61 #include "inet_net_pton.h"
62 #include "inet_ntop.h"
63
64 #ifndef HAVE_STRDUP
65 #  include "ares_strdup.h"
66 #  define strdup(ptr) ares_strdup(ptr)
67 #endif
68
69 #ifndef HAVE_STRCASECMP
70 #  include "ares_strcasecmp.h"
71 #  define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
72 #endif
73
74 #ifndef HAVE_STRNCASECMP
75 #  include "ares_strcasecmp.h"
76 #  define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
77 #endif
78
79 #ifndef INADDR_NONE
80 #define INADDR_NONE 0xffffffff
81 #endif
82
83 static const char *usage      = "acountry [-vh?] {host|addr} ...\n";
84 static const char  nerd_fmt[] = "%u.%u.%u.%u.zz.countries.nerd.dk";
85 static const char *nerd_ver1  = nerd_fmt + 14;
86 static const char *nerd_ver2  = nerd_fmt + 11;
87 static int         verbose    = 0;
88
89 #define TRACE(fmt) do {               \
90                      if (verbose > 0) \
91                        printf fmt ;   \
92                    } while (0)
93
94 static void wait_ares(ares_channel channel);
95 static void callback(void *arg, int status, int timeouts, struct hostent *host);
96 static void callback2(void *arg, int status, int timeouts, struct hostent *host);
97 static void find_country_from_cname(const char *cname, struct in_addr addr);
98
99 static void Abort(const char *fmt, ...)
100 {
101   va_list args;
102   va_start(args, fmt);
103   vfprintf(stderr, fmt, args);
104   va_end(args);
105   exit(1);
106 }
107
108 int main(int argc, char **argv)
109 {
110   ares_channel channel;
111   int    ch, status;
112
113 #if defined(WIN32) && !defined(WATT32)
114   WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
115   WSADATA wsaData;
116   WSAStartup(wVersionRequested, &wsaData);
117 #endif
118
119 #ifdef WIN32
120   LoadLibrary ("exchndl.dll");
121 #endif
122
123   status = ares_library_init(ARES_LIB_INIT_ALL);
124   if (status != ARES_SUCCESS)
125     {
126       fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
127       return 1;
128     }
129
130   while ((ch = ares_getopt(argc, argv, "dvh?")) != -1)
131     switch (ch)
132       {
133       case 'd':
134 #ifdef WATT32
135         dbug_init();
136 #endif
137         break;
138       case 'v':
139         verbose++;
140         break;
141       case 'h':
142       case '?':
143       default:
144         Abort(usage);
145       }
146
147   argc -= optind;
148   argv += optind;
149   if (argc < 1)
150      Abort(usage);
151
152   status = ares_init(&channel);
153   if (status != ARES_SUCCESS)
154     {
155       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
156       return 1;
157     }
158
159   /* Initiate the queries, one per command-line argument. */
160   for ( ; *argv; argv++)
161     {
162       struct in_addr addr;
163       char buf[100];
164
165       /* If this fails, assume '*argv' is a host-name that
166        * must be resolved first
167        */
168       if (ares_inet_pton(AF_INET, *argv, &addr) != 1)
169         {
170           ares_gethostbyname(channel, *argv, AF_INET, callback2, &addr);
171           wait_ares(channel);
172           if (addr.s_addr == INADDR_NONE)
173             {
174               printf("Failed to lookup %s\n", *argv);
175               continue;
176             }
177         }
178
179       sprintf(buf, nerd_fmt,
180               (unsigned int)(addr.s_addr >> 24),
181               (unsigned int)((addr.s_addr >> 16) & 255),
182               (unsigned int)((addr.s_addr >> 8) & 255),
183               (unsigned int)(addr.s_addr & 255));
184       TRACE(("Looking up %s...", buf));
185       fflush(stdout);
186       ares_gethostbyname(channel, buf, AF_INET, callback, buf);
187     }
188
189   wait_ares(channel);
190   ares_destroy(channel);
191
192   ares_library_cleanup();
193
194 #if defined(WIN32) && !defined(WATT32)
195   WSACleanup();
196 #endif
197
198   return 0;
199 }
200
201 /*
202  * Wait for the queries to complete.
203  */
204 static void wait_ares(ares_channel channel)
205 {
206   while (1)
207     {
208       struct timeval *tvp, tv;
209       fd_set read_fds, write_fds;
210       int nfds;
211
212       FD_ZERO(&read_fds);
213       FD_ZERO(&write_fds);
214       nfds = ares_fds(channel, &read_fds, &write_fds);
215       if (nfds == 0)
216         break;
217       tvp = ares_timeout(channel, NULL, &tv);
218       select(nfds, &read_fds, &write_fds, NULL, tvp);
219       ares_process(channel, &read_fds, &write_fds);
220     }
221 }
222
223 /*
224  * This is the callback used when we have the IP-address of interest.
225  * Extract the CNAME and figure out the country-code from it.
226  */
227 static void callback(void *arg, int status, int timeouts, struct hostent *host)
228 {
229   const char *name = (const char*)arg;
230   const char *cname;
231   char buf[20];
232
233   (void)timeouts;
234
235   if (!host || status != ARES_SUCCESS)
236     {
237       printf("Failed to lookup %s: %s\n", name, ares_strerror(status));
238       return;
239     }
240
241   TRACE(("\nFound address %s, name %s\n",
242          ares_inet_ntop(AF_INET,(const char*)host->h_addr,buf,sizeof(buf)),
243          host->h_name));
244
245   cname = host->h_name;  /* CNAME gets put here */
246   if (!cname)
247     printf("Failed to get CNAME for %s\n", name);
248   else
249     find_country_from_cname(cname, *(struct in_addr*)host->h_addr);
250 }
251
252 /*
253  * This is the callback used to obtain the IP-address of the host of interest.
254  */
255 static void callback2(void *arg, int status, int timeouts, struct hostent *host)
256 {
257   struct in_addr *addr = (struct in_addr*) arg;
258
259   (void)timeouts;
260   if (!host || status != ARES_SUCCESS)
261     memset(addr, INADDR_NONE, sizeof(*addr));
262   else
263     memcpy(addr, host->h_addr, sizeof(*addr));
264 }
265
266 struct search_list {
267        int         country_number; /* ISO-3166 country number */
268        char        short_name[3];  /* A2 short country code */
269        const char *long_name;      /* normal country name */
270      };
271
272 static const struct search_list *list_lookup(int number, const struct search_list *list, int num)
273 {
274   while (num > 0 && list->long_name)
275     {
276       if (list->country_number == number)
277         return (list);
278       num--;
279       list++;
280     }
281   return (NULL);
282 }
283
284 /*
285  * Ref: ftp://ftp.ripe.net/iso3166-countrycodes.txt
286  */
287 static const struct search_list country_list[] = {
288        {   4, "af", "Afghanistan"                          },
289        { 248, "ax", "Ã…land Island"                         },
290        {   8, "al", "Albania"                              },
291        {  12, "dz", "Algeria"                              },
292        {  16, "as", "American Samoa"                       },
293        {  20, "ad", "Andorra"                              },
294        {  24, "ao", "Angola"                               },
295        { 660, "ai", "Anguilla"                             },
296        {  10, "aq", "Antarctica"                           },
297        {  28, "ag", "Antigua & Barbuda"                    },
298        {  32, "ar", "Argentina"                            },
299        {  51, "am", "Armenia"                              },
300        { 533, "aw", "Aruba"                                },
301        {  36, "au", "Australia"                            },
302        {  40, "at", "Austria"                              },
303        {  31, "az", "Azerbaijan"                           },
304        {  44, "bs", "Bahamas"                              },
305        {  48, "bh", "Bahrain"                              },
306        {  50, "bd", "Bangladesh"                           },
307        {  52, "bb", "Barbados"                             },
308        { 112, "by", "Belarus"                              },
309        {  56, "be", "Belgium"                              },
310        {  84, "bz", "Belize"                               },
311        { 204, "bj", "Benin"                                },
312        {  60, "bm", "Bermuda"                              },
313        {  64, "bt", "Bhutan"                               },
314        {  68, "bo", "Bolivia"                              },
315        {  70, "ba", "Bosnia & Herzegowina"                 },
316        {  72, "bw", "Botswana"                             },
317        {  74, "bv", "Bouvet Island"                        },
318        {  76, "br", "Brazil"                               },
319        {  86, "io", "British Indian Ocean Territory"       },
320        {  96, "bn", "Brunei Darussalam"                    },
321        { 100, "bg", "Bulgaria"                             },
322        { 854, "bf", "Burkina Faso"                         },
323        { 108, "bi", "Burundi"                              },
324        { 116, "kh", "Cambodia"                             },
325        { 120, "cm", "Cameroon"                             },
326        { 124, "ca", "Canada"                               },
327        { 132, "cv", "Cape Verde"                           },
328        { 136, "ky", "Cayman Islands"                       },
329        { 140, "cf", "Central African Republic"             },
330        { 148, "td", "Chad"                                 },
331        { 152, "cl", "Chile"                                },
332        { 156, "cn", "China"                                },
333        { 162, "cx", "Christmas Island"                     },
334        { 166, "cc", "Cocos Islands"                        },
335        { 170, "co", "Colombia"                             },
336        { 174, "km", "Comoros"                              },
337        { 178, "cg", "Congo"                                },
338        { 180, "cd", "Congo"                                },
339        { 184, "ck", "Cook Islands"                         },
340        { 188, "cr", "Costa Rica"                           },
341        { 384, "ci", "Cote d'Ivoire"                        },
342        { 191, "hr", "Croatia"                              },
343        { 192, "cu", "Cuba"                                 },
344        { 196, "cy", "Cyprus"                               },
345        { 203, "cz", "Czech Republic"                       },
346        { 208, "dk", "Denmark"                              },
347        { 262, "dj", "Djibouti"                             },
348        { 212, "dm", "Dominica"                             },
349        { 214, "do", "Dominican Republic"                   },
350        { 218, "ec", "Ecuador"                              },
351        { 818, "eg", "Egypt"                                },
352        { 222, "sv", "El Salvador"                          },
353        { 226, "gq", "Equatorial Guinea"                    },
354        { 232, "er", "Eritrea"                              },
355        { 233, "ee", "Estonia"                              },
356        { 231, "et", "Ethiopia"                             },
357        { 238, "fk", "Falkland Islands"                     },
358        { 234, "fo", "Faroe Islands"                        },
359        { 242, "fj", "Fiji"                                 },
360        { 246, "fi", "Finland"                              },
361        { 250, "fr", "France"                               },
362        { 249, "fx", "France, Metropolitan"                 },
363        { 254, "gf", "French Guiana"                        },
364        { 258, "pf", "French Polynesia"                     },
365        { 260, "tf", "French Southern Territories"          },
366        { 266, "ga", "Gabon"                                },
367        { 270, "gm", "Gambia"                               },
368        { 268, "ge", "Georgia"                              },
369        { 276, "de", "Germany"                              },
370        { 288, "gh", "Ghana"                                },
371        { 292, "gi", "Gibraltar"                            },
372        { 300, "gr", "Greece"                               },
373        { 304, "gl", "Greenland"                            },
374        { 308, "gd", "Grenada"                              },
375        { 312, "gp", "Guadeloupe"                           },
376        { 316, "gu", "Guam"                                 },
377        { 320, "gt", "Guatemala"                            },
378        { 324, "gn", "Guinea"                               },
379        { 624, "gw", "Guinea-Bissau"                        },
380        { 328, "gy", "Guyana"                               },
381        { 332, "ht", "Haiti"                                },
382        { 334, "hm", "Heard & Mc Donald Islands"            },
383        { 336, "va", "Vatican City"                         },
384        { 340, "hn", "Honduras"                             },
385        { 344, "hk", "Hong kong"                            },
386        { 348, "hu", "Hungary"                              },
387        { 352, "is", "Iceland"                              },
388        { 356, "in", "India"                                },
389        { 360, "id", "Indonesia"                            },
390        { 364, "ir", "Iran"                                 },
391        { 368, "iq", "Iraq"                                 },
392        { 372, "ie", "Ireland"                              },
393        { 376, "il", "Israel"                               },
394        { 380, "it", "Italy"                                },
395        { 388, "jm", "Jamaica"                              },
396        { 392, "jp", "Japan"                                },
397        { 400, "jo", "Jordan"                               },
398        { 398, "kz", "Kazakhstan"                           },
399        { 404, "ke", "Kenya"                                },
400        { 296, "ki", "Kiribati"                             },
401        { 408, "kp", "Korea (north)"                        },
402        { 410, "kr", "Korea (south)"                        },
403        { 414, "kw", "Kuwait"                               },
404        { 417, "kg", "Kyrgyzstan"                           },
405        { 418, "la", "Laos"                                 },
406        { 428, "lv", "Latvia"                               },
407        { 422, "lb", "Lebanon"                              },
408        { 426, "ls", "Lesotho"                              },
409        { 430, "lr", "Liberia"                              },
410        { 434, "ly", "Libya"                                },
411        { 438, "li", "Liechtenstein"                        },
412        { 440, "lt", "Lithuania"                            },
413        { 442, "lu", "Luxembourg"                           },
414        { 446, "mo", "Macao"                                },
415        { 807, "mk", "Macedonia"                            },
416        { 450, "mg", "Madagascar"                           },
417        { 454, "mw", "Malawi"                               },
418        { 458, "my", "Malaysia"                             },
419        { 462, "mv", "Maldives"                             },
420        { 466, "ml", "Mali"                                 },
421        { 470, "mt", "Malta"                                },
422        { 584, "mh", "Marshall Islands"                     },
423        { 474, "mq", "Martinique"                           },
424        { 478, "mr", "Mauritania"                           },
425        { 480, "mu", "Mauritius"                            },
426        { 175, "yt", "Mayotte"                              },
427        { 484, "mx", "Mexico"                               },
428        { 583, "fm", "Micronesia"                           },
429        { 498, "md", "Moldova"                              },
430        { 492, "mc", "Monaco"                               },
431        { 496, "mn", "Mongolia"                             },
432        { 500, "ms", "Montserrat"                           },
433        { 504, "ma", "Morocco"                              },
434        { 508, "mz", "Mozambique"                           },
435        { 104, "mm", "Myanmar"                              },
436        { 516, "na", "Namibia"                              },
437        { 520, "nr", "Nauru"                                },
438        { 524, "np", "Nepal"                                },
439        { 528, "nl", "Netherlands"                          },
440        { 530, "an", "Netherlands Antilles"                 },
441        { 540, "nc", "New Caledonia"                        },
442        { 554, "nz", "New Zealand"                          },
443        { 558, "ni", "Nicaragua"                            },
444        { 562, "ne", "Niger"                                },
445        { 566, "ng", "Nigeria"                              },
446        { 570, "nu", "Niue"                                 },
447        { 574, "nf", "Norfolk Island"                       },
448        { 580, "mp", "Northern Mariana Islands"             },
449        { 578, "no", "Norway"                               },
450        { 512, "om", "Oman"                                 },
451        { 586, "pk", "Pakistan"                             },
452        { 585, "pw", "Palau"                                },
453        { 275, "ps", "Palestinian Territory"                },
454        { 591, "pa", "Panama"                               },
455        { 598, "pg", "Papua New Guinea"                     },
456        { 600, "py", "Paraguay"                             },
457        { 604, "pe", "Peru"                                 },
458        { 608, "ph", "Philippines"                          },
459        { 612, "pn", "Pitcairn"                             },
460        { 616, "pl", "Poland"                               },
461        { 620, "pt", "Portugal"                             },
462        { 630, "pr", "Puerto Rico"                          },
463        { 634, "qa", "Qatar"                                },
464        { 638, "re", "Reunion"                              },
465        { 642, "ro", "Romania"                              },
466        { 643, "ru", "Russia"                               },
467        { 646, "rw", "Rwanda"                               },
468        { 659, "kn", "Saint Kitts & Nevis"                  },
469        { 662, "lc", "Saint Lucia"                          },
470        { 670, "vc", "Saint Vincent"                        },
471        { 882, "ws", "Samoa"                                },
472        { 674, "sm", "San Marino"                           },
473        { 678, "st", "Sao Tome & Principe"                  },
474        { 682, "sa", "Saudi Arabia"                         },
475        { 686, "sn", "Senegal"                              },
476        { 891, "cs", "Serbia and Montenegro"                },
477        { 690, "sc", "Seychelles"                           },
478        { 694, "sl", "Sierra Leone"                         },
479        { 702, "sg", "Singapore"                            },
480        { 703, "sk", "Slovakia"                             },
481        { 705, "si", "Slovenia"                             },
482        {  90, "sb", "Solomon Islands"                      },
483        { 706, "so", "Somalia"                              },
484        { 710, "za", "South Africa"                         },
485        { 239, "gs", "South Georgia"                        },
486        { 724, "es", "Spain"                                },
487        { 144, "lk", "Sri Lanka"                            },
488        { 654, "sh", "St. Helena"                           },
489        { 666, "pm", "St. Pierre & Miquelon"                },
490        { 736, "sd", "Sudan"                                },
491        { 740, "sr", "Suriname"                             },
492        { 744, "sj", "Svalbard & Jan Mayen Islands"         },
493        { 748, "sz", "Swaziland"                            },
494        { 752, "se", "Sweden"                               },
495        { 756, "ch", "Switzerland"                          },
496        { 760, "sy", "Syrian Arab Republic"                 },
497        { 626, "tl", "Timor-Leste"                          },
498        { 158, "tw", "Taiwan"                               },
499        { 762, "tj", "Tajikistan"                           },
500        { 834, "tz", "Tanzania"                             },
501        { 764, "th", "Thailand"                             },
502        { 768, "tg", "Togo"                                 },
503        { 772, "tk", "Tokelau"                              },
504        { 776, "to", "Tonga"                                },
505        { 780, "tt", "Trinidad & Tobago"                    },
506        { 788, "tn", "Tunisia"                              },
507        { 792, "tr", "Turkey"                               },
508        { 795, "tm", "Turkmenistan"                         },
509        { 796, "tc", "Turks & Caicos Islands"               },
510        { 798, "tv", "Tuvalu"                               },
511        { 800, "ug", "Uganda"                               },
512        { 804, "ua", "Ukraine"                              },
513        { 784, "ae", "United Arab Emirates"                 },
514        { 826, "gb", "United Kingdom"                       },
515        { 840, "us", "United States"                        },
516        { 581, "um", "United States Minor Outlying Islands" },
517        { 858, "uy", "Uruguay"                              },
518        { 860, "uz", "Uzbekistan"                           },
519        { 548, "vu", "Vanuatu"                              },
520        { 862, "ve", "Venezuela"                            },
521        { 704, "vn", "Vietnam"                              },
522        {  92, "vg", "Virgin Islands (British)"             },
523        { 850, "vi", "Virgin Islands (US)"                  },
524        { 876, "wf", "Wallis & Futuna Islands"              },
525        { 732, "eh", "Western Sahara"                       },
526        { 887, "ye", "Yemen"                                },
527        { 894, "zm", "Zambia"                               },
528        { 716, "zw", "Zimbabwe"                             }
529      };
530
531 /*
532  * Check if start of 'str' is simply an IPv4 address.
533  */
534 #define BYTE_OK(x) ((x) >= 0 && (x) <= 255)
535
536 static int is_addr(char *str, char **end)
537 {
538   int a0, a1, a2, a3, num, rc = 0, length = 0;
539
540   num = sscanf(str,"%3d.%3d.%3d.%3d%n",&a0,&a1,&a2,&a3,&length);
541   if( (num == 4) &&
542       BYTE_OK(a0) && BYTE_OK(a1) && BYTE_OK(a2) && BYTE_OK(a3) &&
543       length >= (3+4))
544     {
545       rc = 1;
546       *end = str + length;
547     }
548   return rc;
549 }
550
551 /*
552  * Find the country-code and name from the CNAME. E.g.:
553  *   version 1: CNAME = zzno.countries.nerd.dk with address 127.0.2.66
554  *              yields ccode_A" = "no" and cnumber 578 (2.66).
555  *   version 2: CNAME = <a.b.c.d>.zz.countries.nerd.dk with address 127.0.2.66
556  *              yields cnumber 578 (2.66). ccode_A is "";
557  */
558 static void find_country_from_cname(const char *cname, struct in_addr addr)
559 {
560   const struct search_list *country;
561   char  ccode_A2[3], *ccopy, *dot_4;
562   int   cnumber, z0, z1, ver_1, ver_2;
563   unsigned long ip;
564
565   ip = ntohl(addr.s_addr);
566   z0 = tolower(cname[0]);
567   z1 = tolower(cname[1]);
568   ccopy = strdup(cname);
569   dot_4 = NULL;
570
571   ver_1 = (z0 == 'z' && z1 == 'z' && !strcasecmp(cname+4,nerd_ver1));
572   ver_2 = (is_addr(ccopy,&dot_4) && !strcasecmp(dot_4,nerd_ver2));
573
574   if (ver_1)
575     {
576       const char *dot = strchr(cname, '.');
577       if ((z0 != 'z' && z1 != 'z') || dot != cname+4)
578         {
579           printf("Unexpected CNAME %s (ver_1)\n", cname);
580           return;
581         }
582     }
583   else if (ver_2)
584     {
585       z0 = tolower(dot_4[1]);
586       z1 = tolower(dot_4[2]);
587       if (z0 != 'z' && z1 != 'z')
588         {
589           printf("Unexpected CNAME %s (ver_2)\n", cname);
590           return;
591         }
592     }
593   else
594     {
595       printf("Unexpected CNAME %s (ver?)\n", cname);
596       return;
597     }
598
599   if (ver_1)
600     {
601       ccode_A2[0] = (char)tolower(cname[2]);
602       ccode_A2[1] = (char)tolower(cname[3]);
603       ccode_A2[2] = '\0';
604     }
605   else
606     ccode_A2[0] = '\0';
607
608   cnumber = ip & 0xFFFF;
609
610   TRACE(("Found country-code `%s', number %d\n",
611          ver_1 ? ccode_A2 : "<n/a>", cnumber));
612
613   country = list_lookup(cnumber, country_list,
614                         sizeof(country_list) / sizeof(country_list[0]));
615   if (!country)
616     printf("Name for country-number %d not found.\n", cnumber);
617   else
618     {
619       if (ver_1)
620         {
621           if ((country->short_name[0] != ccode_A2[0]) ||
622               (country->short_name[1] != ccode_A2[1]) ||
623               (country->short_name[2] != ccode_A2[2]))
624             printf("short-name mismatch; %s vs %s\n",
625                    country->short_name, ccode_A2);
626         }
627       printf("%s (%s), number %d.\n",
628              country->long_name, country->short_name, cnumber);
629     }
630   free(ccopy);
631 }
632