xref: /freebsd/contrib/ntp/libntp/numtoa.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*
2  * numtoa - return asciized network numbers store in local array space
3  */
4 #include <config.h>
5 
6 #include <sys/types.h>
7 #ifdef HAVE_NETINET_IN_H
8 #include <netinet/in.h>		/* ntohl */
9 #endif
10 
11 #include <stdio.h>
12 
13 #include "ntp_fp.h"
14 #include "lib_strbuf.h"
15 #include "ntp_stdlib.h"
16 
17 char *
18 numtoa(
19 	u_int32 num
20 	)
21 {
22 	register u_int32 netnum;
23 	register char *buf;
24 
25 	netnum = ntohl(num);
26 	LIB_GETBUF(buf);
27 	snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu",
28 		 ((u_long)netnum >> 24) & 0xff,
29 		 ((u_long)netnum >> 16) & 0xff,
30 		 ((u_long)netnum >> 8) & 0xff,
31 		 (u_long)netnum & 0xff);
32 	return buf;
33 }
34 
35 
36 /* Convert a refid & stratum to a string */
37 const char *
38 refid_str(
39 	u_int32	refid,
40 	int	stratum
41 	)
42 {
43 	char *	text;
44 	size_t	tlen;
45 
46 	if (stratum > 1)
47 		return numtoa(refid);
48 
49 	LIB_GETBUF(text);
50 	text[0] = '.';
51 	memcpy(&text[1], &refid, sizeof(refid));
52 	text[1 + sizeof(refid)] = '\0';
53 	tlen = strlen(text);
54 	text[tlen] = '.';
55 	text[tlen + 1] = '\0';
56 
57 	return text;
58 }
59 
60