xref: /original-bsd/old/hostid/hostid.c (revision 0fc6f013)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)hostid.c	5.4 (Berkeley) 05/19/86";
15 #endif not lint
16 
17 #include <sys/types.h>
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <netdb.h>
21 
22 extern	char *index();
23 extern	unsigned long inet_addr();
24 extern	long gethostid();
25 
26 main(argc, argv)
27 	int argc;
28 	char **argv;
29 {
30 	register char *id;
31 	u_long addr;
32 	long hostid;
33 	struct hostent *hp;
34 
35 	if (argc < 2) {
36 		printf("%#x\n", gethostid());
37 		exit(0);
38 	}
39 
40 	id = argv[1];
41 	if (hp = gethostbyname(id)) {
42 		bcopy(hp->h_addr, &addr, sizeof(addr));
43 		hostid = addr;
44 	} else if (index(id, '.')) {
45 		if ((hostid = inet_addr(id)) == -1)
46 			goto usage;
47 	} else {
48 		if (id[0] == '0' && (id[1] == 'x' || id[1] == 'X'))
49 			id += 2;
50 		if (sscanf(id, "%x", &hostid) != 1) {
51 usage:
52 			fprintf(stderr,
53 			    "usage: %s [hexnum or internet address]\n",
54 			    argv[0]);
55 			exit(1);
56 		}
57 	}
58 
59 	if (sethostid(hostid) < 0) {
60 		perror("sethostid");
61 		exit(1);
62 	}
63 
64 	exit(0);
65 }
66