xref: /csrg-svn/lib/libc/net/ns_ntoa.c (revision 25987)
1 /*
2  * Copyright (c) 1986 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 
8 #ifndef lint
9 static char sccsid[] = "@(#)ns_ntoa.c	6.1 (Berkeley) 01/29/86";
10 #endif not lint
11 
12 #include <sys/types.h>
13 #include <netns/ns.h>
14 
15 char *
16 ns_ntoa(addr)
17 struct ns_addr addr;
18 {
19 	static char obuf[40];
20 	char *spectHex();
21 	u_long net;
22 	u_short port = htons(addr.x_port);
23 	register char *cp;
24 	char *cp2;
25 	register u_char *up = addr.x_host.c_host;
26 	u_char *uplim = up + 6;
27 
28 	* (union ns_net *) &net = addr.x_net;
29 	net = ntohl(net);
30 	sprintf(obuf, "%lx", net);
31 	cp = spectHex(obuf);
32 	cp2 = cp + 1;
33 	while (*up==0 && up < uplim) up++;
34 	if (up == uplim) return (obuf);
35 	sprintf(cp, ".%x", *up++);
36 	while (up < uplim) {
37 		while (*cp) cp++;
38 		sprintf(cp, "%02x", *up++);
39 	}
40 	cp = spectHex(cp2);
41 	if (port) {
42 		sprintf(cp, ".%x", port);
43 		spectHex(cp + 1);
44 	}
45 	return (obuf);
46 }
47 
48 static char *
49 spectHex(p0)
50 char *p0;
51 {
52 	int ok = 0;
53 	int nonzero = 0;
54 	register char *p = p0;
55 	for (; *p; p++) switch (*p) {
56 
57 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
58 		*p += ('A' - 'a');
59 		/* fall into . . . */
60 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
61 		ok = 1;
62 	case '1': case '2': case '3': case '4': case '5':
63 	case '6': case '7': case '8': case '9':
64 		nonzero = 1;
65 	}
66 	if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
67 	return (p);
68 }
69