1 /* 2 * Copyright (c) 1983, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)hostname.c 5.3 (Berkeley) 06/18/88"; 26 #endif /* not lint */ 27 28 #include <stdio.h> 29 #include <sys/param.h> 30 31 main(argc,argv) 32 int argc; 33 char **argv; 34 { 35 extern int optind; 36 int ch, sflag; 37 char hostname[MAXHOSTNAMELEN], *p, *index(); 38 39 sflag = 0; 40 while ((ch = getopt(argc, argv, "s")) != EOF) 41 switch((char)ch) { 42 case 's': 43 sflag = 1; 44 break; 45 case '?': 46 default: 47 fputs("hostname [-s] [hostname]\n", stderr); 48 exit(1); 49 } 50 argv += optind; 51 52 if (*argv) { 53 if (sethostname(*argv, strlen(*argv))) { 54 perror("sethostname"); 55 exit(1); 56 } 57 } else { 58 if (gethostname(hostname, sizeof(hostname))) { 59 perror("gethostname"); 60 exit(1); 61 } 62 if (sflag && (p = index(hostname, '.'))) 63 *p = '\0'; 64 puts(hostname); 65 } 66 exit(0); 67 } 68