aboutsummaryrefslogtreecommitdiff
path: root/src/download.c
blob: bb8365ce6f389481de056c2f3995da8633f695e8 (plain)
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
#include "curl/curl.h"
#include "stdio.h"

static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream) {
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);

  return written;
}

int pow_download_file(char *path, char *url) {
  FILE *pagefile;
  CURL *curl = curl_easy_init();

  if (!curl) {
    printf("failed to init curl\n");

    return 1;
  }

  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);

  pagefile = fopen(path, "wb");

  if (!pagefile) {
    curl_easy_cleanup(curl);

    return 1;
  }

  curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);

  int result = curl_easy_perform(curl);

  fclose(pagefile);

  return result;
}