1*2204Sesaxe /*
2*2204Sesaxe  * CDDL HEADER START
3*2204Sesaxe  *
4*2204Sesaxe  * The contents of this file are subject to the terms of the
5*2204Sesaxe  * Common Development and Distribution License (the "License").
6*2204Sesaxe  * You may not use this file except in compliance with the License.
7*2204Sesaxe  *
8*2204Sesaxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2204Sesaxe  * or http://www.opensolaris.org/os/licensing.
10*2204Sesaxe  * See the License for the specific language governing permissions
11*2204Sesaxe  * and limitations under the License.
12*2204Sesaxe  *
13*2204Sesaxe  * When distributing Covered Code, include this CDDL HEADER in each
14*2204Sesaxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2204Sesaxe  * If applicable, add the following below this CDDL HEADER, with the
16*2204Sesaxe  * fields enclosed by brackets "[]" replaced with your own identifying
17*2204Sesaxe  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2204Sesaxe  *
19*2204Sesaxe  * CDDL HEADER END
20*2204Sesaxe  */
21*2204Sesaxe 
22*2204Sesaxe /*
23*2204Sesaxe  * University Copyright- Copyright (c) 1982, 1986, 1988
24*2204Sesaxe  * The Regents of the University of California
25*2204Sesaxe  * All Rights Reserved
26*2204Sesaxe  *
27*2204Sesaxe  * University Acknowledgment- Portions of this document are derived from
28*2204Sesaxe  * software developed by the University of California, Berkeley, and its
29*2204Sesaxe  * contributors.
30*2204Sesaxe  */
31*2204Sesaxe 
32*2204Sesaxe /* Portions Copyright 2006 Jeremy Teo */
33*2204Sesaxe 
34*2204Sesaxe /*
35*2204Sesaxe  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
36*2204Sesaxe  * Use is subject to license terms.
37*2204Sesaxe  */
38*2204Sesaxe #pragma ident	"%Z%%M%	%I%	%E% SMI"
39*2204Sesaxe 
40*2204Sesaxe #include <unistd.h>
41*2204Sesaxe #include <stdio.h>
42*2204Sesaxe #include <string.h>
43*2204Sesaxe #include <locale.h>
44*2204Sesaxe #include <libgen.h>
45*2204Sesaxe #include <stdlib.h>
46*2204Sesaxe #include <errno.h>
47*2204Sesaxe #include <sys/utsname.h>
48*2204Sesaxe #include <sys/systeminfo.h>
49*2204Sesaxe 
50*2204Sesaxe #ifndef	TEXT_DOMAIN		/* should be defined by cc -D */
51*2204Sesaxe #define	TEXT_DOMAIN "SYS_TEST"	/* use this only if it wasn't */
52*2204Sesaxe #endif
53*2204Sesaxe 
54*2204Sesaxe static char *progname;
55*2204Sesaxe 
56*2204Sesaxe static void
57*2204Sesaxe usage(void)
58*2204Sesaxe {
59*2204Sesaxe 	(void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
60*2204Sesaxe 		basename(progname));
61*2204Sesaxe 	exit(1);
62*2204Sesaxe }
63*2204Sesaxe 
64*2204Sesaxe int
65*2204Sesaxe main(int argc, char *argv[])
66*2204Sesaxe {
67*2204Sesaxe 	char	*nodename = NULL;
68*2204Sesaxe 	char	c_hostname[SYS_NMLN];
69*2204Sesaxe 	int	optlet;
70*2204Sesaxe 	char	*optstring = "?";
71*2204Sesaxe 
72*2204Sesaxe 	(void) setlocale(LC_ALL, "");
73*2204Sesaxe 	(void) textdomain(TEXT_DOMAIN);
74*2204Sesaxe 
75*2204Sesaxe 	progname = argv[0];
76*2204Sesaxe 
77*2204Sesaxe 	opterr = 0;
78*2204Sesaxe 	while ((optlet = getopt(argc, argv, optstring)) != -1) {
79*2204Sesaxe 		switch (optlet) {
80*2204Sesaxe 		case '?':
81*2204Sesaxe 			usage();
82*2204Sesaxe 		}
83*2204Sesaxe 	}
84*2204Sesaxe 
85*2204Sesaxe 	/*
86*2204Sesaxe 	 * if called with no options, just print out the hostname/nodename
87*2204Sesaxe 	 */
88*2204Sesaxe 	if (argc <= optind) {
89*2204Sesaxe 		if (sysinfo(SI_HOSTNAME, c_hostname, SYS_NMLN)) {
90*2204Sesaxe 			(void) fprintf(stdout, "%s\n", c_hostname);
91*2204Sesaxe 		} else {
92*2204Sesaxe 			(void) fprintf(stderr,
93*2204Sesaxe 			    gettext("%s: unable to obtain hostname\n"),
94*2204Sesaxe 			    basename(progname));
95*2204Sesaxe 			exit(1);
96*2204Sesaxe 		}
97*2204Sesaxe 	} else {
98*2204Sesaxe 		/*
99*2204Sesaxe 		 * if called with an argument,
100*2204Sesaxe 		 * we have to try to set the new hostname/nodename
101*2204Sesaxe 		 */
102*2204Sesaxe 		if (argc > optind + 1)
103*2204Sesaxe 			usage();	/* too many arguments */
104*2204Sesaxe 
105*2204Sesaxe 		nodename = argv[optind];
106*2204Sesaxe 		if (sysinfo(SI_SET_HOSTNAME, nodename, strlen(nodename)) < 0) {
107*2204Sesaxe 			int err = errno;
108*2204Sesaxe 			(void) fprintf(stderr,
109*2204Sesaxe 			    gettext("%s: error in setting name: %s\n"),
110*2204Sesaxe 			    basename(progname), strerror(err));
111*2204Sesaxe 			exit(1);
112*2204Sesaxe 		}
113*2204Sesaxe 	}
114*2204Sesaxe 	return (0);
115*2204Sesaxe }
116