1 /* ip4atos() converts binary IP4 address into textual printable
2 * dotted-quad form.
3 */
4
5 #include "ip4addr.h"
6
7 /* helper routine for ip4atos() */
8
oct(char * s,unsigned char o,char e)9 static char *oct(char *s, unsigned char o, char e) {
10 if (o >= 100) {
11 *s++ = o / 100 + '0', o %= 100;
12 *s++ = o / 10 + '0', o %= 10;
13 }
14 else if (o >= 10)
15 *s++ = o / 10 + '0', o %= 10;
16 *s++ = o + '0';
17 *s++ = e;
18 return s;
19 }
20
21 /* return printable representation of ip4addr like inet_ntoa() */
22
ip4atos(ip4addr_t a)23 const char *ip4atos(ip4addr_t a) {
24 static char buf[16];
25 oct(oct(oct(oct(buf,
26 (a >> 24) & 0xff, '.'),
27 (a >> 16) & 0xff, '.'),
28 (a >> 8) & 0xff, '.'),
29 a & 0xff, '\0');
30 return buf;
31 }
32