aboutsummaryrefslogtreecommitdiff
path: root/snag/pkg_db.c
diff options
context:
space:
mode:
authorRiley Beckett <rbeckettvt@gmail.com>2026-07-21 00:31:37 -0400
committerRiley Beckett <rbeckettvt@gmail.com>2026-07-21 00:31:37 -0400
commitd345e5eb933fbe84615f6050ce744423af1f55d8 (patch)
tree47ec06cad78795d29fa6f8670a62dc268a16cdc2 /snag/pkg_db.c
parentd598be66eddc0e925eee96e23d9f95c88cf1f6ed (diff)
remove popen because its not cool
Diffstat (limited to 'snag/pkg_db.c')
-rw-r--r--snag/pkg_db.c128
1 files changed, 80 insertions, 48 deletions
diff --git a/snag/pkg_db.c b/snag/pkg_db.c
index 26e972d..ee11288 100644
--- a/snag/pkg_db.c
+++ b/snag/pkg_db.c
@@ -4,26 +4,46 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/wait.h>
#define PKG_DB "../packages"
+#define PKG_DB_LEN (sizeof(PKG_DB) - 1)
#define PKG_INFO_CMD "./get_pkg_attrs.sh"
bool load_package_info(package_info_t* info, char* pkgid) {
+ memset(info, 0, sizeof(*info));
// construct package description directory path and ensure it exists
- size_t pkg_dir_len = strlen(PKG_DB) + strlen(pkgid) + 1;
+ size_t pkgid_len = strlen(pkgid);
+ size_t pkg_dir_len = PKG_DB_LEN + pkgid_len + 1;
char* pkg_dir = malloc(pkg_dir_len + 1);
- snprintf(pkg_dir, pkg_dir_len + 1, "%s/%s", PKG_DB, pkgid);
+ if (!pkg_dir) {
+ fprintf(stderr, "Failed to alloc pkg_dir\n");
+ exit(1);
+ }
+ // pkg_dir = "PKG_DB/pkgid"
+ memcpy(pkg_dir, PKG_DB, PKG_DB_LEN);
+ pkg_dir[PKG_DB_LEN] = '/';
+ memcpy(&pkg_dir[PKG_DB_LEN + 1], pkgid, pkgid_len + 1);
+
if (access(pkg_dir, F_OK) != 0) {
- fprintf(stderr, "Package '%s' not found in database\n", pkgid);
- free(pkg_dir);
- return false;
+ fprintf(stderr, "Package '%s' not found\n", pkgid);
+ free(pkg_dir);
+ return false;
}
// construct package description script path and ensure it exists
- size_t script_path_len = pkg_dir_len + strlen(pkgid) + 4;
+ size_t script_path_len = pkg_dir_len + pkgid_len + 4;
info->script_path = malloc(script_path_len + 1);
- snprintf(info->script_path, script_path_len + 1, "%s/%s.sh", pkg_dir, pkgid);
+ if (!info->script_path) {
+ fprintf(stderr, "Failed to alloc info->script_path\n");
+ exit(1);
+ }
+
+ memcpy(info->script_path, pkg_dir, pkg_dir_len);
+ info->script_path[pkg_dir_len] = '/';
+ memcpy(&info->script_path[pkg_dir_len + 1], pkgid, pkgid_len);
+ memcpy(&info->script_path[script_path_len - 3], ".sh", 4);
free(pkg_dir);
if (access(info->script_path, F_OK) != 0) {
fprintf(stderr, "Package '%s' broken, doesn't have description script\n", pkgid);
@@ -31,51 +51,63 @@ bool load_package_info(package_info_t* info, char* pkgid) {
return false;
}
- // construct get_pkg_attrs script cmd and invoke it
- size_t cmd_len = strlen(PKG_INFO_CMD) + script_path_len + 3;
- char* cmd = malloc(cmd_len + 1);
- snprintf(cmd, cmd_len + 1, "%s '%s'", PKG_INFO_CMD, info->script_path);
- FILE* getinfo_fp = popen(cmd, "r");
- free(cmd);
- if (getinfo_fp == NULL) {
- perror("failed to popen get_pkg_attrs");
- exit(1); // fatal error
+ int pipefds[2] = {0};
+ if (pipe(pipefds) < 0) {
+ fprintf(stderr, "Failed to create pipe\n");
+ exit(1);
+ }
+ pid_t pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "Failed to create child process\n");
+ exit(1);
+ }
+
+ if (pid == 0) {
+ close(pipefds[0]);
+ dup2(pipefds[1], STDOUT_FILENO);
+ close(pipefds[1]);
+
+ char* argv[] = { PKG_INFO_CMD, info->script_path, NULL };
+ char* envp[] = { NULL };
+ execve(PKG_INFO_CMD, argv, envp);
+ fprintf(stderr, "execve failed\n");
+ exit(127);
}
- // go through each NULL delimited section of the output (attributes)
- size_t n = 0;
- char* line = NULL;
- ssize_t read;
- while ((read = getdelim(&line, &n, '\0', getinfo_fp)) != -1) {
- // read in property based on first attribute type character
- char atype = line[0];
- char* avalue = line+1;
- switch (atype) {
- case 'i':
- // id
- info->id = strdup(avalue);
- break;
- case 'n':
- // name
- info->name = strdup(avalue);
- break;
- case 'd':
- // description
- info->desc = strdup(avalue);
- break;
- }
+ int wstatus;
+ if (waitpid(pid, &wstatus, 0) < 0) {
+ fprintf(stderr, "waitpid failed\n");
+ exit(1);
+ }
+ if (WEXITSTATUS(wstatus) == 127) {
+ exit(1);
}
- free(line);
- // close the get_pkg_attrs process
- int close_err = pclose(getinfo_fp);
- if (close_err == -1) {
- perror("failed to pclose get_pkg_attrs");
- exit(1); // fatal error
- } else if (close_err != 0) {
- fprintf(stderr, "failed to execute get_pkg_attrs: %d\n", close_err);
- exit(1); // fatal error
- }
+ ssize_t bytes_read = read(pipefds[0], info->package_attrs, sizeof(info->package_attrs));
+ if (bytes_read < 0) {
+ fprintf(stderr, "Failed to read from pipe\n");
+ exit(1);
+ }
+
+ free(info->script_path);
+
+ for (int i = 0; i < bytes_read;) {
+ char atype = info->package_attrs[i];
+ i++;
+ switch (atype) {
+ case 'i':
+ info->attrs.id = &info->package_attrs[i];
+ break;
+ case 'n':
+ info->attrs.name = &info->package_attrs[i];
+ break;
+ case 'd':
+ info->attrs.desc = &info->package_attrs[i];
+ break;
+ }
+ for(;info->package_attrs[i] && i < bytes_read; i++);
+ i++;
+ }
return true;
}