1 /*
2    ISO 3166 Country Codes program
3    --------------------------------------------------------------------
4    Country Codes
5 
6    Copyright (C) 1999, 2000 Diego Javier Grigna <diego@grigna.com>
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22 
23 #include "common.h"
24 #include "protos.h"
25 #include "tables.h"
26 
main(int argc,char * argv[])27 int main( int argc, char *argv[])
28 {
29  int domain_flag = 0;
30  int ch;
31 
32  progname = cc_basename( strdup( argv[ 0]));
33 
34  if( argc < 2)
35      usage();
36 
37  while(( ch = getopt( argc, argv, "ad:")) != EOF){
38          switch( ch) {
39                 case 'a': iso3166_show_all();
40                           break;
41                 case 'd': if( optarg == NULL)
42                               usage();
43                           domain_name_print( optarg);
44                           domain_flag = 1;
45                           break;
46                 default : usage();
47          }
48  }
49 
50  if( !domain_flag && optind >= argc)
51      usage();
52 
53  if( !domain_flag)
54      iso3166_print_heading();
55 
56  while( optind < argc)
57         if( domain_flag)
58             domain_name_print( argv[ optind++]);
59         else
60             iso3166_show_one( argv[ optind++]);
61 
62  putchar( '\n');
63 
64  return 0;
65 }
66 
usage(void)67 static void usage( void)
68 {
69  fprintf( stderr, "\nCountry Codes Release %s - ", COCO_VERSION);
70  fprintf( stderr, "ISO 3166 Country Codes\n\n");
71  fprintf( stderr, "Copyright © 1999, 2000 Diego J. Grigna "
72                   "(diego@grigna.com)\n");
73  fprintf( stderr, "          © 2017 Johnny A. Solbu "
74                   "(johnny@solbu.net\n");
75  fprintf( stderr, "This program is free software, covered by the "
76                   "GNU General Public License.\n\n");
77  fprintf( stderr, "Usage:\n%s [-a] [[code]|[country]|[number]...] "
78                   "[-d <domain name>]\n\n", progname);
79  fprintf( stderr, "   -a   Show all codes.\n");
80  fprintf( stderr, " [code]|[country]|[number]\n");
81  fprintf( stderr, "        You could enter a 2 letter (or 3 letter) "
82                   "ISO 3166 code,\n");
83  fprintf( stderr, "        a country name, or a country number.\n");
84  fprintf( stderr, "   -d   <domain name>\n");
85  fprintf( stderr, "        Show the domain name country (if possible) and "
86                   "try to explain it.\n\n");
87  exit( -1);
88 }
89 
iso3166_print_heading(void)90 static void iso3166_print_heading( void)
91 {
92  printf( "\nCountry                                          2 "
93          "letter  3 letter  Number\n");
94  printf( "%s\n", cc_hyp());
95 }
96 
iso3166_print_cc(int index)97 static void iso3166_print_cc( int index)
98 {
99  printf( "%-52.52s %-2.2s       %-3.3s     %3d\n"
100                    , isoccs[ index].country
101                    , isoccs[ index].codes2l
102                    , isoccs[ index].codes3l
103                    , isoccs[ index].number
104         );
105 }
106 
iso3166_show_all(void)107 static void iso3166_show_all( void)
108 {
109  int i;
110 
111  iso3166_print_heading();
112 
113  i = 0;
114  while( isoccs[ i].country[ 0] != ' ') {
115         iso3166_print_cc( i);
116         i++;
117  }
118 
119  printf( "\nDomain      Explanation\n");
120  printf( "%s\n", cc_hyp());
121 
122  i = 0;
123  while( topdom[ i].str[ 0] != ' ') {
124         printf( "%-12.12s%-60.60s\n", topdom[ i].str, topdom[ i].expl);
125         i++;
126  }
127 
128  printf( "\nSubdomain      Explanation\n");
129  printf( "%s\n", cc_hyp());
130 
131  i = 0;
132  while( subdom[ i].str[ 0] != ' ') {
133         printf( "%-12.12s%-60.60s\n", subdom[ i].str, subdom[ i].expl);
134         i++;
135  }
136 
137  putchar( '\n');
138  exit( 0);
139 }
140 
iso3166_show_one(char * code)141 static void iso3166_show_one( char *code)
142 {
143  char buffer[ 256];
144  unsigned short int number;
145  int search_type = 0;
146  int len         = 0;
147  int i           = 0;
148  int found       = 0;
149 
150  if( code == NULL)
151      return;
152 
153  len = strlen( code);
154 
155  if( len > (sizeof( buffer) - 1)) {
156      fprintf( stderr, "\n%s: %s Argument too long\n\n", progname, code);
157      return;
158  }
159 
160  strcpy( buffer, code);
161 
162  number = atoi( buffer);
163 
164  if( number > 0)
165      search_type = ISO_SEARCH_NUMBER;
166  else if( len == 2)
167           search_type = ISO_SEARCH_2L;
168       else if( len == 3)
169                search_type = ISO_SEARCH_3L;
170            else {
171                cc_lowercase( buffer);
172                search_type = ISO_SEARCH_COUNTRY;
173            }
174 
175  while( isoccs[ i].country[ 0] != ' ') {
176         switch( search_type) {
177                 case ISO_SEARCH_COUNTRY: buffer[ 0] = toupper( buffer[ 0]);
178                                          if( strstr( isoccs[ i].country, buffer) != NULL) {
179                                              iso3166_print_cc( i);
180                                              found = 1;
181                                          }
182                                          break;
183                 case ISO_SEARCH_2L     : if( !strncasecmp( isoccs[ i].codes2l, buffer, 2)) {
184                                              iso3166_print_cc( i);
185                                              found = 1;
186                                          }
187                                          break;
188                 case ISO_SEARCH_3L     : if( !strncasecmp( isoccs[ i].codes3l, buffer, 3)) {
189                                              iso3166_print_cc( i);
190                                              found = 1;
191                                          }
192                                          break;
193                 case ISO_SEARCH_NUMBER : if( number == isoccs[ i].number) {
194                                              iso3166_print_cc( i);
195                                              found = 1;
196                                          }
197                                          break;
198         }
199         i++;
200  }
201 
202  if( !found) {
203      printf( "[%s] Not found ", code);
204      if( index( code, '.') != NULL)
205          printf( "(try -d %s)\n", code);
206      else
207          printf( "\n");
208  }
209 
210 }
211 
domain_name_print(char * str)212 static void domain_name_print( char *str)
213 {
214  char *dptr;
215  char *wptr;
216  char *buffer;
217  int size;
218  int i     = 0;
219  int found = 0;
220  int level = 0;
221  int len   = 0;
222 
223  printf( "\nDomain name  : %s\n", str);
224 
225  size = strlen( str);
226 
227  buffer = ( char *) calloc( size + 2, sizeof( char));
228 
229  if( buffer == NULL) {
230      fprintf( stderr, "\n%s: Not enough memory\n\n", progname);
231      exit( 1);
232  }
233 
234  buffer[ 0] = '.';
235  strcat( buffer, str);
236 
237  dptr = rindex( buffer, '.');
238 
239  while( dptr != NULL) {
240         found = 0;
241 
242         wptr = dptr + 1;
243 
244         len = strlen( wptr);
245 
246         if( level == 0)
247             printf( "Top domain   : %s \t" , wptr);
248         else
249             printf( "Sub domain #%d: %s \t", level, wptr);
250 
251         i = 0;
252         while( !found && topdom[ i].str[ 0] != ' ') {
253                if( len != strlen( topdom[ i].str)) {
254                    i++;
255                    continue;
256                }
257                if( !strncasecmp( topdom[ i].str, wptr, len)) {
258                    printf( "(%s)", topdom[ i].expl);
259                    found = 1;
260                }
261                i++;
262         }
263 
264         i = 0;
265         while( !found && subdom[ i].str[ 0] != ' ') {
266                if( len != strlen( subdom[ i].str)) {
267                    i++;
268                    continue;
269                }
270                if( !strncasecmp( subdom[ i].str, wptr, len)) {
271                    printf( "(%s)", subdom[ i].expl);
272                    found = 1;
273                }
274                i++;
275         }
276 
277         i = 0;
278 
279         if( level == 0)
280             while( !found && isoccs[ i].country[ 0] != ' ') {
281                    if( len != 2) {
282                        i++;
283                        continue;
284                    }
285                    if( !strncasecmp( isoccs[ i].codes2l, wptr, len)) {
286                        printf( "(%s)", isoccs[ i].country);
287                        found = 1;
288                    }
289                    i++;
290             }
291 
292         if( !found)
293             printf( "(Unknown)");
294 
295         putchar( '\n');
296 
297         level++;
298 
299         *dptr = 0;
300         dptr = rindex( buffer, '.');
301  } /* end while */
302 
303  putchar( '\n');
304 
305  free( buffer);
306 }
307 
308