hostname.c revision 20d159bb22d487d2a4feb5d283a2b7d832bff8d5
342N/A/*
1879N/A * CDDL HEADER START
342N/A *
342N/A * The contents of this file are subject to the terms of the
342N/A * Common Development and Distribution License (the "License").
342N/A * You may not use this file except in compliance with the License.
342N/A *
342N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
342N/A * or http://www.opensolaris.org/os/licensing.
342N/A * See the License for the specific language governing permissions
342N/A * and limitations under the License.
342N/A *
342N/A * When distributing Covered Code, include this CDDL HEADER in each
342N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
342N/A * If applicable, add the following below this CDDL HEADER, with the
342N/A * fields enclosed by brackets "[]" replaced with your own identifying
342N/A * information: Portions Copyright [yyyy] [name of copyright owner]
342N/A *
1472N/A * CDDL HEADER END
1472N/A */
1472N/A
342N/A/*
342N/A * University Copyright- Copyright (c) 1982, 1986, 1988
342N/A * The Regents of the University of California
1879N/A * All Rights Reserved
1879N/A *
1879N/A * University Acknowledgment- Portions of this document are derived from
1879N/A * software developed by the University of California, Berkeley, and its
1879N/A * contributors.
1879N/A */
1879N/A
342N/A/*
342N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
342N/A * Use is subject to license terms.
342N/A */
342N/A
342N/A/* Portions Copyright 2007 Jeremy Teo */
342N/A/* Portions Copyright 2006 Stephen P. Potter */
342N/A#pragma ident "%Z%%M% %I% %E% SMI"
342N/A
342N/A#include <unistd.h>
342N/A#include <stdio.h>
342N/A#include <string.h>
342N/A#include <locale.h>
342N/A#include <libgen.h>
342N/A#include <stdlib.h>
342N/A#include <errno.h>
342N/A#include <netdb.h>
342N/A
342N/A#ifndef TEXT_DOMAIN /* should be defined by cc -D */
342N/A#define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
342N/A#endif
342N/A
342N/Astatic char *progname;
342N/A
342N/Astatic void
845N/Ausage(void)
845N/A{
845N/A (void) fprintf(stderr, gettext("usage: %s [system_name]\n"),
845N/A basename(progname));
342N/A exit(1);
342N/A}
342N/A
342N/Aint
845N/Amain(int argc, char *argv[])
845N/A{
342N/A char *nodename = NULL;
342N/A char c_hostname[MAXHOSTNAMELEN];
342N/A int optlet;
845N/A char *optstring = "?";
342N/A
342N/A (void) setlocale(LC_ALL, "");
342N/A (void) textdomain(TEXT_DOMAIN);
845N/A
845N/A progname = argv[0];
845N/A
845N/A opterr = 0;
845N/A while ((optlet = getopt(argc, argv, optstring)) != -1) {
342N/A switch (optlet) {
342N/A case '?':
845N/A usage();
845N/A }
845N/A }
342N/A
845N/A /*
2171N/A * if called with no options, just print out the hostname/nodename
2171N/A */
2171N/A if (argc <= optind) {
2171N/A if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
845N/A (void) fprintf(stderr,
2171N/A gettext("%s: unable to obtain hostname\n"),
2171N/A basename(progname));
2171N/A exit(1);
2171N/A } else {
845N/A (void) fprintf(stdout, "%s\n", c_hostname);
342N/A }
342N/A } else {
342N/A /*
342N/A * if called with an argument,
342N/A * we have to try to set the new hostname/nodename
342N/A */
342N/A if (argc > optind + 1)
342N/A usage(); /* too many arguments */
342N/A
342N/A nodename = argv[optind];
342N/A if (sethostname(nodename, strlen(nodename)) < 0) {
342N/A int err = errno;
342N/A (void) fprintf(stderr,
342N/A gettext("%s: error in setting name: %s\n"),
342N/A basename(progname), strerror(err));
342N/A exit(1);
342N/A }
342N/A }
342N/A return (0);
342N/A}
342N/A