Here is an example gperf program. I included NUL and a new line in the sample data to prove that they did not fail.
%{ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <inttypes.h> #include <arpa/inet.h> %} %% "\xc0\x01\x02\x03" "\xc0\xff\xff\xff" "\xc0\xff\x00\xff" "\xc0\x0a\xff\xff" %% int main(int argc, const char **argv) { int i; for(i=1;i<argc;++i) { uint32_t addr = ntohl(strtoul(argv[i], 0, 16)); if(in_word_set((char *)&addr, 4)) printf("0x%08"PRIx32" is in the list.\n", htonl(addr)); else printf("0x%08"PRIx32" is not in the list.\n", htonl(addr)); } return 0; }
Save as addrs.gperf
, compile and test with
gperf -l addrs.gperf > addrs.c gcc addrs.c -o addrs ./addrs c0000000 c0010203 c0ffffff c00affff c0ff0aff c0ffff00 c0ff00ff
source share