xref: /titanic_44/usr/src/cmd/hostname/hostname.c (revision a62f338b060e0aff31e43a1fecf8cbb6d43ad3ba)
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 /* Portions Copyright 2006 Jeremy Teo */
33 
34 /*
35  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
36  * Use is subject to license terms.
37  */
38 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <locale.h>
44 #include <libgen.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <sys/utsname.h>
48 #include <sys/systeminfo.h>
49 
50 #ifndef	TEXT_DOMAIN		/* should be defined by cc -D */
51 #define	TEXT_DOMAIN "SYS_TEST"	/* use this only if it wasn't */
52 #endif
53 
54 static char *progname;
55 
56 static void
57 usage(void)
58 {
59 	(void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
60 		basename(progname));
61 	exit(1);
62 }
63 
64 int
65 main(int argc, char *argv[])
66 {
67 	char	*nodename = NULL;
68 	char	c_hostname[SYS_NMLN];
69 	int	optlet;
70 	char	*optstring = "?";
71 
72 	(void) setlocale(LC_ALL, "");
73 	(void) textdomain(TEXT_DOMAIN);
74 
75 	progname = argv[0];
76 
77 	opterr = 0;
78 	while ((optlet = getopt(argc, argv, optstring)) != -1) {
79 		switch (optlet) {
80 		case '?':
81 			usage();
82 		}
83 	}
84 
85 	/*
86 	 * if called with no options, just print out the hostname/nodename
87 	 */
88 	if (argc <= optind) {
89 		if (sysinfo(SI_HOSTNAME, c_hostname, SYS_NMLN)) {
90 			(void) fprintf(stdout, "%s\n", c_hostname);
91 		} else {
92 			(void) fprintf(stderr,
93 			    gettext("%s: unable to obtain hostname\n"),
94 			    basename(progname));
95 			exit(1);
96 		}
97 	} else {
98 		/*
99 		 * if called with an argument,
100 		 * we have to try to set the new hostname/nodename
101 		 */
102 		if (argc > optind + 1)
103 			usage();	/* too many arguments */
104 
105 		nodename = argv[optind];
106 		if (sysinfo(SI_SET_HOSTNAME, 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);
112 		}
113 	}
114 	return (0);
115 }
116