12204Sesaxe /*
22204Sesaxe * CDDL HEADER START
32204Sesaxe *
42204Sesaxe * The contents of this file are subject to the terms of the
52204Sesaxe * Common Development and Distribution License (the "License").
62204Sesaxe * You may not use this file except in compliance with the License.
72204Sesaxe *
82204Sesaxe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92204Sesaxe * or http://www.opensolaris.org/os/licensing.
102204Sesaxe * See the License for the specific language governing permissions
112204Sesaxe * and limitations under the License.
122204Sesaxe *
132204Sesaxe * When distributing Covered Code, include this CDDL HEADER in each
142204Sesaxe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152204Sesaxe * If applicable, add the following below this CDDL HEADER, with the
162204Sesaxe * fields enclosed by brackets "[]" replaced with your own identifying
172204Sesaxe * information: Portions Copyright [yyyy] [name of copyright owner]
182204Sesaxe *
192204Sesaxe * CDDL HEADER END
202204Sesaxe */
212204Sesaxe
222204Sesaxe /*
232204Sesaxe * University Copyright- Copyright (c) 1982, 1986, 1988
242204Sesaxe * The Regents of the University of California
252204Sesaxe * All Rights Reserved
262204Sesaxe *
272204Sesaxe * University Acknowledgment- Portions of this document are derived from
282204Sesaxe * software developed by the University of California, Berkeley, and its
292204Sesaxe * contributors.
302204Sesaxe */
312204Sesaxe
322204Sesaxe /*
33*3371Sstevel * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
342204Sesaxe * Use is subject to license terms.
352204Sesaxe */
36*3371Sstevel
37*3371Sstevel /* Portions Copyright 2007 Jeremy Teo */
38*3371Sstevel /* Portions Copyright 2006 Stephen P. Potter */
392204Sesaxe #pragma ident "%Z%%M% %I% %E% SMI"
402204Sesaxe
412204Sesaxe #include <unistd.h>
422204Sesaxe #include <stdio.h>
432204Sesaxe #include <string.h>
442204Sesaxe #include <locale.h>
452204Sesaxe #include <libgen.h>
462204Sesaxe #include <stdlib.h>
472204Sesaxe #include <errno.h>
48*3371Sstevel #include <netdb.h>
492204Sesaxe
502204Sesaxe #ifndef TEXT_DOMAIN /* should be defined by cc -D */
512204Sesaxe #define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
522204Sesaxe #endif
532204Sesaxe
542204Sesaxe static char *progname;
552204Sesaxe
562204Sesaxe static void
usage(void)572204Sesaxe usage(void)
582204Sesaxe {
592204Sesaxe (void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
602204Sesaxe basename(progname));
612204Sesaxe exit(1);
622204Sesaxe }
632204Sesaxe
642204Sesaxe int
main(int argc,char * argv[])652204Sesaxe main(int argc, char *argv[])
662204Sesaxe {
672204Sesaxe char *nodename = NULL;
68*3371Sstevel char c_hostname[MAXHOSTNAMELEN];
692204Sesaxe int optlet;
702204Sesaxe char *optstring = "?";
712204Sesaxe
722204Sesaxe (void) setlocale(LC_ALL, "");
732204Sesaxe (void) textdomain(TEXT_DOMAIN);
742204Sesaxe
752204Sesaxe progname = argv[0];
762204Sesaxe
772204Sesaxe opterr = 0;
782204Sesaxe while ((optlet = getopt(argc, argv, optstring)) != -1) {
792204Sesaxe switch (optlet) {
802204Sesaxe case '?':
812204Sesaxe usage();
822204Sesaxe }
832204Sesaxe }
842204Sesaxe
852204Sesaxe /*
862204Sesaxe * if called with no options, just print out the hostname/nodename
872204Sesaxe */
882204Sesaxe if (argc <= optind) {
89*3371Sstevel if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
902204Sesaxe (void) fprintf(stderr,
912204Sesaxe gettext("%s: unable to obtain hostname\n"),
922204Sesaxe basename(progname));
932204Sesaxe exit(1);
94*3371Sstevel } else {
95*3371Sstevel (void) fprintf(stdout, "%s\n", c_hostname);
962204Sesaxe }
972204Sesaxe } else {
982204Sesaxe /*
992204Sesaxe * if called with an argument,
1002204Sesaxe * we have to try to set the new hostname/nodename
1012204Sesaxe */
1022204Sesaxe if (argc > optind + 1)
1032204Sesaxe usage(); /* too many arguments */
1042204Sesaxe
1052204Sesaxe nodename = argv[optind];
106*3371Sstevel if (sethostname(nodename, strlen(nodename)) < 0) {
1072204Sesaxe int err = errno;
1082204Sesaxe (void) fprintf(stderr,
1092204Sesaxe gettext("%s: error in setting name: %s\n"),
1102204Sesaxe basename(progname), strerror(err));
1112204Sesaxe exit(1);
1122204Sesaxe }
1132204Sesaxe }
1142204Sesaxe return (0);
1152204Sesaxe }
116