xref: /csrg-svn/bin/hostname/hostname.c (revision 34655)
119842Sdist /*
2*34655Sbostic  * Copyright (c) 1983, 1988 Regents of the University of California.
3*34655Sbostic  * All rights reserved.
4*34655Sbostic  *
5*34655Sbostic  * Redistribution and use in source and binary forms are permitted
6*34655Sbostic  * provided that this notice is preserved and that due credit is given
7*34655Sbostic  * to the University of California at Berkeley. The name of the University
8*34655Sbostic  * may not be used to endorse or promote products derived from this
9*34655Sbostic  * software without specific prior written permission. This software
10*34655Sbostic  * is provided ``as is'' without express or implied warranty.
1119842Sdist  */
1219842Sdist 
1314465Ssam #ifndef lint
1419842Sdist char copyright[] =
15*34655Sbostic "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
1619842Sdist  All rights reserved.\n";
17*34655Sbostic #endif /* not lint */
1819842Sdist 
1919842Sdist #ifndef lint
20*34655Sbostic static char sccsid[] = "@(#)hostname.c	5.2 (Berkeley) 06/06/88";
21*34655Sbostic #endif /* not lint */
2219842Sdist 
236488Ssklower #include <stdio.h>
24*34655Sbostic #include <sys/param.h>
256488Ssklower 
266488Ssklower main(argc,argv)
27*34655Sbostic 	int argc;
28*34655Sbostic 	char **argv;
296488Ssklower {
30*34655Sbostic 	extern int optind;
31*34655Sbostic 	int ch, sflag;
32*34655Sbostic 	char hostname[MAXHOSTNAMELEN], *p, *index();
3311773Srrh 
34*34655Sbostic 	sflag = 0;
35*34655Sbostic 	while ((ch = getopt(argc, argv, "s")) != EOF)
36*34655Sbostic 		switch((char)ch) {
37*34655Sbostic 		case 's':
38*34655Sbostic 			sflag = 1;
39*34655Sbostic 			break;
40*34655Sbostic 		case '?':
41*34655Sbostic 		default:
42*34655Sbostic 			fputs("hostname [-s] [hostname]\n", stderr);
43*34655Sbostic 			exit(1);
44*34655Sbostic 		}
45*34655Sbostic 	argv += optind;
46*34655Sbostic 
47*34655Sbostic 	if (*argv) {
48*34655Sbostic 		if (sethostname(*argv, strlen(*argv))) {
496488Ssklower 			perror("sethostname");
50*34655Sbostic 			exit(1);
51*34655Sbostic 		}
526488Ssklower 	} else {
53*34655Sbostic 		if (gethostname(hostname, sizeof(hostname))) {
54*34655Sbostic 			perror("gethostname");
55*34655Sbostic 			exit(1);
56*34655Sbostic 		}
57*34655Sbostic 		if (sflag && (p = index(hostname, '.')))
58*34655Sbostic 			*p = '\0';
59*34655Sbostic 		puts(hostname);
606488Ssklower 	}
61*34655Sbostic 	exit(0);
626488Ssklower }
63