refactor for modularity
This commit is contained in:
parent
b4e63cfecf
commit
09af0b7366
12 changed files with 92 additions and 76 deletions
14
Makefile
14
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)
|
||||
|
|
BIN
caur
BIN
caur
Binary file not shown.
BIN
download.o
Normal file
BIN
download.o
Normal file
Binary file not shown.
BIN
main.o
Normal file
BIN
main.o
Normal file
Binary file not shown.
16
src/caur.h
Normal file
16
src/caur.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef CAUR_H
|
||||
#define CAUR_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
// 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
|
47
src/download.c
Normal file
47
src/download.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "caur.h"
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
BIN
src/download.o
Normal file
BIN
src/download.o
Normal file
Binary file not shown.
72
src/main.c
72
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 <stdio.h> // For standard input/output operations
|
||||
#include <stdlib.h> // For memory allocation, process control, and conversions
|
||||
|
@ -9,75 +11,11 @@
|
|||
// #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;
|
||||
return perform_system_upgrade();
|
||||
}
|
||||
|
||||
// The package name is the first argument after the program name
|
||||
|
|
BIN
src/main.o
Normal file
BIN
src/main.o
Normal file
Binary file not shown.
19
src/upgrade.c
Normal file
19
src/upgrade.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "caur.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
src/upgrade.o
Normal file
BIN
src/upgrade.o
Normal file
Binary file not shown.
BIN
upgrade.o
Normal file
BIN
upgrade.o
Normal file
Binary file not shown.
Loading…
Reference in a new issue