hostname.c revision 20415
1119895Sanholt/* 2152909Sanholt * Copyright (c) 1988, 1993 3152909Sanholt * The Regents of the University of California. All rights reserved. 4139749Simp * 5119895Sanholt * Redistribution and use in source and binary forms, with or without 6119895Sanholt * modification, are permitted provided that the following conditions 7119895Sanholt * are met: 8119895Sanholt * 1. Redistributions of source code must retain the above copyright 9119895Sanholt * notice, this list of conditions and the following disclaimer. 10119895Sanholt * 2. Redistributions in binary form must reproduce the above copyright 11119895Sanholt * notice, this list of conditions and the following disclaimer in the 12119895Sanholt * documentation and/or other materials provided with the distribution. 13119895Sanholt * 3. All advertising materials mentioning features or use of this software 14145132Sanholt * must display the following acknowledgement: 15119895Sanholt * This product includes software developed by the University of 16119895Sanholt * California, Berkeley and its contributors. 17119895Sanholt * 4. Neither the name of the University nor the names of its contributors 18145132Sanholt * may be used to endorse or promote products derived from this software 19119895Sanholt * without specific prior written permission. 20119895Sanholt * 21119895Sanholt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22119895Sanholt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23119895Sanholt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24119895Sanholt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25119895Sanholt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26145132Sanholt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27119895Sanholt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28119895Sanholt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29145132Sanholt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30119895Sanholt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31119895Sanholt * SUCH DAMAGE. 32152909Sanholt * 33152909Sanholt * $Id: hostname.c,v 1.2 1994/09/24 02:55:40 davidg Exp $ 34152909Sanholt */ 35119895Sanholt 36119895Sanholt#ifndef lint 37119895Sanholtstatic char const copyright[] = 38119895Sanholt"@(#) Copyright (c) 1988, 1993\n\ 39119895Sanholt The Regents of the University of California. All rights reserved.\n"; 40119895Sanholt#endif /* not lint */ 41119895Sanholt 42126533Sobrien#ifndef lint 43119895Sanholtstatic char const sccsid[] = "@(#)hostname.c 8.1 (Berkeley) 5/31/93"; 44119895Sanholt#endif /* not lint */ 45119895Sanholt 46119895Sanholt#include <sys/param.h> 47119895Sanholt 48119895Sanholt#include <err.h> 49119895Sanholt#include <stdio.h> 50119895Sanholt#include <stdlib.h> 51119895Sanholt#include <string.h> 52119895Sanholt#include <unistd.h> 53119895Sanholt 54119895Sanholtvoid usage __P((void)); 55119895Sanholt 56119895Sanholtint 57145132Sanholtmain(argc,argv) 58145132Sanholt int argc; 59145132Sanholt char *argv[]; 60145132Sanholt{ 61145132Sanholt int ch, sflag; 62119895Sanholt char *p, hostname[MAXHOSTNAMELEN]; 63119895Sanholt 64119895Sanholt sflag = 0; 65145132Sanholt while ((ch = getopt(argc, argv, "s")) != EOF) 66119895Sanholt switch (ch) { 67119895Sanholt case 's': 68119895Sanholt sflag = 1; 69119895Sanholt break; 70119895Sanholt case '?': 71119895Sanholt default: 72119895Sanholt usage(); 73119895Sanholt } 74119895Sanholt argc -= optind; 75119895Sanholt argv += optind; 76119895Sanholt 77119895Sanholt if (argc > 1) 78119895Sanholt usage(); 79119895Sanholt 80157617Sanholt if (*argv) { 81145132Sanholt if (sethostname(*argv, strlen(*argv))) 82145132Sanholt err(1, "sethostname"); 83119895Sanholt } else { 84119895Sanholt if (gethostname(hostname, sizeof(hostname))) 85119895Sanholt err(1, "gethostname"); 86119895Sanholt if (sflag && (p = strchr(hostname, '.'))) 87119895Sanholt *p = '\0'; 88119895Sanholt (void)printf("%s\n", hostname); 89119895Sanholt } 90145132Sanholt exit(0); 91119895Sanholt} 92145132Sanholt 93145132Sanholtvoid 94119895Sanholtusage() 95119895Sanholt{ 96119895Sanholt 97119895Sanholt (void)fprintf(stderr, "usage: hostname [-s] [hostname]\n"); 98119895Sanholt exit(1); 99119895Sanholt} 100119895Sanholt