xref: /original-bsd/lib/libc/net/ns_ntoa.c (revision e188a54c)
1 /*
2  * Copyright (c) 1986 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)ns_ntoa.c	6.4 (Berkeley) 08/12/88";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <netns/ns.h>
24 
25 char *
26 ns_ntoa(addr)
27 struct ns_addr addr;
28 {
29 	static char obuf[40];
30 	char *spectHex();
31 	union { union ns_net net_e; u_long long_e; } net;
32 	u_short port = htons(addr.x_port);
33 	register char *cp;
34 	char *cp2;
35 	register u_char *up = addr.x_host.c_host;
36 	u_char *uplim = up + 6;
37 
38 	net.net_e = addr.x_net;
39 	sprintf(obuf, "%lx", ntohl(net.long_e));
40 	cp = spectHex(obuf);
41 	cp2 = cp + 1;
42 	while (*up==0 && up < uplim) up++;
43 	if (up == uplim) {
44 		if (port) {
45 			sprintf(cp, ".0");
46 			cp += 2;
47 		}
48 	} else {
49 		sprintf(cp, ".%x", *up++);
50 		while (up < uplim) {
51 			while (*cp) cp++;
52 			sprintf(cp, "%02x", *up++);
53 		}
54 		cp = spectHex(cp2);
55 	}
56 	if (port) {
57 		sprintf(cp, ".%x", port);
58 		spectHex(cp + 1);
59 	}
60 	return (obuf);
61 }
62 
63 static char *
64 spectHex(p0)
65 char *p0;
66 {
67 	int ok = 0;
68 	int nonzero = 0;
69 	register char *p = p0;
70 	for (; *p; p++) switch (*p) {
71 
72 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
73 		*p += ('A' - 'a');
74 		/* fall into . . . */
75 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
76 		ok = 1;
77 	case '1': case '2': case '3': case '4': case '5':
78 	case '6': case '7': case '8': case '9':
79 		nonzero = 1;
80 	}
81 	if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
82 	return (p);
83 }
84