diff --git a/Makefile b/Makefile index 2cbe37f..86a7721 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Define the compiler and flags CC = gcc -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra -std=c99 # Define the target executable and the source files TARGET = caur @@ -12,16 +12,12 @@ all: $(TARGET) # Link the object files into the target executable $(TARGET): $(OBJS) - $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) -lcurl # Compile the .c files into .o files -%.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ +src/%.o: src/%.c + $(CC) $(CFLAGS) -c $< -o $@ # Clean up the build artifacts clean: - rm -f $(TARGET) $(OBJS) - -# Run the program -run: $(TARGET) - ./$(TARGET) + rm -f $(TARGET) $(OBJS) diff --git a/caur b/caur index 707dfa6..a175484 100755 Binary files a/caur and b/caur differ diff --git a/download.o b/download.o new file mode 100644 index 0000000..284bca6 Binary files /dev/null and b/download.o differ diff --git a/main.o b/main.o new file mode 100644 index 0000000..95ee78b Binary files /dev/null and b/main.o differ diff --git a/src/caur.h b/src/caur.h new file mode 100644 index 0000000..150fb08 --- /dev/null +++ b/src/caur.h @@ -0,0 +1,16 @@ +#ifndef CAUR_H +#define CAUR_H + +#include +#include + +// Function to write data to file +size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream); + +// Function to download a package from the AUR +int download_package(const char *package_name); + +// Function to perform a system upgrade +int perform_system_upgrade(); + +#endif // CAUR_H diff --git a/src/download.c b/src/download.c new file mode 100644 index 0000000..77c1224 --- /dev/null +++ b/src/download.c @@ -0,0 +1,47 @@ +#include "caur.h" +#include + +#define AUR_PACKAGE_URL "https://aur.archlinux.org/cgit/aur.git/snapshot/" +#define AUR_PACKAGE_URL_END ".tar.gz" + +size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { + size_t written = fwrite(ptr, size, nmemb, stream); + return written; +} + +int download_package(const char *package_name) { + CURL *curl; + FILE *fp; + CURLcode res; + char url[256]; + char outfilename[256]; + + snprintf(url, sizeof(url), "%s%s%s", AUR_PACKAGE_URL, package_name, AUR_PACKAGE_URL_END); + snprintf(outfilename, sizeof(outfilename), "%s%s", package_name, AUR_PACKAGE_URL_END); + + curl = curl_easy_init(); + if (curl) { + fp = fopen(outfilename, "wb"); + if (fp == NULL) { + fprintf(stderr, "Failed to open file %s\n", outfilename); + return 1; + } + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + fclose(fp); + + if (res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + return 1; + } + } else { + fprintf(stderr, "Failed to init curl\n"); + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/src/download.o b/src/download.o new file mode 100644 index 0000000..284bca6 Binary files /dev/null and b/src/download.o differ diff --git a/src/main.c b/src/main.c index cdb2c0e..75e8a44 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ // Version: 0.1 -// caur - a simple command line utility for managing aur packages written in C hence the name lol +// date created 8-31-24 + +#include "caur.h" #include // For standard input/output operations #include // For memory allocation, process control, and conversions @@ -9,75 +11,11 @@ // #include // For access to POSIX operating system API, useful for things like file operations and process management // #include // Parsing packing information from the AUR packages -// #define AUR_URL "https://aur.archlinux.org/rpc/?v=5&type=info&arg[]=" -// #define AUR_SEARCH_URL "https://aur.archlinux.org/rpc/?v=5&type=search&arg=" -#define AUR_PACKAGE_URL "https://aur.archlinux.org/cgit/aur.git/snapshot/" -#define AUR_PACKAGE_URL_END ".tar.gz" - -// Function to write data to file -size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { - size_t written = fwrite(ptr, size, nmemb, stream); - return written; -} - -// Function to download a package from the AUR -int download_package(const char *package_name) { - CURL *curl; - FILE *fp; - CURLcode res; - char url[256]; - char outfilename[256]; - - // Construct the URL and output the file name - snprintf(url, sizeof(url), "%s%s%s", AUR_PACKAGE_URL, package_name, AUR_PACKAGE_URL_END); - snprintf(outfilename, sizeof(outfilename), "%s%s", package_name, AUR_PACKAGE_URL_END); - - curl = curl_easy_init(); - if (curl) { - fp = fopen(outfilename, "wb"); - if (fp == NULL) { - fprintf(stderr, "Failed to open file %s\n", outfilename); - return 1; - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - fclose(fp); - - if (res != CURLE_OK) { - fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); - return 1; - } - } else { - fprintf(stderr, "Failed to init curl\n"); - return 1; - } - - return 0; - -} - int main(int argc, char *argv[]) { - // Check if the user provided a package name as an argument, otherwise, run the system upgrade command + if (argc < 2) { printf("Performing system upgrade...\n"); - - // System upgrade command - int result = system("sudo pacman -Syu"); - - if (result == -1) { - fprintf(stderr, "Failed to execute system upgrade\n"); - return 1; - } else if (WIFEXITED(result) && WEXITSTATUS(result) != 0) { - fprintf(stderr, "System upgrade failed with exit status %d.\n", WEXITSTATUS(result)); - return 1; - } - - printf("System upgrade completed successfully.\n"); - return 0; + return perform_system_upgrade(); } // The package name is the first argument after the program name diff --git a/src/main.o b/src/main.o new file mode 100644 index 0000000..95ee78b Binary files /dev/null and b/src/main.o differ diff --git a/src/upgrade.c b/src/upgrade.c new file mode 100644 index 0000000..3ef0795 --- /dev/null +++ b/src/upgrade.c @@ -0,0 +1,19 @@ +#include "caur.h" + +#include +#include + +int perform_system_upgrade() { + int result = system("sudo pacman -Syu"); + + if (result == -1) { + fprintf(stderr, "Failed to execute system upgrade\n"); + return 1; + } else if (WIFEXITED(result) && WEXITSTATUS(result) != 0) { + fprintf(stderr, "System upgrade failed with exit status %d.\n", WEXITSTATUS(result)); + return 1; + } + + printf("System upgrade completed successfully.\n"); + return 0; +} \ No newline at end of file diff --git a/src/upgrade.o b/src/upgrade.o new file mode 100644 index 0000000..0543c57 Binary files /dev/null and b/src/upgrade.o differ diff --git a/upgrade.o b/upgrade.o new file mode 100644 index 0000000..0543c57 Binary files /dev/null and b/upgrade.o differ