xref: /original-bsd/lib/libc/net/ns_ntoa.c (revision fa921481)
1 /*
2  * Copyright (c) 1986 Regents of the University of California.
3  * 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	6.5 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and 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 	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 
28 	net.net_e = addr.x_net;
29 	sprintf(obuf, "%lx", ntohl(net.long_e));
30 	cp = spectHex(obuf);
31 	cp2 = cp + 1;
32 	while (*up==0 && up < uplim) up++;
33 	if (up == uplim) {
34 		if (port) {
35 			sprintf(cp, ".0");
36 			cp += 2;
37 		}
38 	} else {
39 		sprintf(cp, ".%x", *up++);
40 		while (up < uplim) {
41 			while (*cp) cp++;
42 			sprintf(cp, "%02x", *up++);
43 		}
44 		cp = spectHex(cp2);
45 	}
46 	if (port) {
47 		sprintf(cp, ".%x", port);
48 		spectHex(cp + 1);
49 	}
50 	return (obuf);
51 }
52 
53 static char *
54 spectHex(p0)
55 char *p0;
56 {
57 	int ok = 0;
58 	int nonzero = 0;
59 	register char *p = p0;
60 	for (; *p; p++) switch (*p) {
61 
62 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
63 		*p += ('A' - 'a');
64 		/* fall into . . . */
65 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
66 		ok = 1;
67 	case '1': case '2': case '3': case '4': case '5':
68 	case '6': case '7': case '8': case '9':
69 		nonzero = 1;
70 	}
71 	if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
72 	return (p);
73 }
74