first commit upgrades system, downloads package

This commit is contained in:
Tristan Smith 2024-08-31 01:17:14 -04:00
commit 9eebe5b301
No known key found for this signature in database
GPG key ID: 0858A9B022DE8ECE
4 changed files with 142 additions and 0 deletions

27
Makefile Normal file
View file

@ -0,0 +1,27 @@
# Define the compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -Werror
# Define the target executable and the source files
TARGET = caur
SRCS = $(wildcard src/*.c)
OBJS = $(SRCS:.c=.o)
# The default rule: build the target
all: $(TARGET)
# Link the object files into the target executable
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
# Compile the .c files into .o files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Clean up the build artifacts
clean:
rm -f $(TARGET) $(OBJS)
# Run the program
run: $(TARGET)
./$(TARGET)

BIN
caur Executable file

Binary file not shown.

14
readme.md Normal file
View file

@ -0,0 +1,14 @@
## caur
A simple command line utility for managing AUR packages written in C hence the name
Of course, Arch Linux needs another AUR helper, right? No, of course not. But that's not why I'm building mine. I'm building mine because I suck at programming and want to learn. If you use it, great. If not, also great. I'll probably keep using yay.
It currently has a Makefile, but I stole it. Dunno if it'll be needed yet. Ignore it for now.
### Depends:
curl
### 8-31-24
Version 0.1 - compiles with ```gcc -o caur src/main.c```, currently will run ```sudo pacman -Syu``` just running, ```./caur```. Currently will *only* download the archive of the package when you specify a package name.

101
src/main.c Normal file
View file

@ -0,0 +1,101 @@
// Version: 0.1
// caur - a simple command line utility for managing aur packages written in C hence the name lol
#include <stdio.h> // For standard input/output operations
#include <stdlib.h> // For memory allocation, process control, and conversions
#include <string.h> // For handling strings and memory blocks
#include <curl/curl.h> // This is for handling HTTP requests, which youll need to fetch data from the AUR
// #include <unistd.h> // For access to POSIX operating system API, useful for things like file operations and process management
// #include <json-c/json.h> // 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;
}
// The package name is the first argument after the program name
const char *package_name = argv[1];
// Call download function with provided package name
if (download_package(package_name) == 0) {
printf("Package %s downloaded successfully\n", package_name);
} else {
printf("Failed to download package %s\n", package_name);
}
return 0;
/* const char *package_name = "neofetch";
if (download_package(package_name) == 0) {
printf("Package downloaded successfully\n");
} else {
printf("Failed to download package");
} */
}