shokara's own minecraft launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 

83 lines
1.5 KiB

#include <stdio.h>
#include <curl/curl.h>
#include "fetch.h"
static CURL *curl;
static int fetch_init(void);
static void fetch_cleanup(void);
static size_t fetch_file_cb(char *ptr, size_t size, size_t nmemb, FILE *stream);
static int fetch_init(void)
{
curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "curl failed to initialize\n");
return -1;
}
return 0;
}
static void fetch_cleanup(void)
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
static size_t fetch_file_cb(char *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int fetch_file(char *url, char *filename, int overwrite)
{
FILE *fp;
CURLcode res;
int status;
size_t flen;
status = 0;
if (fetch_init() < 0)
return -1;
if ((fp = fopen(filename, "wb")) == NULL) {
perror("fopen failed");
status = -1;
goto fopen_err;
}
fseek(fp, 0L, SEEK_END);
flen = ftell(fp);
rewind(fp);
if (flen > 0 && overwrite == 0) {
fprintf(stderr, "not overwriting %s as it already exists\n",
filename);
goto end;
} else if (flen > 0 && overwrite != 0) {
printf("overwriting %s\n", filename);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fetch_file_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "%s failed to download to %s (curl error %i)\n",
url, filename, res);
status = -2;
goto end;
}
end:
fetch_cleanup();
fopen_err:
fclose(fp);
return status;
}