Print this page
Placeholder

@@ -30,86 +30,126 @@
  */
 
 /*
  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright 2016 RackTop Systems.
  */
 
 /* Portions Copyright 2007 Jeremy Teo */
 /* Portions Copyright 2006 Stephen P. Potter */
-#pragma ident   "%Z%%M% %I%     %E% SMI"
 
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include <locale.h>
 #include <libgen.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <netdb.h>
 
+extern int getdomainname(char *name, int namelen);
+extern int setdomainname(char *domain, int len);
+
 #ifndef TEXT_DOMAIN             /* should be defined by cc -D */
 #define TEXT_DOMAIN "SYS_TEST"  /* use this only if it wasn't */
 #endif
 
 static char *progname;
 
 static void
 usage(void)
 {
-        (void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
+        (void) fprintf(stderr, gettext("usage: %s [-d|-f] [hostname|domain]\n"),
                 basename(progname));
         exit(1);
 }
 
 int
 main(int argc, char *argv[])
 {
-        char    *nodename = NULL;
-        char    c_hostname[MAXHOSTNAMELEN];
-        int     optlet;
-        char    *optstring = "?";
+        int     optlet, dflag = 0, fflag = 0;
+        char    *optstring = "df?";
 
         (void) setlocale(LC_ALL, "");
         (void) textdomain(TEXT_DOMAIN);
 
         progname = argv[0];
 
         opterr = 0;
         while ((optlet = getopt(argc, argv, optstring)) != -1) {
                 switch (optlet) {
+                case 'd':
+                        dflag++;
+                        break;
+                case 'f':
+                        fflag++;
+                        break;
                 case '?':
                         usage();
                 }
         }
 
+        if (dflag && fflag)
+                usage();
+
         /*
-         * if called with no options, just print out the hostname/nodename
+         * if called with no options, just print out the hostname/domain
          */
         if (argc <= optind) {
+                char c_hostname[MAXHOSTNAMELEN];
                 if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
                         (void) fprintf(stderr,
                             gettext("%s: unable to obtain hostname\n"),
                             basename(progname));
                         exit(1);
+                }
+                if (dflag || fflag) {
+                        char c_domain[MAXHOSTNAMELEN];
+                        if (getdomainname(c_domain, sizeof (c_domain)) < 0) {
+                                (void) fprintf(stderr,
+                                    gettext("%s: unable to obtain domain\n"),
+                                    basename(progname));
+                                exit(1);
+                        }
+                        /* Might not actually get a domain back */
+                        if (fflag && *c_domain)
+                                (void) fprintf(stdout, "%s.%s\n",
+                                    c_hostname, c_domain);
+                        else
+                                (void) fprintf(stdout, "%s\n",
+                                    dflag ? c_domain : c_hostname);
                 } else {
                         (void) fprintf(stdout, "%s\n", c_hostname);
                 }
         } else {
                 /*
                  * if called with an argument,
-                 * we have to try to set the new hostname/nodename
+                 * we have to try to set the new hostname/domain
                  */
                 if (argc > optind + 1)
                         usage();        /* too many arguments */
-
-                nodename = argv[optind];
-                if (sethostname(nodename, strlen(nodename)) < 0) {
+                if (fflag)
+                        usage();        /* can't set fqdn this way */
+                if (dflag) {
+                        char *domain = argv[optind];
+                        if (setdomainname(domain, strlen(domain)) < 0) {
                         int err = errno;
                         (void) fprintf(stderr,
-                            gettext("%s: error in setting name: %s\n"),
+                                    gettext("%s: error setting domain: %s\n"),
                             basename(progname), strerror(err));
                         exit(1);
                 }
+                } else {
+                        char *hostname = argv[optind];
+                        if (sethostname(hostname, strlen(hostname)) < 0) {
+                                int err = errno;
+                                (void) fprintf(stderr,
+                                    gettext("%s: error setting hostname: %s\n"),
+                                    basename(progname), strerror(err));
+                                exit(1);
+                        }
+                }
         }
         return (0);
 }