]> git.ayabusa.dev Git - thoryum.git/commitdiff
Added a download helper with libcurl
authorAyabusa <lebgpub@gmail.com>
Fri, 5 Jun 2026 13:56:15 +0000 (15:56 +0200)
committerAyabusa <lebgpub@gmail.com>
Fri, 5 Jun 2026 13:56:15 +0000 (15:56 +0200)
.gitignore
Makefile
README.txt
src/downloader.c [new file with mode: 0644]
src/downloader.h [new file with mode: 0644]
src/main.c

index d9b4f015d30ae4da6e3764d8db3381a41b95d4e0..96428863f52802023c8f7f0c4ab5534ef7dfa036 100644 (file)
@@ -1 +1,4 @@
 /build/*
+*.out
+*.bak
+*.BAK
index b43dc539746015235b247a60a6497b200392f1e3..700dab008917adb70d3bcc97d857454cd9dd9458 100644 (file)
--- 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
 
index 10c14ac7fed247a2f421588ce22233d33c1d687d..c5ec1baba78171d1e422692381eb911cf3f5750d 100644 (file)
@@ -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 (file)
index 0000000..0d48967
--- /dev/null
@@ -0,0 +1,45 @@
+#include <curl/curl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#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 (file)
index 0000000..331b340
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef DOWNLOADER_H
+#define DOWNLOADER_H
+
+#include <stdio.h>
+
+int DL_get_file(char *url, FILE *file);
+
+#endif
index 6bb056e3f6ecf6e9549f259008b9b91639784b29..276d4d128137a90ca8fe068d4ee114b1fbdc9883 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #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);
 }