- 论坛徽章:
- 0
|
找到了两个实现,在sys/libkern/inet_ntoa.c 中,
http://fxr.watson.org/fxr/source/libkern/inet_ntoa.c?v=FREEBSD6
- #include <sys/cdefs.h>
- 31 __FBSDID("$FreeBSD$");
- 32
- 33 #include <sys/param.h>
- 34 #include <sys/systm.h>
- 35
- 36 #include <netinet/in.h>
- 37
- 38 char *
- 39 inet_ntoa(struct in_addr ina)
- 40 {
- 41 static char buf[4*sizeof "123"];
- 42 unsigned char *ucp = (unsigned char *)&ina;
- 43
- 44 sprintf(buf, "%d.%d.%d.%d",
- 45 ucp[0] & 0xff,
- 46 ucp[1] & 0xff,
- 47 ucp[2] & 0xff,
- 48 ucp[3] & 0xff);
- 49 return buf;
- 50 }
- 51
- 52 char *
- 53 inet_ntoa_r(struct in_addr ina, char *buf)
- 54 {
- 55 unsigned char *ucp = (unsigned char *)&ina;
- 56
- 57 sprintf(buf, "%d.%d.%d.%d",
- 58 ucp[0] & 0xff,
- 59 ucp[1] & 0xff,
- 60 ucp[2] & 0xff,
- 61 ucp[3] & 0xff);
- 62 return buf;
- 63 }
复制代码 另外一个
http://fxr.watson.org/fxr/source/libkern/inet_ntoa.c?v=DFBSD
多了个kprintf,还是直接用上面的sprintf的吧。
- #include <sys/param.h>
- 35 #include <sys/systm.h>
- 36
- 37 #include <netinet/in.h>
- 38
- 39 char *
- 40 inet_ntoa(struct in_addr ina)
- 41 {
- 42 static char buf[sizeof "aaa.bbb.ccc.ddd"];
- 43 unsigned char *ucp = (unsigned char *)&ina;
- 44
- 45 ksprintf(buf, "%d.%d.%d.%d",
- 46 ucp[0] & 0xff,
- 47 ucp[1] & 0xff,
- 48 ucp[2] & 0xff,
- 49 ucp[3] & 0xff);
- 50 return buf;
- 51 }
复制代码 :wink: |
|