From 8f5dc1930debe95e106595da33951bd870672d85 Mon Sep 17 00:00:00 2001 From: forever2k38 Date: Mon, 31 Aug 2026 10:19:12 +0200 Subject: feat: find package by name --- src/args.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/args.h | 5 ++++- src/config.c | 2 -- src/main.c | 8 ++++++++ src/package.c | 0 src/package.h | 9 +++++++++ src/repo.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/repo.h | 10 ++++++++++ src/utils.c | 6 ++++++ src/utils.h | 3 +++ 10 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 src/package.c create mode 100644 src/package.h create mode 100644 src/utils.c create mode 100644 src/utils.h (limited to 'src') diff --git a/src/args.c b/src/args.c index b353579..d5ea57e 100644 --- a/src/args.c +++ b/src/args.c @@ -3,6 +3,7 @@ #include "stdbool.h" #include "stdio.h" #include "string.h" +#include "utils.h" char *help = "▄▄▄▄▄▄▄▄▄▄▄ ▄\n" "█ █ █ ▄ █\n" @@ -22,6 +23,7 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { int flags = 0; bool next_flag_config; + bool next_flag_package; for (int i = 0; i < argc; i++) { char *arg = argv[i]; @@ -40,6 +42,20 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { continue; } + if (next_flag_package) { + if (!strncmp(arg, "-", sizeof("-") - 1)) { + printf("Missing package argument\n"); + + return 1; + } + + args->package = arg; + + next_flag_package = false; + + continue; + } + if (strcmp(arg, "--help") == 0) { flags |= POW_FLAG_HELP; @@ -51,6 +67,36 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { continue; } + + if (string_has_prefix("-", arg)) { + size_t len = strlen(arg); + + for (size_t j = 0; j < len; j++) { + char c = (char)arg[j]; + + switch (c) { + case 'S': { + if (flags & POW_FLAG_SYNC) { + printf("flag double defined, skipping\n"); + } + + flags |= POW_FLAG_SYNC; + next_flag_package = true; + }; + case 'Q': { + if (flags & POW_FLAG_QUERY) { + printf("flag double defined, skipping\n"); + + flags |= POW_FLAG_QUERY; + + next_flag_package = true; + } + } + } + } + + continue; + } } if (next_flag_config) { diff --git a/src/args.h b/src/args.h index 79ea308..578eeb7 100644 --- a/src/args.h +++ b/src/args.h @@ -2,12 +2,15 @@ #define DEFAULT_CONFIG_PATH "/etc/pow/opts.lua" -#define POW_FLAG_HELP 1 +#define POW_FLAG_HELP 0x01 +#define POW_FLAG_SYNC 0x02 +#define POW_FLAG_QUERY 0x04 typedef struct { int flags; char *cfg_path; + char *package; } pow_args; int pow_args_parse(int argc, char *argv[], pow_args *args); diff --git a/src/config.c b/src/config.c index dd44129..d82cf9f 100644 --- a/src/config.c +++ b/src/config.c @@ -80,8 +80,6 @@ int pow_open_config(pow_config *cfg, char *path) { } repo->path = path_dup; - - printf("%s\n", path); } else { printf("invalid (or non existent) path for repo\n"); diff --git a/src/main.c b/src/main.c index abc3ffc..64cb537 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,8 @@ #include "args.h" #include "config.h" +#include "repo.h" + +#include "stdio.h" int main(int argc, char *argv[]) { pow_args args = {0}; @@ -15,6 +18,11 @@ int main(int argc, char *argv[]) { pow_config config = {0}; pow_open_config(&config, args.cfg_path); + + if (args.flags & POW_FLAG_QUERY) { + repo_check_package(config.repos, config.repos_count, args.package); + } + pow_close_config(&config); return 0; diff --git a/src/package.c b/src/package.c new file mode 100644 index 0000000..e69de29 diff --git a/src/package.h b/src/package.h new file mode 100644 index 0000000..c555428 --- /dev/null +++ b/src/package.h @@ -0,0 +1,9 @@ +#include "stddef.h" + +typedef struct { + const char *name; + const char *version; + const char *description; + + size_t *arch; +} pow_package; diff --git a/src/repo.c b/src/repo.c index e69de29..9238a27 100644 --- a/src/repo.c +++ b/src/repo.c @@ -0,0 +1,56 @@ +#define _XOPEN_SOURCE 700 + +#include "repo.h" + +#include "dirent.h" +#include "limits.h" +#include "malloc.h" +#include "stdio.h" + +int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) { + if (!repos) { + printf("no repos defined in your config, please define them\n"); + + return 1; + } + + DIR *dp; + struct dirent *ep; + + for (size_t i = 0; i < repo_count; i++) { + char *package_path = malloc(PATH_MAX); + + if (!package_path) { + printf("failed to allocate package_path, sizeof %zu\n", PATH_MAX); + + return 1; + } + + int written = + snprintf(package_path, PATH_MAX, "%s/%s", repos[i]->path, query); + + if (written >= PATH_MAX) { + printf("something went seriously wrong. the path for the package %s is " + "too long?\n", + query); + + free(package_path); + + return 1; + } + + dp = opendir(package_path); + + free(package_path); + + if (!dp) { + continue; + } + + printf("found package %s in repo %s\n", query, repos[i]->path); + + closedir(dp); + } + + return 0; +} diff --git a/src/repo.h b/src/repo.h index be04b3c..958210d 100644 --- a/src/repo.h +++ b/src/repo.h @@ -1,5 +1,15 @@ +#ifndef _POW_REPO_H +#define _POW_REPO_H + #include "stddef.h" +#define POW_X86_64 1 +#define POW_ARM 2 + typedef struct { const char *path; } pow_repo; + +int repo_check_package(pow_repo **repos, size_t repo_count, const char *query); + +#endif diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..19dc193 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,6 @@ +#include "utils.h" +#include "string.h" + +bool string_has_prefix(const char *pre, const char *str) { + return strncmp(pre, str, strlen(pre)) == 0; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..1c5163b --- /dev/null +++ b/src/utils.h @@ -0,0 +1,3 @@ +#include "stdbool.h" + +bool string_has_prefix(const char *pre, const char *str); -- cgit v1.3