aboutsummaryrefslogtreecommitdiff
path: root/snag
diff options
context:
space:
mode:
authorJack Jamison <jackqjamison@gmail.com>2026-07-21 09:17:56 -0400
committerJack Jamison <jackqjamison@gmail.com>2026-07-21 09:17:56 -0400
commitea598e719d557a876a52d0cbecb21c1fb8ff917f (patch)
treef81bd793d686e2a6c7784e25c7118f351d3be7a7 /snag
parent013a332a5ad21b4faea1644a6ad38b144ef22901 (diff)
add a bunch of util and clean up
Diffstat (limited to 'snag')
-rw-r--r--snag/main.c7
-rw-r--r--snag/pkg_db.c100
-rw-r--r--snag/util.c55
-rw-r--r--snag/util.h18
4 files changed, 108 insertions, 72 deletions
diff --git a/snag/main.c b/snag/main.c
index eff1f2f..9804f99 100644
--- a/snag/main.c
+++ b/snag/main.c
@@ -1,16 +1,11 @@
#include <stdio.h>
#include "pkg_db.h"
-#include "util.h"
void subcmd_install(char* pkgs[], size_t count);
int main(int argc, char* argv[]) {
- char buf[4096];
- vstrcat(buf, "this ", "is", " a", " te", "st");
- fprintf(stderr, "buf: %s\n", buf);
-
// run install subcommand with each argument as a package
subcmd_install(argv+1, argc-1);
@@ -28,7 +23,7 @@ void subcmd_install(char* pkgs[], size_t count) {
// load package description
package_info_t info;
if (load_package_info(&info, pkgid)) {
- printf("Installing '%s'\n", info.attrs.id);
+ printf("Installing %s - %s\n", info.attrs.name, info.attrs.desc);
}
}
}
diff --git a/snag/pkg_db.c b/snag/pkg_db.c
index ee11288..ad9eff3 100644
--- a/snag/pkg_db.c
+++ b/snag/pkg_db.c
@@ -1,9 +1,11 @@
#include "pkg_db.h"
+#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <sys/wait.h>
#define PKG_DB "../packages"
@@ -11,86 +13,72 @@
#define PKG_INFO_CMD "./get_pkg_attrs.sh"
bool load_package_info(package_info_t* info, char* pkgid) {
+
memset(info, 0, sizeof(*info));
+ size_t pkgid_len = strlen(pkgid);
// construct package description directory path and ensure it exists
- 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);
- 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\n", pkgid);
- free(pkg_dir);
- return false;
- }
+ size_t pkg_dir_len = PKG_DB_LEN + pkgid_len + 1;
+ char* pkg_dir = emalloc(pkg_dir_len + 1, "pkg_dir");
+ {
+ memcpy(pkg_dir, PKG_DB, PKG_DB_LEN);
+ pkg_dir[PKG_DB_LEN] = '/';
+ memcpy(pkg_dir + PKG_DB_LEN + 1, pkgid, pkgid_len);
+ pkg_dir[pkg_dir_len] = '\0';
+ if (access(pkg_dir, F_OK) != 0) {
+ eprintf("Package '%s' not found\n", pkgid);
+ free(pkg_dir);
+ return false;
+ }
+ }
// construct package description script path and ensure it exists
+ // script_path = "pkg_dir/pkgid.sh"
size_t script_path_len = pkg_dir_len + pkgid_len + 4;
- info->script_path = malloc(script_path_len + 1);
- 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);
- free(info->script_path);
- return false;
- }
+ info->script_path = emalloc(script_path_len + 1, "info->script_path");
+ {
+ 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) {
+ eprintf("Package '%s' broken, doesn't have description script\n", pkgid);
+ free(info->script_path);
+ return false;
+ }
+ }
+ // PIPE and FORK to run get pkg attrs script
int pipefds[2] = {0};
- if (pipe(pipefds) < 0) {
- fprintf(stderr, "Failed to create pipe\n");
- exit(1);
- }
+ if (pipe(pipefds) < 0) error_out("failed to pipe: %s", strerror(errno));
pid_t pid = fork();
- if (pid < 0) {
- fprintf(stderr, "Failed to create child process\n");
- exit(1);
- }
+ if (pid < 0) error_out("failed to fork: %s", strerror(errno));
+ // child proc
if (pid == 0) {
+ // write my stdout the the pipe
close(pipefds[0]);
dup2(pipefds[1], STDOUT_FILENO);
close(pipefds[1]);
-
+ // exec into get attr script
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);
+ error_out("execve failed");
}
+ // wait for child to finish
int wstatus;
- if (waitpid(pid, &wstatus, 0) < 0) {
- fprintf(stderr, "waitpid failed\n");
- exit(1);
- }
- if (WEXITSTATUS(wstatus) == 127) {
- exit(1);
- }
+ if (waitpid(pid, &wstatus, 0) < 0) error_out("waitpid failed: %s", strerror(errno));
+ if (WEXITSTATUS(wstatus) != 0) error_out("failed to execute get pkg attr script");
+ // read all bytes from pipe
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);
+ if (bytes_read < 0) error_out("Failed to read from pipe");
+ // read attributes from pipe data
for (int i = 0; i < bytes_read;) {
char atype = info->package_attrs[i];
i++;
diff --git a/snag/util.c b/snag/util.c
index c4137f6..d8947a7 100644
--- a/snag/util.c
+++ b/snag/util.c
@@ -1,15 +1,58 @@
-#include <stdarg.h>
+#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
+
+#include "util.h"
+
+void eprintf(const char *format, ...) {
+ // send that shi to stderr
+ va_list args;
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+}
+
+void error_out(const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ exit(1);
+}
+
+void* emalloc(size_t size, char* alloc_reason) {
+ void* d = malloc(size);
+ if (d == NULL) error_out("Failed to allocate %s", alloc_reason);
+ return d;
+}
+
+char* vastrcat_(int dummy, ...) {
-void vstrcat_(char* dest, ...) {
va_list args;
- va_start(args, dest);
+ va_list args2;
+ va_start(args, dummy);
+ va_copy(args2, args);
+
+ // get len of output string
+ size_t len = 0;
while (1) {
char* arg = va_arg(args, char*);
if (!arg) break;
- memcpy(dest, arg, strlen(arg));
- dest += strlen(arg);
+ len += strlen(arg);
}
va_end(args);
- *dest = 0;
+
+ // allocate and build output
+ char* output = malloc(len + 1);
+ char* p = output;
+ while (1) {
+ char* arg = va_arg(args2, char*);
+ if (!arg) break;
+ memcpy(p, arg, strlen(arg));
+ p += strlen(arg);
+ }
+ va_end(args2);
+
+ return output;
}
diff --git a/snag/util.h b/snag/util.h
index 88906cc..6f23607 100644
--- a/snag/util.h
+++ b/snag/util.h
@@ -1,6 +1,16 @@
#ifndef UTIL_H
#define UTIL_H
-#include <stdlib.h>
-#define vstrcat(dest, ...) vstrcat_(dest, __VA_ARGS__, NULL)
-void vstrcat_(char* dest, ...);
-#endif // UTIL_H
+
+#include <stdarg.h>
+#include <stddef.h>
+
+// utility for errors
+void eprintf(const char *format, ...) __attribute__((format(printf, 1, 2)));
+void error_out(const char *format, ...) __attribute__((format(printf, 1, 2))); // panic, as the kids say
+void* emalloc(size_t size, char* alloc_reason);
+
+// string functions
+char* vastrcat_(int dummy, ...);
+#define vastrcat(...) vastrcat_(0, __VA_ARGS__, NULL)
+
+#endif