1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * University Copyright- Copyright (c) 1982, 1986, 1988 24 * The Regents of the University of California 25 * All Rights Reserved 26 * 27 * University Acknowledgment- Portions of this document are derived from 28 * software developed by the University of California, Berkeley, and its 29 * contributors. 30 */ 31 32 /* 33 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 34 * Use is subject to license terms. 35 * 36 * Copyright 2016 RackTop Systems. 37 */ 38 39 /* Portions Copyright 2007 Jeremy Teo */ 40 /* Portions Copyright 2006 Stephen P. Potter */ 41 42 #include <unistd.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <locale.h> 46 #include <libgen.h> 47 #include <stdlib.h> 48 #include <errno.h> 49 #include <netdb.h> 50 51 extern int getdomainname(char *name, int namelen); 52 extern int setdomainname(char *domain, int len); 53 54 #ifndef TEXT_DOMAIN /* should be defined by cc -D */ 55 #define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */ 56 #endif 57 58 static char *progname; 59 60 static void 61 usage(void) 62 { 63 (void) fprintf(stderr, gettext("usage: %s [-d|-f] [hostname|domain]\n"), 64 basename(progname)); 65 exit(1); 66 } 67 68 int 69 main(int argc, char *argv[]) 70 { 71 int optlet, dflag = 0, fflag = 0; 72 char *optstring = "df?"; 73 74 (void) setlocale(LC_ALL, ""); 75 (void) textdomain(TEXT_DOMAIN); 76 77 progname = argv[0]; 78 79 opterr = 0; 80 while ((optlet = getopt(argc, argv, optstring)) != -1) { 81 switch (optlet) { 82 case 'd': 83 dflag++; 84 break; 85 case 'f': 86 fflag++; 87 break; 88 case '?': 89 usage(); 90 } 91 } 92 93 if (dflag && fflag) 94 usage(); 95 96 /* 97 * if called with no options, just print out the hostname/domain 98 */ 99 if (argc <= optind) { 100 char c_hostname[MAXHOSTNAMELEN]; 101 if (gethostname(c_hostname, sizeof (c_hostname)) < 0) { 102 (void) fprintf(stderr, 103 gettext("%s: unable to obtain hostname\n"), 104 basename(progname)); 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); 122 } else { 123 (void) fprintf(stdout, "%s\n", c_hostname); 124 } 125 } else { 126 /* 127 * if called with an argument, 128 * we have to try to set the new hostname/domain 129 */ 130 if (argc > optind + 1) 131 usage(); /* too many arguments */ 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 } 152 } 153 } 154 return (0); 155 }