int main() { entry **t = malloc(sizeof(entry *)*100010); char *s = malloc(sizeof(char)*128); freopen("big_file.txt","r",stdin); freopen("big_file_sorted.txt","w",stdout); unsigned int cnt = 0; unsigned a, b, c, d; while (scanf("%s %u.%u.%u.%u",s, &a, &b, &c, &d)!=EOF) { entry *temp =(entry *) malloc(sizeof(entry)); temp->host = s; temp->ip = ip2int(a, b, c, d); t[cnt++] = temp; s = malloc(sizeof(char)*128); } qsort(t, cnt, sizeof(entry*),cmp); int i; printf("Comparari %lld %lld\n", comparari, comparari2); for (i = 0; i < cnt; ++ i) printf("%s %u ", t[i]->host, t[i]->ip), int2ip(t[i]->ip); return 0; }
static int loadlist_ascii(const char *filename, const char *charset) { stream_t s; char buf[MAX_LINE_LENGTH + 1], name[MAX_LABEL_LENGTH]; int ip1[4], ip2[4], filter_level; uint32_t total, ok; int ret = -1; iconv_t ic; if (stream_open(&s, filename) < 0) { // do_log(LOG_ERR, "ERROR: Error opening %s as ASCII.", filename); return -2; } ic = iconv_open("UTF-8", charset); if (ic < 0) { do_log(LOG_ERR, "ERROR: Cannot initialize charset conversion: %s", strerror(errno)); goto err; } total = ok = 0; while (stream_getline(buf, MAX_LINE_LENGTH, &s)) { strip_crlf(buf); total++; // try 100 lines if none worked ("ok" didn't increment) then it isn't ascii if (ok == 0 && total > 100) { stream_close(&s); goto err; } memset(name, 0, sizeof(name)); // try the line as a p2p line if (sscanf(buf, "%299[^:]:%d.%d.%d.%d-%d.%d.%d.%d", name, &ip1[0], &ip1[1], &ip1[2], &ip1[3], &ip2[0], &ip2[1], &ip2[2], &ip2[3]) == 9) { blocklist_append(ip2int(ip1), ip2int(ip2), name, ic); ok++; } // else try the line as a ipfilter.dat line else if (sscanf(buf, "%d.%d.%d.%d %*[-,] %d.%d.%d.%d , %d , %299s", &ip1[0], &ip1[1], &ip1[2], &ip1[3], &ip2[0], &ip2[1], &ip2[2], &ip2[3], &filter_level, name) == 10){ // .DAT spec if 3rd entry on line is <=127, the IP is blocked else >=128 it is allowed. if ( filter_level <= 127 ) { blocklist_append(ip2int(ip1), ip2int(ip2), name, ic); ok++; } } // just a range if in LOWMEM else if (sscanf(buf, "%d.%d.%d.%d-%d.%d.%d.%d", &ip1[0], &ip1[1], &ip1[2], &ip1[3], &ip2[0], &ip2[1], &ip2[2], &ip2[3]) == 8){ name[0]='\0'; blocklist_append(ip2int(ip1), ip2int(ip2), name, ic); ok++; } // could add more tests for other ASCII formats here. // else the line is invalid else { do_log(LOG_INFO, "WARN: Invalid ASCII line: %s", buf); } } stream_close(&s); if (ok == 0) goto err; ret = 0; err: if (ic) iconv_close(ic); return ret; }