1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1988, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)hostname.c 8.2 (Berkeley) 04/28/95"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 20 #include <err.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <unistd.h> 25 26 void usage __P((void)); 27 28 int 29 main(argc,argv) 30 int argc; 31 char *argv[]; 32 { 33 int ch, sflag; 34 char *p, hostname[MAXHOSTNAMELEN]; 35 36 sflag = 0; 37 while ((ch = getopt(argc, argv, "s")) != -1) 38 switch (ch) { 39 case 's': 40 sflag = 1; 41 break; 42 case '?': 43 default: 44 usage(); 45 } 46 argc -= optind; 47 argv += optind; 48 49 if (*argv) { 50 if (sethostname(*argv, strlen(*argv))) 51 err(1, "sethostname"); 52 } else { 53 if (gethostname(hostname, sizeof(hostname))) 54 err(1, "gethostname"); 55 if (sflag && (p = strchr(hostname, '.'))) 56 *p = '\0'; 57 (void)printf("%s\n", hostname); 58 } 59 exit(0); 60 } 61 62 void 63 usage() 64 { 65 66 (void)fprintf(stderr, "usage: hostname [-s] [hostname]\n"); 67 exit(1); 68 } 69