xref: /csrg-svn/old/hostid/hostid.c (revision 34765)
119842Sdist /*
234657Sbostic  * Copyright (c) 1983, 1988 Regents of the University of California.
334657Sbostic  * All rights reserved.
434657Sbostic  *
534657Sbostic  * Redistribution and use in source and binary forms are permitted
6*34765Sbostic  * provided that the above copyright notice and this paragraph are
7*34765Sbostic  * duplicated in all such forms and that any documentation,
8*34765Sbostic  * advertising materials, and other materials related to such
9*34765Sbostic  * distribution and use acknowledge that the software was developed
10*34765Sbostic  * by the University of California, Berkeley.  The name of the
11*34765Sbostic  * University may not be used to endorse or promote products derived
12*34765Sbostic  * from this software without specific prior written permission.
13*34765Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34765Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34765Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1619842Sdist  */
1719842Sdist 
1814465Ssam #ifndef lint
1919842Sdist char copyright[] =
2034657Sbostic "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
2119842Sdist  All rights reserved.\n";
2234657Sbostic #endif /* not lint */
239112Ssam 
2419842Sdist #ifndef lint
25*34765Sbostic static char sccsid[] = "@(#)hostid.c	5.6 (Berkeley) 06/18/88";
2634657Sbostic #endif /* not lint */
2719842Sdist 
2824917Skarels #include <sys/types.h>
2918476Sralph #include <stdio.h>
3024917Skarels #include <netdb.h>
3118476Sralph 
main(argc,argv)329112Ssam main(argc, argv)
339112Ssam 	int argc;
349112Ssam 	char **argv;
359112Ssam {
3618476Sralph 	register char *id;
3724917Skarels 	struct hostent *hp;
3834657Sbostic 	u_long addr, inet_addr();
3934657Sbostic 	long hostid, gethostid();
4034657Sbostic 	char *index();
419112Ssam 
4218476Sralph 	if (argc < 2) {
4334657Sbostic 		printf("%#lx\n", gethostid());
4418476Sralph 		exit(0);
4518476Sralph 	}
4628306Skarels 
4718476Sralph 	id = argv[1];
4828306Skarels 	if (hp = gethostbyname(id)) {
4928306Skarels 		bcopy(hp->h_addr, &addr, sizeof(addr));
5028306Skarels 		hostid = addr;
5128306Skarels 	} else if (index(id, '.')) {
5228306Skarels 		if ((hostid = inet_addr(id)) == -1)
5328306Skarels 			goto usage;
5428306Skarels 	} else {
5528306Skarels 		if (id[0] == '0' && (id[1] == 'x' || id[1] == 'X'))
5618476Sralph 			id += 2;
5734657Sbostic 		if (sscanf(id, "%lx", &hostid) != 1) {
5834657Sbostic usage:			fputs("usage: hostid [hexnum or internet address]\n",
5934657Sbostic 			    stderr);
6028306Skarels 			exit(1);
6118476Sralph 		}
6218476Sralph 	}
6318476Sralph 
6418476Sralph 	if (sethostid(hostid) < 0) {
6518476Sralph 		perror("sethostid");
6618476Sralph 		exit(1);
6718476Sralph 	}
6818476Sralph 
6918476Sralph 	exit(0);
709112Ssam }
71