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