home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-24  |  4.6 KB  |  188 lines

  1. /* hostname -- set the host name or show the host/domain name
  2.  
  3.    Copyright (C) 1994 Peter Tobias
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <getopt.h>
  25. #include <string.h>
  26. #include <netdb.h>
  27. #include <errno.h>
  28. #include <sys/param.h>
  29.  
  30. #define NO_OPT -1
  31.  
  32. static char *program_name;
  33. static const char *version_string = "hostname 1.5";
  34.  
  35. static void sethname(char *);
  36. static void showhname(char *, int);
  37. static void usage(void);
  38.  
  39. static void sethname(char *hname)
  40. {
  41.     if(sethostname(hname, strlen(hname))) {
  42.         switch(errno) {
  43.             case EPERM:
  44.                 fprintf(stderr,"%s: you must be root to change the host name\n", program_name);
  45.                 break;
  46.             case EINVAL:
  47.                 fprintf(stderr,"%s: name too long\n", program_name);
  48.                 break;
  49.             default:
  50.         }
  51.         exit(1);
  52.     };
  53. }
  54.  
  55. static void showhname(char *hname, int c)
  56. {
  57.     struct hostent *hp;
  58.     register char *p;
  59.  
  60.     if (!(hp = gethostbyname(hname))) {
  61.         herror(program_name);
  62.         exit(1);
  63.     }
  64.  
  65.         if (!(p = strchr(hp->h_name, '.'))) {
  66.         fprintf(stderr,"%s: can't find a FQDN for the host name `%s'\n", program_name, hname);
  67.         exit(1);
  68.     }
  69.  
  70.     switch(c) {
  71.         case 'd':
  72.             printf("%s\n", ++p);
  73.             break;
  74.         case 'f':
  75.             printf("%s\n", hp->h_name);
  76.             break;
  77.         case 's':
  78.             *p = '\0';
  79.             printf("%s\n", hp->h_name);
  80.             break;
  81.         default:
  82.     }
  83. }
  84.  
  85. static void usage(void)
  86. {
  87.   printf("Usage: %s [OPTION]... [hostname]\n\n\
  88.   -d, --domain                 display the DNS domain name\n\
  89.   -F, --file filename          read the host name from file\n\
  90.   -f, --fqdn, --long           display the long host name (FQDN)\n\
  91.   -s, --short                  display the short host name\n\
  92.   -h, --help                   display this help and exit\n\
  93.   -v, --version                output version information and exit\n\
  94. \n\
  95.    When the program is called without any arguments, it displays the\n\
  96.    current host name as set by the hostname command. If an argument\n\
  97.    is given, the program will set the value of the host name to the\n\
  98.    value specified.\n\
  99.    Unless you are using bind or NIS for host lookups you can change the\n\
  100.    FQDN (Fully Qualified Domain Name) and the DNS domain name (which is\n\
  101.    part of the FQDN) in the /etc/hosts file.\n", program_name);
  102. }
  103.  
  104. int main(int argc, char **argv)
  105. {
  106.     int c;
  107.     int option_index = 0;
  108.  
  109.     char myname[MAXHOSTNAMELEN+1];
  110.  
  111.     static const struct option long_options[] =
  112.     {
  113.         {"domain", no_argument, 0, 'd'},
  114.         {"file", required_argument, 0, 'F'},
  115.         {"fqdn", no_argument, 0, 'f'},
  116.         {"help", no_argument, 0, 'h'},
  117.         {"long", no_argument, 0, 'f'},
  118.         {"short", no_argument, 0, 's'},
  119.         {"version", no_argument, 0, 'v'},
  120.         {0, 0, 0, 0}
  121.     };
  122.  
  123.     program_name = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
  124.  
  125.     if (strcmp(program_name, "dnsdomainname") == 0) {
  126.         if (argc > 1) {
  127.             fprintf(stderr,"%s: You can't change the DNS domain name with this command\n", program_name);
  128.             fprintf(stderr,"\nUnless you are using bind or NIS for host lookups you can change the DNS\n");
  129.             fprintf(stderr,"domain name (which is part of the FQDN) in the /etc/hosts file.\n");
  130.             exit(1);
  131.         }
  132.         c = 'd';
  133.     } else
  134.         c = getopt_long(argc, argv, "dfF:hsv", long_options, &option_index);
  135.  
  136.     gethostname(myname, sizeof(myname));
  137.  
  138.     switch(c)
  139.     {
  140.         case 'd':
  141.         case 'f':
  142.         case 's':
  143.             showhname(myname, c);
  144.             break;
  145.         case 'F':
  146.             {
  147.             register FILE *fd;
  148.             register char *p;
  149.             char fline[MAXHOSTNAMELEN];
  150.  
  151.             if ((fd = fopen(optarg, "r")) != NULL) {
  152.                 while (fgets(fline, sizeof(fline), fd) != NULL)
  153.                     if ((p = index(fline, '\n')) != NULL) {
  154.                         *p = '\0';
  155.                         if (fline[0] == '#')
  156.                             continue;
  157.                         sethname(fline);
  158.                     }
  159.                 (void) fclose(fd);
  160.             } else {
  161.                 fprintf(stderr,"%s: can't open `%s'\n",
  162.                     program_name, optarg);
  163.                 exit(1);
  164.             }
  165.             }
  166.             break;
  167.         case 'h':
  168.             usage();
  169.             break;
  170.         case 'v':
  171.             printf("%s\n", version_string);
  172.             break;
  173.         case '?':
  174.             fprintf(stderr,"Try `%s --help' for more information.\n", program_name);
  175.             exit(1);
  176.             break;
  177.         case NO_OPT:
  178.             if (optind < argc) {
  179.                 sethname(argv[optind]);
  180.                 exit(0);
  181.             }
  182.         default:
  183.             printf("%s\n", myname);
  184.  
  185.     };
  186.     exit(0);
  187. }
  188.