hostname.c revision 91084
155682Smarkm/* 278527Sassar * Copyright (c) 1988, 1993 355682Smarkm * The Regents of the University of California. All rights reserved. 455682Smarkm * 555682Smarkm * Redistribution and use in source and binary forms, with or without 655682Smarkm * modification, are permitted provided that the following conditions 755682Smarkm * are met: 855682Smarkm * 1. Redistributions of source code must retain the above copyright 955682Smarkm * notice, this list of conditions and the following disclaimer. 1055682Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1155682Smarkm * notice, this list of conditions and the following disclaimer in the 1255682Smarkm * documentation and/or other materials provided with the distribution. 1355682Smarkm * 3. All advertising materials mentioning features or use of this software 1455682Smarkm * must display the following acknowledgement: 1555682Smarkm * This product includes software developed by the University of 1655682Smarkm * California, Berkeley and its contributors. 1755682Smarkm * 4. Neither the name of the University nor the names of its contributors 1855682Smarkm * may be used to endorse or promote products derived from this software 1955682Smarkm * without specific prior written permission. 2055682Smarkm * 2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2455682Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3155682Smarkm * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 3455682Smarkm#ifndef lint 3555682Smarkmstatic char const copyright[] = 36178825Sdfr"@(#) Copyright (c) 1988, 1993\n\ 3755682Smarkm The Regents of the University of California. All rights reserved.\n"; 3855682Smarkm#endif /* not lint */ 39178825Sdfr 4055682Smarkm#ifndef lint 4155682Smarkm#if 0 4255682Smarkmstatic char sccsid[] = "@(#)hostname.c 8.1 (Berkeley) 5/31/93"; 4355682Smarkm#endif 4455682Smarkmstatic const char rcsid[] = 4555682Smarkm "$FreeBSD: head/bin/hostname/hostname.c 91084 2002-02-22 21:11:03Z markm $"; 4655682Smarkm#endif /* not lint */ 4755682Smarkm 4855682Smarkm#include <sys/param.h> 4955682Smarkm 5055682Smarkm#include <err.h> 5155682Smarkm#include <stdio.h> 5255682Smarkm#include <stdlib.h> 5355682Smarkm#include <string.h> 5455682Smarkm#include <unistd.h> 5555682Smarkm 5655682Smarkmvoid usage(void); 5755682Smarkm 5855682Smarkmint 5955682Smarkmmain(int argc, char *argv[]) 6055682Smarkm{ 6155682Smarkm int ch, sflag; 6255682Smarkm char *p, hostname[MAXHOSTNAMELEN]; 6355682Smarkm 6455682Smarkm sflag = 0; 6555682Smarkm while ((ch = getopt(argc, argv, "s")) != -1) 6655682Smarkm switch (ch) { 6755682Smarkm case 's': 6855682Smarkm sflag = 1; 6955682Smarkm break; 7055682Smarkm case '?': 7155682Smarkm default: 7255682Smarkm usage(); 7355682Smarkm } 7455682Smarkm argc -= optind; 7555682Smarkm argv += optind; 7655682Smarkm 7755682Smarkm if (argc > 1) 7855682Smarkm usage(); 7955682Smarkm 8055682Smarkm if (*argv) { 8155682Smarkm if (sethostname(*argv, (int)strlen(*argv))) 8255682Smarkm err(1, "sethostname"); 8355682Smarkm } else { 8455682Smarkm if (gethostname(hostname, (int)sizeof(hostname))) 8555682Smarkm err(1, "gethostname"); 8655682Smarkm if (sflag) { 8755682Smarkm p = strchr(hostname, '.'); 8855682Smarkm if (p != NULL) 8955682Smarkm *p = '\0'; 9055682Smarkm } 9155682Smarkm (void)printf("%s\n", hostname); 9255682Smarkm } 9355682Smarkm exit(0); 9455682Smarkm} 9578527Sassar 9655682Smarkmvoid 9755682Smarkmusage(void) 9855682Smarkm{ 9955682Smarkm 10055682Smarkm (void)fprintf(stderr, "usage: hostname [-s] [name-of-host]\n"); 10155682Smarkm exit(1); 10255682Smarkm} 10355682Smarkm