All pastes #626293 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-07-19 15:37 UTC
rendered paste body
#include <stdio.h>
#include <string.h>

#define IPLEN 3+1+3+1+3+1+3

void num2ip(int num, char *buf)
{
    int s1,s2,s3,s4;

    s4 = (num & 0x000000ff);
    s3 = (num & 0x0000ff00) >> 8;
    s2 = (num & 0x00ff0000) >> 16;
    s1 = (num & 0xff000000) >> 24;

    sprintf(buf, "%d.%d.%d.%d", s1, s2, s3, s4);
}

int load_file(const char* filename)
{
    FILE* fp = fopen(filename, "r");
    char buf[512+1];

    if(!fp)
    {
        printf("%s: file not found\n", filename);
        return -1;
    }

    while(fgets(buf, sizeof buf - 1, fp)    )
    {
        char* num = strtok(buf, " ");

        num = strtok(NULL, " ");

        if(num)
        {
            char ip[IPLEN+1];
            num2ip(atoi(num), ip);
            printf("%s\n", ip);
        }
    }

    fclose(fp);

    return 0;
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        printf("Usage: %s number\n", argv[0]);
        return -1;
    }

    int num = atoi(argv[1]);

    if(num)
    {
        char ip[IPLEN+1];
        num2ip(num, ip);
        printf("%s\n", ip);
    }
    else
        return load_file(argv[1]);

    return 0;

}