xref: /original-bsd/lib/libc/net/ns_ntoa.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)ns_ntoa.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <netns/ns.h>
14 #include <stdio.h>
15 
16 char *
17 ns_ntoa(addr)
18 	struct ns_addr addr;
19 {
20 	static char obuf[40];
21 	union { union ns_net net_e; u_long long_e; } 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 	static char *spectHex();
28 
29 	net.net_e = addr.x_net;
30 	sprintf(obuf, "%lx", ntohl(net.long_e));
31 	cp = spectHex(obuf);
32 	cp2 = cp + 1;
33 	while (*up==0 && up < uplim) up++;
34 	if (up == uplim) {
35 		if (port) {
36 			sprintf(cp, ".0");
37 			cp += 2;
38 		}
39 	} else {
40 		sprintf(cp, ".%x", *up++);
41 		while (up < uplim) {
42 			while (*cp) cp++;
43 			sprintf(cp, "%02x", *up++);
44 		}
45 		cp = spectHex(cp2);
46 	}
47 	if (port) {
48 		sprintf(cp, ".%x", port);
49 		spectHex(cp + 1);
50 	}
51 	return (obuf);
52 }
53 
54 static char *
55 spectHex(p0)
56 	char *p0;
57 {
58 	int ok = 0;
59 	int nonzero = 0;
60 	register char *p = p0;
61 	for (; *p; p++) switch (*p) {
62 
63 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
64 		*p += ('A' - 'a');
65 		/* fall into . . . */
66 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
67 		ok = 1;
68 	case '1': case '2': case '3': case '4': case '5':
69 	case '6': case '7': case '8': case '9':
70 		nonzero = 1;
71 	}
72 	if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
73 	return (p);
74 }
75