#include "birch.h" #include "interface.h" /* content at url */ char *remote_buffer; int main(int argc, char *argv[]) { int i; char *remote_url; /* make sure we aren't running as root */ if (!geteuid()) { fprintf(stderr,"Don't run this as root, are you insane?!\n"); exit(EXIT_FAILURE); } /* make sure we were called with at least one argument */ if (argc < 2) { print_usage(argv[0]); exit(EXIT_FAILURE); } /* if we didn't exit, we have at least one argument */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-u") && (i+1) < argc && *argv[i+1] != '-') { remote_url = malloc(strlen(argv[i+1]) + 1); if (!remote_url) { fprintf(stderr,"Out of memory.\n"); exit(EXIT_FAILURE); } strcpy(remote_url, argv[i+1]); break; } } if (!remote_url) { print_usage(argv[0]); exit(EXIT_FAILURE); } if (!dispatch_reader(remote_url)) { /* at this point, remote_buffer is populated with the goods */ } free(remote_url); /* builds out the user interface & waits in gtk_main() */ interface_build(&argc, argv); exit(EXIT_SUCCESS); } int dispatch_reader(char *remote_url) { /* http://curl.haxx.se/lxr/source/docs/examples/sepheaders.c */ CURL *curl; CURLcode res; /* init the curl session */ curl = curl_easy_init(); /* set the url to get */ curl_easy_setopt(curl, CURLOPT_URL, remote_url); /* no progress meter please */ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); /* shut up completely */ curl_easy_setopt(curl, CURLOPT_MUTE, 1); /* send all data to this function */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dipatch_reader_callback); /* let's do it to it */ res = curl_easy_perform(curl); /* BEG: generate some statistics */ double con_tm_t, con_tm_c, con_xfer; curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &con_tm_t); curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &con_tm_c); curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &con_xfer); printf(" stats: connect[%2.3fs] total[%2.3fs] speed[%2.2fk]\n", con_tm_c, con_tm_t, con_xfer / 1024); /* END: generate some statistics */ /* always clean up */ curl_easy_cleanup(curl); /* return 0 on success, non-zero on failure */ return(res == CURLE_OK ? 0 : 1); } size_t dipatch_reader_callback( void *buffer, size_t size, size_t nmemb, void *stream) { /* can this be done with a callback instead of a global remote_buffer? */ remote_buffer = malloc(size * nmemb - 1); strcpy(remote_buffer, buffer); return(size * nmemb); } void print_usage(char *called_name) { printf("usage: %s -u \n", called_name); }