Print this page
3758 RFE: Would like "hostname -s"
3430 Support "hostname --fqdn"

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/hostname/hostname.c
          +++ new/usr/src/cmd/hostname/hostname.c
↓ open down ↓ 24 lines elided ↑ open up ↑
  25   25   * All Rights Reserved
  26   26   *
  27   27   * University Acknowledgment- Portions of this document are derived from
  28   28   * software developed by the University of California, Berkeley, and its
  29   29   * contributors.
  30   30   */
  31   31  
  32   32  /*
  33   33   * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  34   34   * Use is subject to license terms.
       35 + *
       36 + * Copyright 2016 RackTop Systems.
  35   37   */
  36   38  
  37   39  /* Portions Copyright 2007 Jeremy Teo */
  38   40  /* Portions Copyright 2006 Stephen P. Potter */
  39      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  40   41  
  41   42  #include <unistd.h>
  42   43  #include <stdio.h>
  43   44  #include <string.h>
  44   45  #include <locale.h>
  45   46  #include <libgen.h>
  46   47  #include <stdlib.h>
  47   48  #include <errno.h>
  48   49  #include <netdb.h>
       50 +#include <getopt.h>
  49   51  
  50   52  #ifndef TEXT_DOMAIN             /* should be defined by cc -D */
  51   53  #define TEXT_DOMAIN "SYS_TEST"  /* use this only if it wasn't */
  52   54  #endif
  53   55  
  54   56  static char *progname;
  55   57  
  56   58  static void
  57   59  usage(void)
  58   60  {
  59      -        (void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
       61 +        (void) fprintf(stderr, gettext("usage: %s [-fls|system_name]\n"),
  60   62                  basename(progname));
  61   63          exit(1);
  62   64  }
  63   65  
  64   66  int
  65   67  main(int argc, char *argv[])
  66   68  {
  67   69          char    *nodename = NULL;
  68   70          char    c_hostname[MAXHOSTNAMELEN];
  69      -        int     optlet;
  70      -        char    *optstring = "?";
       71 +        int     optlet, optindex = 0;
       72 +        char    *optstring = "fls";
       73 +        int     fflag = 0, lflag = 0, sflag = 0;
       74 +        struct  option long_options[] = {
       75 +                {"fqdn",        no_argument,    0,      'f'     },
       76 +                {"long",        no_argument,    0,      'l'     },
       77 +                {"short",       no_argument,    0,      's'     },
       78 +                {NULL,          0,              0,      0       }
       79 +        };
  71   80  
  72   81          (void) setlocale(LC_ALL, "");
  73   82          (void) textdomain(TEXT_DOMAIN);
  74   83  
  75   84          progname = argv[0];
  76   85  
  77   86          opterr = 0;
  78      -        while ((optlet = getopt(argc, argv, optstring)) != -1) {
       87 +        while ((optlet = getopt_long(argc, argv, optstring, long_options,
       88 +            &optindex)) != -1) {
  79   89                  switch (optlet) {
       90 +                case 'f':
       91 +                        fflag++;
       92 +                        break;
       93 +                case 'l':
       94 +                        lflag++;
       95 +                        break;
       96 +                case 's':
       97 +                        sflag++;
       98 +                        break;
  80   99                  case '?':
  81  100                          usage();
  82  101                  }
  83  102          }
  84  103  
      104 +        /* No flag combinations allowed */
      105 +        if (fflag && (lflag || sflag))
      106 +                usage();
      107 +        if (lflag && (fflag || sflag))
      108 +                usage();
      109 +        if (sflag && (lflag || fflag))
      110 +                usage();
      111 +
  85  112          /*
  86  113           * if called with no options, just print out the hostname/nodename
  87  114           */
  88  115          if (argc <= optind) {
  89  116                  if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
  90  117                          (void) fprintf(stderr,
  91  118                              gettext("%s: unable to obtain hostname\n"),
  92  119                              basename(progname));
  93  120                          exit(1);
      121 +                }
      122 +                if (fflag || lflag) {
      123 +                        struct addrinfo hints, *info;
      124 +                        memset(&hints, 0, sizeof (struct addrinfo));
      125 +                        hints.ai_socktype = SOCK_DGRAM;
      126 +                        hints.ai_flags = AI_CANONNAME;
      127 +                        if (getaddrinfo(c_hostname, NULL, &hints, &info) != 0) {
      128 +                                (void) fprintf(stderr,
      129 +                                    gettext("%s: unable to obtain fqdn\n"),
      130 +                                    basename(progname));
      131 +                                exit(1);
      132 +                        }
      133 +                        (void) fprintf(stdout, "%s\n", info->ai_canonname);
  94  134                  } else {
      135 +                        if (sflag) {
      136 +                                char *p = strchr(c_hostname, '.');
      137 +                                if (p != NULL)
      138 +                                        *p = '\0';
      139 +                        }
  95  140                          (void) fprintf(stdout, "%s\n", c_hostname);
  96  141                  }
  97  142          } else {
  98  143                  /*
  99  144                   * if called with an argument,
 100  145                   * we have to try to set the new hostname/nodename
 101  146                   */
 102  147                  if (argc > optind + 1)
 103  148                          usage();        /* too many arguments */
 104      -
      149 +                if (fflag || lflag)
      150 +                        usage();        /* can't set fqdn this way */
      151 +                if (sflag)
      152 +                        usage();        /* can't set short name */
 105  153                  nodename = argv[optind];
 106  154                  if (sethostname(nodename, strlen(nodename)) < 0) {
 107  155                          int err = errno;
 108  156                          (void) fprintf(stderr,
 109  157                              gettext("%s: error in setting name: %s\n"),
 110  158                              basename(progname), strerror(err));
 111  159                          exit(1);
 112  160                  }
 113  161          }
 114  162          return (0);
 115  163  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX