xref: /netbsd/bin/hostname/hostname.c (revision fcb69782)
1*fcb69782Selric /* $NetBSD: hostname.c,v 1.21 2014/02/13 12:00:29 elric Exp $ */
249f0ad86Scgd 
361f28255Scgd /*
4667b5ea1Smycroft  * Copyright (c) 1988, 1993
5667b5ea1Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3251e9bcecSchristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3949f0ad86Scgd #if 0
40f859c958Sjtc static char sccsid[] = "@(#)hostname.c	8.2 (Berkeley) 4/28/95";
4149f0ad86Scgd #else
42*fcb69782Selric __RCSID("$NetBSD: hostname.c,v 1.21 2014/02/13 12:00:29 elric Exp $");
4349f0ad86Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
46667b5ea1Smycroft #include <sys/param.h>
47667b5ea1Smycroft 
48667b5ea1Smycroft #include <err.h>
4961f28255Scgd #include <stdio.h>
500eee1ab6Sjtc #include <stdlib.h>
510eee1ab6Sjtc #include <string.h>
520eee1ab6Sjtc #include <unistd.h>
5361f28255Scgd 
54007a7cf0Sjoerg __dead static void usage(void);
550eee1ab6Sjtc 
56667b5ea1Smycroft int
main(int argc,char * argv[])57354d883bSwiz main(int argc, char *argv[])
5861f28255Scgd {
59*fcb69782Selric 	int ch, sflag;
602beab49aSmrg 	char *p, hostname[MAXHOSTNAMELEN + 1];
6161f28255Scgd 
62354d883bSwiz 	setprogname(argv[0]);
63*fcb69782Selric 	sflag = 0;
64*fcb69782Selric 	while ((ch = getopt(argc, argv, "s")) != -1)
65667b5ea1Smycroft 		switch (ch) {
6661f28255Scgd 		case 's':
6761f28255Scgd 			sflag = 1;
6861f28255Scgd 			break;
6961f28255Scgd 		case '?':
7061f28255Scgd 		default:
710eee1ab6Sjtc 			usage();
7261f28255Scgd 		}
73667b5ea1Smycroft 	argc -= optind;
7461f28255Scgd 	argv += optind;
7561f28255Scgd 
76ea92d19cSmycroft 	if (argc > 1)
77ea92d19cSmycroft 		usage();
78ea92d19cSmycroft 
7961f28255Scgd 	if (*argv) {
80667b5ea1Smycroft 		if (sethostname(*argv, strlen(*argv)))
81667b5ea1Smycroft 			err(1, "sethostname");
8261f28255Scgd 	} else {
83667b5ea1Smycroft 		if (gethostname(hostname, sizeof(hostname)))
84667b5ea1Smycroft 			err(1, "gethostname");
852beab49aSmrg 		hostname[sizeof(hostname) - 1] = '\0';
86667b5ea1Smycroft 		if (sflag && (p = strchr(hostname, '.')))
8761f28255Scgd 			*p = '\0';
88*fcb69782Selric 		(void)printf("%s\n", hostname);
8961f28255Scgd 	}
9061f28255Scgd 	exit(0);
919dc385beSmycroft 	/* NOTREACHED */
9261f28255Scgd }
930eee1ab6Sjtc 
94007a7cf0Sjoerg static void
usage(void)95354d883bSwiz usage(void)
960eee1ab6Sjtc {
97*fcb69782Selric 	(void)fprintf(stderr, "usage: %s [-s] [name-of-host]\n",
98354d883bSwiz 	    getprogname());
990eee1ab6Sjtc 	exit(1);
1009dc385beSmycroft 	/* NOTREACHED */
1010eee1ab6Sjtc }
102