X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Fmyhost.c;fp=sources%2Fmyhost.c;h=eeb2e6925ca6d1f50e1e98011494c6c70cd9e1ce;hp=0000000000000000000000000000000000000000;hb=062e1036f07efd623e11b5bb194bcf2684dd7866;hpb=8bfe80f7fe2470bf0a26d656b281ea44c69d8fe5 diff --git a/sources/myhost.c b/sources/myhost.c new file mode 100644 index 0000000..eeb2e69 --- /dev/null +++ b/sources/myhost.c @@ -0,0 +1,125 @@ +/* myhost.c + * + * Copyright (C) 2004 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/***************************************************************************** + * + * File myhost.c: An example host command + * + * Author: S. Piccardi Jul. 2004 + * + * $Id: myhost.c,v 1.1 2004/07/03 22:53:13 piccardi Exp $ + * + *****************************************************************************/ +#include +#include +#include +#include /* C standard library */ +#include /* I/O standard library */ +#include +#include +extern int h_errno; + +#include "Gapil.h" +/* + * Program myhost + * + * Use gethostbyname and print results + */ +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + struct hostent *data; + char **alias; + char *addr; + char buffer[INET6_ADDRSTRLEN]; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + if ((argc - optind) != 1) { + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + data = gethostbyname(argv[1]); + if (data == NULL) { + herror("Errore di risoluzione"); + exit(1); + } + printf("Canonical name %s\n", data->h_name); + alias = data->h_aliases; + while (*alias != NULL) { + printf("Alias %s\n", *alias); + alias++; + } + if (data->h_addrtype == AF_INET) { + printf("Address are IPv4\n"); + } else if (data->h_addrtype == AF_INET6) { + printf("Address are IPv6\n"); + } else { + printf("Tipo di indirizzo non valido\n"); + exit(1); + } + alias = data->h_addr_list; + while (*alias != NULL) { + addr = inet_ntop(data->h_addrtype, *alias, buffer, sizeof(buffer)); + printf("Indirizzo %s\n", addr); + alias++; + } + exit(0); +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program myhost: do an hostname resolution \n"); + printf("Usage:\n"); + printf(" myhost [-h] hostname \n"); + printf(" -h print this help\n"); + exit(1); +}