欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

linux系统编程快速定位头文件的技巧之强大的grep命令

程序员文章站 2022-07-05 23:48:55
这个技巧来自于我的实际开发碰到的: inet_addr这个函数用于把ip地址转成网络字节序,他的原型:in_addr_t inet_addr(const char *cp); 返回值为一个in_addr_t的类型,很显然这不是一个c语言的标准数据类型,想搞清楚他到底是什么类型,肯定要找到头文件,在l ......

这个技巧来自于我的实际开发碰到的:

inet_addr这个函数用于把ip地址转成网络字节序,他的原型:in_addr_t inet_addr(const char *cp);

返回值为一个in_addr_t的类型,很显然这不是一个c语言的标准数据类型,想搞清楚他到底是什么类型,肯定要找到头文件,在linux系统上,头文件一般放在/usr/include下面,但是这下面非常多的头文件,根本不知道是哪个,所以:

1,第一次:grep in_addr_t /usr/include/*.h  没有结果,说明不在/usr/include的第一层目录中

2,第二次: grep "in_addr_t" /usr/include/*/*.h, 这一次出现很多结果

/usr/include/arpa/inet.h:extern in_addr_t inet_addr (const char *__cp) __THROW;
/usr/include/arpa/inet.h:extern in_addr_t inet_lnaof (struct in_addr __in) __THROW;
/usr/include/arpa/inet.h:extern struct in_addr inet_makeaddr (in_addr_t __net, in_addr_t __host)
/usr/include/arpa/inet.h:extern in_addr_t inet_netof (struct in_addr __in) __THROW;
/usr/include/arpa/inet.h:extern in_addr_t inet_network (const char *__cp) __THROW;
/usr/include/arpa/inet.h:extern char *inet_neta (in_addr_t __net, char *__buf, size_t __len) __THROW;
/usr/include/netinet/in.h:typedef uint32_t in_addr_t;
/usr/include/netinet/in.h:    in_addr_t s_addr;
/usr/include/netinet/in.h:#define    IN_CLASSA(a)        ((((in_addr_t)(a)) & 0x80000000) == 0)
/usr/include/netinet/in.h:#define    IN_CLASSB(a)        ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000)
/usr/include/netinet/in.h:#define    IN_CLASSC(a)        ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000)
/usr/include/netinet/in.h:#define    IN_CLASSD(a)        ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000)
/usr/include/netinet/in.h:#define    IN_EXPERIMENTAL(a)    ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000)
/usr/include/netinet/in.h:#define    IN_BADCLASS(a)        ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000)
/usr/include/netinet/in.h:#define    INADDR_ANY        ((in_addr_t) 0x00000000)
/usr/include/netinet/in.h:#define    INADDR_BROADCAST    ((in_addr_t) 0xffffffff)
/usr/include/netinet/in.h:#define    INADDR_NONE        ((in_addr_t) 0xffffffff)
/usr/include/netinet/in.h:# define INADDR_LOOPBACK    ((in_addr_t) 0x7f000001) /* Inet 127.0.0.1.  */
/usr/include/netinet/in.h:#define INADDR_UNSPEC_GROUP    ((in_addr_t) 0xe0000000) /* 224.0.0.0 */
/usr/include/netinet/in.h:#define INADDR_ALLHOSTS_GROUP    ((in_addr_t) 0xe0000001) /* 224.0.0.1 */
/usr/include/netinet/in.h:#define INADDR_ALLRTRS_GROUP    ((in_addr_t) 0xe0000002) /* 224.0.0.2 */
/usr/include/netinet/in.h:#define INADDR_MAX_LOCAL_GROUP  ((in_addr_t) 0xe00000ff) /* 224.0.0.255 */

3,过滤,grep "in_addr_t" /usr/include/*/*.h | grep "typedef"  

用typedef过滤一次,或者用define等关键字, 这种数据类型肯定是标准类型的别名定义,出现下面这条数据,他是uint32_t这个数据类型的别名

/usr/include/netinet/in.h:typedef uint32_t in_addr_t;

4,下一步,肯定是找uint32_t的定义类型 grep "uint32_t" /usr/include/*/*.h | grep "typedef",出现结果:

/usr/include/drm/drm.h:typedef uint32_t __u32;
/usr/include/netinet/in.h:typedef uint32_t in_addr_t;

这不是我想要的

5,grep "uint32_t" /usr/include/*.h | grep "typedef"

/usr/include/elf.h:typedef uint32_t Elf32_Word;
/usr/include/elf.h:typedef uint32_t Elf64_Word;
/usr/include/elf.h:typedef uint32_t Elf32_Addr;
/usr/include/elf.h:typedef uint32_t Elf32_Off;
/usr/include/stdint.h:typedef unsigned int        uint32_t;

这才是我想要的, uint32_t其实是unsigned int类型

6,grep -n "uint32_t" /usr/include/stdint.h 查出数据定义所在的行号

50:#ifndef __uint32_t_defined
51:typedef unsigned int        uint32_t;
52:# define __uint32_t_defined