-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector.c
executable file
·50 lines (39 loc) · 1.24 KB
/
connector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#define MYFILE "tmp.txt"
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr,size,nmemb,stream);
return written;
}
/*!
*This function download the page that is related to the url passed in argument.
*
*\param url
* Url of the web page that has to be downloaded.
*\return The pointer of the file created. The file 'tmp.txt' will contains the html of the web page.
*/
FILE * downloadFromUrl(char* url) {
CURL *curl;
CURLcode res;
FILE *fp;
char outfilename[FILENAME_MAX] = MYFILE;
fp = fopen(outfilename,"wb");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
fclose(fp);
if(res == 0) {
printf("%s\n", "Website downloaded correctly.");
} else {
printf("\n%s '%s'", "Failure in downloading website at url",url);
}
curl_easy_cleanup(curl);
}
return fp;
}