xref: /titanic_51/usr/src/cmd/hostname/hostname.c (revision 20d159bb22d487d2a4feb5d283a2b7d832bff8d5)
17bdc4eabSesaxe /*
27bdc4eabSesaxe  * CDDL HEADER START
37bdc4eabSesaxe  *
47bdc4eabSesaxe  * The contents of this file are subject to the terms of the
57bdc4eabSesaxe  * Common Development and Distribution License (the "License").
67bdc4eabSesaxe  * You may not use this file except in compliance with the License.
77bdc4eabSesaxe  *
87bdc4eabSesaxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97bdc4eabSesaxe  * or http://www.opensolaris.org/os/licensing.
107bdc4eabSesaxe  * See the License for the specific language governing permissions
117bdc4eabSesaxe  * and limitations under the License.
127bdc4eabSesaxe  *
137bdc4eabSesaxe  * When distributing Covered Code, include this CDDL HEADER in each
147bdc4eabSesaxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157bdc4eabSesaxe  * If applicable, add the following below this CDDL HEADER, with the
167bdc4eabSesaxe  * fields enclosed by brackets "[]" replaced with your own identifying
177bdc4eabSesaxe  * information: Portions Copyright [yyyy] [name of copyright owner]
187bdc4eabSesaxe  *
197bdc4eabSesaxe  * CDDL HEADER END
207bdc4eabSesaxe  */
217bdc4eabSesaxe 
227bdc4eabSesaxe /*
237bdc4eabSesaxe  * University Copyright- Copyright (c) 1982, 1986, 1988
247bdc4eabSesaxe  * The Regents of the University of California
257bdc4eabSesaxe  * All Rights Reserved
267bdc4eabSesaxe  *
277bdc4eabSesaxe  * University Acknowledgment- Portions of this document are derived from
287bdc4eabSesaxe  * software developed by the University of California, Berkeley, and its
297bdc4eabSesaxe  * contributors.
307bdc4eabSesaxe  */
317bdc4eabSesaxe 
327bdc4eabSesaxe /*
33*20d159bbSstevel  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
347bdc4eabSesaxe  * Use is subject to license terms.
357bdc4eabSesaxe  */
36*20d159bbSstevel 
37*20d159bbSstevel /* Portions Copyright 2007 Jeremy Teo */
38*20d159bbSstevel /* Portions Copyright 2006 Stephen P. Potter */
397bdc4eabSesaxe #pragma ident	"%Z%%M%	%I%	%E% SMI"
407bdc4eabSesaxe 
417bdc4eabSesaxe #include <unistd.h>
427bdc4eabSesaxe #include <stdio.h>
437bdc4eabSesaxe #include <string.h>
447bdc4eabSesaxe #include <locale.h>
457bdc4eabSesaxe #include <libgen.h>
467bdc4eabSesaxe #include <stdlib.h>
477bdc4eabSesaxe #include <errno.h>
48*20d159bbSstevel #include <netdb.h>
497bdc4eabSesaxe 
507bdc4eabSesaxe #ifndef	TEXT_DOMAIN		/* should be defined by cc -D */
517bdc4eabSesaxe #define	TEXT_DOMAIN "SYS_TEST"	/* use this only if it wasn't */
527bdc4eabSesaxe #endif
537bdc4eabSesaxe 
547bdc4eabSesaxe static char *progname;
557bdc4eabSesaxe 
567bdc4eabSesaxe static void
577bdc4eabSesaxe usage(void)
587bdc4eabSesaxe {
597bdc4eabSesaxe 	(void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
607bdc4eabSesaxe 		basename(progname));
617bdc4eabSesaxe 	exit(1);
627bdc4eabSesaxe }
637bdc4eabSesaxe 
647bdc4eabSesaxe int
657bdc4eabSesaxe main(int argc, char *argv[])
667bdc4eabSesaxe {
677bdc4eabSesaxe 	char	*nodename = NULL;
68*20d159bbSstevel 	char    c_hostname[MAXHOSTNAMELEN];
697bdc4eabSesaxe 	int	optlet;
707bdc4eabSesaxe 	char	*optstring = "?";
717bdc4eabSesaxe 
727bdc4eabSesaxe 	(void) setlocale(LC_ALL, "");
737bdc4eabSesaxe 	(void) textdomain(TEXT_DOMAIN);
747bdc4eabSesaxe 
757bdc4eabSesaxe 	progname = argv[0];
767bdc4eabSesaxe 
777bdc4eabSesaxe 	opterr = 0;
787bdc4eabSesaxe 	while ((optlet = getopt(argc, argv, optstring)) != -1) {
797bdc4eabSesaxe 		switch (optlet) {
807bdc4eabSesaxe 		case '?':
817bdc4eabSesaxe 			usage();
827bdc4eabSesaxe 		}
837bdc4eabSesaxe 	}
847bdc4eabSesaxe 
857bdc4eabSesaxe 	/*
867bdc4eabSesaxe 	 * if called with no options, just print out the hostname/nodename
877bdc4eabSesaxe 	 */
887bdc4eabSesaxe 	if (argc <= optind) {
89*20d159bbSstevel 		if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
907bdc4eabSesaxe 			(void) fprintf(stderr,
917bdc4eabSesaxe 			    gettext("%s: unable to obtain hostname\n"),
927bdc4eabSesaxe 			    basename(progname));
937bdc4eabSesaxe 			exit(1);
94*20d159bbSstevel 		} else {
95*20d159bbSstevel 			(void) fprintf(stdout, "%s\n", c_hostname);
967bdc4eabSesaxe 		}
977bdc4eabSesaxe 	} else {
987bdc4eabSesaxe 		/*
997bdc4eabSesaxe 		 * if called with an argument,
1007bdc4eabSesaxe 		 * we have to try to set the new hostname/nodename
1017bdc4eabSesaxe 		 */
1027bdc4eabSesaxe 		if (argc > optind + 1)
1037bdc4eabSesaxe 			usage();	/* too many arguments */
1047bdc4eabSesaxe 
1057bdc4eabSesaxe 		nodename = argv[optind];
106*20d159bbSstevel 		if (sethostname(nodename, strlen(nodename)) < 0) {
1077bdc4eabSesaxe 			int err = errno;
1087bdc4eabSesaxe 			(void) fprintf(stderr,
1097bdc4eabSesaxe 			    gettext("%s: error in setting name: %s\n"),
1107bdc4eabSesaxe 			    basename(progname), strerror(err));
1117bdc4eabSesaxe 			exit(1);
1127bdc4eabSesaxe 		}
1137bdc4eabSesaxe 	}
1147bdc4eabSesaxe 	return (0);
1157bdc4eabSesaxe }
116