From 741e2058e95ba759b881ef93745ee1353b28b9d4 Mon Sep 17 00:00:00 2001 From: Ayabusa Date: Fri, 5 Jun 2026 15:56:15 +0200 Subject: [PATCH] Added a download helper with libcurl --- .gitignore | 3 +++ Makefile | 9 ++++++--- README.txt | 6 ++++++ src/downloader.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/downloader.h | 8 ++++++++ src/main.c | 5 +++++ 6 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/downloader.c create mode 100644 src/downloader.h diff --git a/.gitignore b/.gitignore index d9b4f01..9642886 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /build/* +*.out +*.bak +*.BAK diff --git a/Makefile b/Makefile index b43dc53..700dab0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -CFLAGS = -Wall -Wextra -fsanitize=address -g -SRC = src/main.c src/cpu.c -OBJ = build/main.o build/cpu.o +CFLAGS = -Wall -Wextra -fsanitize=address -g -lcurl +SRC = src/main.c src/cpu.c src/downloader.c +OBJ = build/main.o build/cpu.o build/downloader.o RES = build/Thoryum all: setup $(OBJ) build @@ -12,6 +12,9 @@ build/main.o: src/main.c build/cpu.o: src/cpu.c $(CC) $(CFLAGS) -c $^ -o $@ +build/downloader.o: src/downloader.c + $(CC) $(CFLAGS) -c $^ -o $@ + setup: mkdir -p build diff --git a/README.txt b/README.txt index 10c14ac..c5ec1ba 100644 --- a/README.txt +++ b/README.txt @@ -33,6 +33,12 @@ out of the box experience and an update wrapper. === This shit is fire ! How do I use it ? === += Install some stuff = + +First you'll need to install the following libs: +- Standart lib, should work with glibc or musl +- Libcurl, to download from da web + TODO : write some shit about using it diff --git a/src/downloader.c b/src/downloader.c new file mode 100644 index 0000000..0d48967 --- /dev/null +++ b/src/downloader.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include "downloader.h" + +// Well its probably usefull +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; +} + +// Ref : https://curl.se/libcurl/c/simple.html +// https://curl.se/libcurl/c/url2file.html +// returns 0 if good or 1 if that failed +int DL_get_file(char *url, FILE *file) { + CURL *curl; + + CURLcode result = curl_global_init(CURL_GLOBAL_ALL); + if(result != CURLE_OK) { + err(EXIT_FAILURE, "Failed to initialize CURL, Abort\n"); + return 1; + } + + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // To debug stuff + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // We want to see the progress + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); // func to write some shit + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // We tell libcurl to follow redirection + curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); + + result = curl_easy_perform(curl); + if(result != CURLE_OK) { + err(EXIT_FAILURE, "Failed to perform download : '%s', Abort\n", curl_easy_strerror(result)); + return 1; + } + + curl_easy_cleanup(curl); + } + curl_global_cleanup(); + return 0; +} diff --git a/src/downloader.h b/src/downloader.h new file mode 100644 index 0000000..331b340 --- /dev/null +++ b/src/downloader.h @@ -0,0 +1,8 @@ +#ifndef DOWNLOADER_H +#define DOWNLOADER_H + +#include + +int DL_get_file(char *url, FILE *file); + +#endif diff --git a/src/main.c b/src/main.c index 6bb056e..276d4d1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include "cpu.h" +#include "downloader.h" int main() { printf("Hello this is Thoryum ;)\n"); @@ -7,4 +8,8 @@ int main() { char opti_name[5] = {0}; CPU_opti_name(opti_name, opti); printf("You are using the CPU : '%s'\n", opti_name); + + FILE *file = fopen("page.out", "w"); + DL_get_file("https://ayabusa.dev", file); + fclose(file); } -- 2.43.0