Risistemazione di system.tex per aggiungere i riferimenti corretti al NSS,
[gapil.git] / sources / myhost.c
diff --git a/sources/myhost.c b/sources/myhost.c
new file mode 100644 (file)
index 0000000..eeb2e69
--- /dev/null
@@ -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 <netdb.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>        /* C standard library */
+#include <stdio.h>         /* I/O standard library */
+#include <unistd.h>
+#include <arpa/inet.h>
+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);
+}