640 number_to_scaled_string is duplicated in several commands
[unleashed.git] / usr / src / cmd / hostname / hostname.c
blobac7f9f84ed324fb165a2c1bac4581b3a3add6aa6
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * University Copyright- Copyright (c) 1982, 1986, 1988
24 * The Regents of the University of California
25 * All Rights Reserved
27 * University Acknowledgment- Portions of this document are derived from
28 * software developed by the University of California, Berkeley, and its
29 * contributors.
33 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
34 * Use is subject to license terms.
37 /* Portions Copyright 2007 Jeremy Teo */
38 /* Portions Copyright 2006 Stephen P. Potter */
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 <netdb.h>
49 #ifndef TEXT_DOMAIN /* should be defined by cc -D */
50 #define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
51 #endif
53 static char *progname;
55 static void
56 usage(void)
58 (void) fprintf(stderr, gettext("usage: %s [-s] [system_name]\n"),
59 basename(progname));
60 exit(1);
63 int
64 main(int argc, char *argv[])
66 char *nodename = NULL;
67 char c_hostname[MAXHOSTNAMELEN];
68 int optlet;
69 int sflag = 0;
70 char *optstring = "s";
72 (void) setlocale(LC_ALL, "");
73 (void) textdomain(TEXT_DOMAIN);
75 progname = argv[0];
77 opterr = 0;
78 while ((optlet = getopt(argc, argv, optstring)) != -1) {
79 switch (optlet) {
80 case 's':
81 sflag = 1;
82 break;
83 case '?':
84 usage();
85 break;
90 * if called with no arguments, just print out the hostname/nodename
92 if (argc <= optind) {
93 if (gethostname(c_hostname, sizeof (c_hostname)) < 0) {
94 (void) fprintf(stderr,
95 gettext("%s: unable to obtain hostname\n"),
96 basename(progname));
97 exit(1);
98 } else {
99 if (sflag)
100 c_hostname[strcspn(c_hostname, ".")] = '\0';
101 (void) fprintf(stdout, "%s\n", c_hostname);
103 } else {
105 * if called with an argument,
106 * we have to try to set the new hostname/nodename
108 if (argc > optind + 1)
109 usage(); /* too many arguments */
111 nodename = argv[optind];
112 if (sethostname(nodename, strlen(nodename)) < 0) {
113 int err = errno;
114 (void) fprintf(stderr,
115 gettext("%s: error in setting name: %s\n"),
116 basename(progname), strerror(err));
117 exit(1);
120 return (0);