Print this page
Placeholder

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>
  49   50  
       51 +extern int getdomainname(char *name, int namelen);
       52 +extern int setdomainname(char *domain, int len);
       53 +
  50   54  #ifndef TEXT_DOMAIN             /* should be defined by cc -D */
  51   55  #define TEXT_DOMAIN "SYS_TEST"  /* use this only if it wasn't */
  52   56  #endif
  53   57  
  54   58  static char *progname;
  55   59  
  56   60  static void
  57   61  usage(void)
  58   62  {
  59      -        (void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
  60      -                basename(progname));
       63 +        (void) fprintf(stderr, gettext("usage: %s [-d|-f] [hostname|domain]\n"),
       64 +            basename(progname));
  61   65          exit(1);
  62   66  }
  63   67  
  64   68  int
  65   69  main(int argc, char *argv[])
  66   70  {
  67      -        char    *nodename = NULL;
  68      -        char    c_hostname[MAXHOSTNAMELEN];
  69      -        int     optlet;
  70      -        char    *optstring = "?";
       71 +        int     optlet, dflag = 0, fflag = 0;
       72 +        char    *optstring = "df?";
  71   73  
  72   74          (void) setlocale(LC_ALL, "");
  73   75          (void) textdomain(TEXT_DOMAIN);
  74   76  
  75   77          progname = argv[0];
  76   78  
  77   79          opterr = 0;
  78   80          while ((optlet = getopt(argc, argv, optstring)) != -1) {
  79   81                  switch (optlet) {
       82 +                case 'd':
       83 +                        dflag++;
       84 +                        break;
       85 +                case 'f':
       86 +                        fflag++;
       87 +                        break;
  80   88                  case '?':
  81   89                          usage();
  82   90                  }
  83   91          }
  84   92  
       93 +        if (dflag && fflag)
       94 +                usage();
       95 +
  85   96          /*
  86      -         * if called with no options, just print out the hostname/nodename
       97 +         * if called with no options, just print out the hostname/domain
  87   98           */
  88   99          if (argc <= optind) {
      100 +                char c_hostname[MAXHOSTNAMELEN];
  89  101                  if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
  90  102                          (void) fprintf(stderr,
  91  103                              gettext("%s: unable to obtain hostname\n"),
  92  104                              basename(progname));
  93  105                          exit(1);
      106 +                }
      107 +                if (dflag || fflag) {
      108 +                        char c_domain[MAXHOSTNAMELEN];
      109 +                        if (getdomainname(c_domain, sizeof (c_domain)) < 0) {
      110 +                                (void) fprintf(stderr,
      111 +                                    gettext("%s: unable to obtain domain\n"),
      112 +                                    basename(progname));
      113 +                                exit(1);
      114 +                        }
      115 +                        /* Might not actually get a domain back */
      116 +                        if (fflag && *c_domain)
      117 +                                (void) fprintf(stdout, "%s.%s\n",
      118 +                                    c_hostname, c_domain);
      119 +                        else
      120 +                                (void) fprintf(stdout, "%s\n",
      121 +                                    dflag ? c_domain : c_hostname);
  94  122                  } else {
  95  123                          (void) fprintf(stdout, "%s\n", c_hostname);
  96  124                  }
  97  125          } else {
  98  126                  /*
  99  127                   * if called with an argument,
 100      -                 * we have to try to set the new hostname/nodename
      128 +                 * we have to try to set the new hostname/domain
 101  129                   */
 102  130                  if (argc > optind + 1)
 103  131                          usage();        /* too many arguments */
 104      -
 105      -                nodename = argv[optind];
 106      -                if (sethostname(nodename, strlen(nodename)) < 0) {
 107      -                        int err = errno;
 108      -                        (void) fprintf(stderr,
 109      -                            gettext("%s: error in setting name: %s\n"),
 110      -                            basename(progname), strerror(err));
 111      -                        exit(1);
      132 +                if (fflag)
      133 +                        usage();        /* can't set fqdn this way */
      134 +                if (dflag) {
      135 +                        char *domain = argv[optind];
      136 +                        if (setdomainname(domain, strlen(domain)) < 0) {
      137 +                                int err = errno;
      138 +                                (void) fprintf(stderr,
      139 +                                    gettext("%s: error setting domain: %s\n"),
      140 +                                    basename(progname), strerror(err));
      141 +                                exit(1);
      142 +                        }
      143 +                } else {
      144 +                        char *hostname = argv[optind];
      145 +                        if (sethostname(hostname, strlen(hostname)) < 0) {
      146 +                                int err = errno;
      147 +                                (void) fprintf(stderr,
      148 +                                    gettext("%s: error setting hostname: %s\n"),
      149 +                                    basename(progname), strerror(err));
      150 +                                exit(1);
      151 +                        }
 112  152                  }
 113  153          }
 114  154          return (0);
 115  155  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX