Skip to content

Instantly share code, notes, and snippets.

@atr000
Created April 1, 2010 06:00
Show Gist options
  • Save atr000/351445 to your computer and use it in GitHub Desktop.
Save atr000/351445 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <curl/curl.h>
#include <string.h>
/* Flag set by .--verbose.. */
static int verbose_flag;
int
main (argc, argv)
int argc;
char **argv;
{
int c;
char *url = "http://ipinfodb.com/ip_query2.php?ip=";
char *ip = "";
char *output = "&output=";
char *outputtype;
char *fullurl;
while (1)
{
static struct option long_options[] =
{
{"ip",required_argument, 0, 'i'},
{"output",required_argument, 0, 'o'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "i:o:",long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'i':
ip = optarg;
break;
case 'o':
outputtype = optarg;
break;
default:
printf(
"Usage: getiploc [OPTION]... VALUE...\n\n"
"-i IP or domain\n"
"-o Output type. Either xml,json or raw\n");
exit(0);
}
}
fullurl = (char *)malloc((strlen(url) + strlen(output) + strlen(ip) + strlen(outputtype) + 1)*sizeof(char));
strcpy(fullurl, url);
strcat(fullurl, ip);
strcat(fullurl, output);
strcat(fullurl, outputtype);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if((curl)&&(ip != "")) {
curl_easy_setopt(curl, CURLOPT_URL, fullurl);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}else{
printf(
"Usage: getiploc [OPTION]... VALUE...\n\n"
"-i IP or domain\n"
"-o Output type. Either xml,json or raw\n");
exit(0);
}
/* Print any remaining command line arguments (not options). */
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
putchar ('\n');
}
exit (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment