xref: /original-bsd/bin/hostname/hostname.c (revision e59fb703)
1 /*
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)hostname.c	5.4 (Berkeley) 05/31/90";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <sys/param.h>
20 
21 main(argc,argv)
22 	int argc;
23 	char **argv;
24 {
25 	extern int optind;
26 	int ch, sflag;
27 	char hostname[MAXHOSTNAMELEN], *p, *index();
28 
29 	sflag = 0;
30 	while ((ch = getopt(argc, argv, "s")) != EOF)
31 		switch((char)ch) {
32 		case 's':
33 			sflag = 1;
34 			break;
35 		case '?':
36 		default:
37 			fputs("hostname [-s] [hostname]\n", stderr);
38 			exit(1);
39 		}
40 	argv += optind;
41 
42 	if (*argv) {
43 		if (sethostname(*argv, strlen(*argv))) {
44 			perror("sethostname");
45 			exit(1);
46 		}
47 	} else {
48 		if (gethostname(hostname, sizeof(hostname))) {
49 			perror("gethostname");
50 			exit(1);
51 		}
52 		if (sflag && (p = index(hostname, '.')))
53 			*p = '\0';
54 		puts(hostname);
55 	}
56 	exit(0);
57 }
58