linux系統編程快速定位標頭檔的技巧之強大的grep命令,linuxgrep

來源:互聯網
上載者:User

linux系統編程快速定位標頭檔的技巧之強大的grep命令,linuxgrep

這個技巧來自於我的實際開發碰到的:

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_defined51:typedef unsigned int        uint32_t;52:# define __uint32_t_defined

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.