#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>

size_t write_function(char *buffer,size_t size, size_t nitems, void *userdata);

int
main(void)
{
	CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if(!curl)
	{
		fprintf(stderr, "Curl init failed!");
		return 1;
	}
	curl_easy_setopt(curl, CURLOPT_URL , "https://moodle.herzen.spb.ru/pluginfile.php/1671864/mod_resource/content/1/%D0%9B%D0%B0%D0%B1%D0%BE%D1%80%D0%B0%D1%82%D0%BE%D1%80%D0%BD%D0%B0%D1%8F%20%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%207%20-%20libcurl.pdf");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
	curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
	res = curl_easy_perform(curl);
	
	if(res != CURLE_OK)
		fprintf(stderr, "curl failed with %s\n", curl_easy_strerror(res));
	else
		fprintf(stdout, "curl succeded with code %d\n", res);

	curl_easy_cleanup(curl);
	return 0;
}

size_t
write_function(char *buffer, size_t size, size_t nitems, void * stream)
{
	if(stream != NULL)
		fwrite(buffer, size, nitems, stream);
	return size*nitems;
}
