Ticket #79: autopackage-curl.c

File autopackage-curl.c, 0.8 kB (added by Jan-Nik, 5 months ago)

Small program using libcurl, binary is 5.7 K

Line 
1 #include <curl/curl.h>
2
3 int main(int argc, char** argv)
4 {
5         if(argc != 3)
6         {
7                 printf("Usage: autopackage-curl <url> <newfile>\n");
8                 return 1;
9         }
10         if(curl_global_init(CURL_GLOBAL_ALL) != 0)
11         {
12                 printf("Error: curl_global_init returned non-zero.\n");
13                 return 1;
14         }
15         CURL* easyhandle = curl_easy_init();
16         if(easyhandle == NULL)
17         {
18                 printf("Error: curl_easy_init returned NULL.\n");
19                 return 1;
20         }
21         FILE* file = fopen(argv[2], "wb");
22         if(file == NULL)
23         {
24                 printf("Error creating %s.\n", argv[2]);
25                 return 1;
26         }
27         curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, file);
28         curl_easy_setopt(easyhandle, CURLOPT_URL, argv[1]);
29         curl_easy_setopt(easyhandle, CURLOPT_FAILONERROR, 1);
30         CURLcode success = curl_easy_perform(easyhandle);
31         fclose(file);
32         if(success != 0)
33         {
34                 printf("Error downloading %s.\n", argv[1]);
35                 return 1;
36         }
37         return 0;
38 }