-CFLAGS = -Wall -Wextra -fsanitize=address -g -lcurl
+CFLAGS = -Wall -Wextra -g
+LIBS = -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
- $(CC) $(CFLAGS) $(OBJ) -o $(RES)
+ $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS)
build/main.o: src/main.c
- $(CC) $(CFLAGS) -c $^ -o $@
+ $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
build/cpu.o: src/cpu.c
- $(CC) $(CFLAGS) -c $^ -o $@
+ $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
build/downloader.o: src/downloader.c
- $(CC) $(CFLAGS) -c $^ -o $@
+ $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
setup:
mkdir -p build
+run:
+ ./$(RES)
+
+debug:
+ gdb -tui $(RES)
+
clean:
$(RM) $(OBJ) $(RES)
+#include <stddef.h>
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <err.h>
#include "downloader.h"
+
+#define THORIUM_RELEASE_API_URL "https://api.github.com/repos/Alex313031/thorium/releases/latest"
+
// Well its probably usefull
-static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
+static size_t write_file(char *ptr, size_t size, size_t nmemb, void **stream)
{
- size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
+ size_t written = fwrite(ptr, size, nmemb, (FILE *)*stream);
return written;
}
+size_t ver_str_size = 1; // Malloced size
+size_t ver_str_len = 0; // Actual length
+static size_t write_char(char *ptr, size_t size, size_t nmemb, void **stream) {
+ printf("s = %zu, l = %zu\n", ver_str_size, ver_str_len);
+ size_t old_len = ver_str_len;
+ ver_str_len += size * nmemb;
+ printf("l++ = %zu\n", ver_str_len);
+ if (ver_str_size < ver_str_len) {
+ while (ver_str_size < ver_str_len)
+ ver_str_size = ver_str_size * 2 + 1;
+ printf("s++ = %zu\n", ver_str_size);
+ *stream = realloc(*stream, ver_str_size);
+ }
+ memcpy(*stream + old_len, ptr, size*nmemb);
+ return nmemb;
+}
+
// 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) {
+int DL_get_file(char *url, struct DL_stream *stream) {
CURL *curl;
-
+
CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
if(result != CURLE_OK) {
err(EXIT_FAILURE, "Failed to initialize CURL, Abort\n");
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);
+ curl_easy_setopt(curl, CURLOPT_USERAGENT, "thoryum/1.0");
+ if (stream->type == FILE_STREAM)
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); // func to write some shit
+ else
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_char); // other func to write some shit
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(stream->stream));
result = curl_easy_perform(curl);
if(result != CURLE_OK) {
curl_global_cleanup();
return 0;
}
+
+char *DL_get_latest_version() {
+ struct DL_stream buf = {.stream = NULL, .type = CHAR_STREAM};
+ //set some globals
+ ver_str_size = 1;
+ ver_str_len = 0;
+ DL_get_file(THORIUM_RELEASE_API_URL, &buf);
+ printf("Received :\n%s\n", (char *)buf.stream);
+ return NULL;
+}