aboutsummaryrefslogtreecommitdiff
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
parentd598be66eddc0e925eee96e23d9f95c88cf1f6ed (diff)
remove popen because its not cool
-rw-r--r--docs/asdf.sh3
-rw-r--r--docs/info.txt12
-rw-r--r--snag/main.c3
-rw-r--r--snag/pkg_db.c128
-rw-r--r--snag/pkg_db.h11
-rw-r--r--snag/readme.txt2
6 files changed, 103 insertions, 56 deletions
diff --git a/docs/asdf.sh b/docs/asdf.sh
index 4eb9fe8..eab2c0f 100644
--- a/docs/asdf.sh
+++ b/docs/asdf.sh
@@ -1,7 +1,8 @@
#!/bin/bash
+set -e
. $FILE
-configure
+[ -z "$1" ] && configure
echo 1 > 3
build
echo 2 > 3
diff --git a/docs/info.txt b/docs/info.txt
index 1a75a38..51927d2 100644
--- a/docs/info.txt
+++ b/docs/info.txt
@@ -2,7 +2,7 @@
# bash
setting BASH_ENV sources it before
-execve("/bin/bash", { "/bin/bash", "build.sh", NULL }, { env..., "BASH_ENV=file", NULL});
+execve("/bin/bash", { "/bin/bash", "build.sh", NULL }, { env..., "BASH_ENV=file", , NULL});
build() {
@@ -17,6 +17,14 @@ check for $snagcache/$packagename/
if not exist full clean build
if exist then just run build and install
-
open log file - fd 3
splice(child stdout, fd 3)
+
+
+snag
+ - make
+ - bash
+ - automake
+ - bash
+ - swenu
+ - bash
diff --git a/snag/main.c b/snag/main.c
index 9f9fd44..c3fc47a 100644
--- a/snag/main.c
+++ b/snag/main.c
@@ -18,11 +18,12 @@ void subcmd_install(char* pkgs[], size_t count) {
for (int i = 0; i < count; ++i) {
char* pkgid = pkgs[i];
+ fprintf(stderr, "package: %s\n", pkgid);
// load package description
package_info_t info;
if (load_package_info(&info, pkgid)) {
- printf("Installing '%s'\n", info.id);
+ printf("Installing '%s'\n", info.attrs.id);
}
}
}
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;
}
diff --git a/snag/pkg_db.h b/snag/pkg_db.h
index e0f9a30..464380b 100644
--- a/snag/pkg_db.h
+++ b/snag/pkg_db.h
@@ -9,9 +9,14 @@
typedef struct {
char* id;
- char* name;
- char* desc;
- char* script_path;
+ char* name;
+ char* desc;
+} package_attrs_t;
+
+typedef struct {
+ char* script_path;
+ char package_attrs[4069];
+ package_attrs_t attrs;
} package_info_t;
bool load_package_info(package_info_t* info, char* pkgid);
diff --git a/snag/readme.txt b/snag/readme.txt
index ffc6041..1ce6b15 100644
--- a/snag/readme.txt
+++ b/snag/readme.txt
@@ -3,7 +3,7 @@
## Useful Subcommands
install/inst - install the specified package(s) (default)
remove - uninstall the specified package(s)
-upgrade/up - upgrade the package database
+update/up - update the package database
find/search - list packages whose metadata matches specified search string (flag to limit to installed)
info/desc - describe the metadata (and install data) of the specified package(s)
log - open your pager to the build log of the specified package