ckiss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7cb52282857d211a31c3334c0b864da0e9198078
parent 26818e36896bd1497268fa2e3e663cad244c9819
Author: aabacchus <ben@bvnf.space>
Date:   Fri, 21 Apr 2023 18:59:05 +0100

add list

Diffstat:
MREADME | 2+-
Msrc/.gitignore | 2++
Msrc/Makefile | 20++++++++++++++++----
Msrc/kiss.h | 13+++++++++++--
Msrc/libkiss.c | 6+++++-
Asrc/list.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main.c | 26++++++++++++++++++++++++++
7 files changed, 110 insertions(+), 8 deletions(-)

diff --git a/README b/README @@ -9,7 +9,7 @@ An implementation of the kiss package manager in C. [ ] checksum [ ] download [ ] install -[ ] list +[.] list [ ] remove [ ] search [ ] update diff --git a/src/.gitignore b/src/.gitignore @@ -1,3 +1,5 @@ *.a *.o test +kiss +kiss-test diff --git a/src/Makefile b/src/Makefile @@ -1,6 +1,13 @@ .POSIX: -XCFLAGS = $(CFLAGS) -Wall -Wextra -pedantic -D_XOPEN_SOURCE=700 -Og -g +XCFLAGS = $(CFLAGS) -Wall -Wextra -Wshadow -pedantic -D_XOPEN_SOURCE=700 -Og -g + +OBJS = main.o list.o + +all: kiss + +kiss: $(OBJS) libkiss.a + $(CC) $(LDFLAGS) $(OBJS) libkiss.a -o $@ libkiss.a: libkiss.o $(AR) -rcs $@ libkiss.o @@ -8,13 +15,18 @@ libkiss.a: libkiss.o .c.o: $(CC) $(XCFLAGS) -c $< -o $@ -test: test.o libkiss.a +test: kiss-test + ./kiss-test + +kiss-test: test.o libkiss.a $(CC) $(LDFLAGS) test.o libkiss.a -o $@ clean: - rm -f libkiss.a libkiss.o test.o test + rm -f libkiss.a libkiss.o test.o test $(OBJS) kiss kiss-test -.PHONY: clean +.PHONY: clean test all libkiss.o: kiss.h test.o: kiss.h +list.o: kiss.h +main.o: kiss.h diff --git a/src/kiss.h b/src/kiss.h @@ -1,6 +1,11 @@ #include <limits.h> #include <stdbool.h> #include <unistd.h> +#include <stdnoreturn.h> + +#ifndef noreturn +#define noreturn +#endif struct cmd { char **args; @@ -23,6 +28,7 @@ struct env { char *pwd; char *root; char *su; + char *sys_db; char *tmpdir; char date[17]; /* YYYY-MM-DD-HH:MM + '\0' */ }; @@ -30,8 +36,8 @@ struct env { /* called "mylog" to avoid collision with math.h log function. */ void mylog(const char *s); void warn(const char *s); -void die(const char *s); -void die_perror(const char *s); +noreturn void die(const char *s); +noreturn void die_perror(const char *s); /* returns a string containing the concatenation of all args. Args must be * terminated with a NULL. Returned string must be freed by caller.*/ @@ -55,3 +61,6 @@ struct env *setup_env(void); /* correctly frees a struct env. */ void destroy_env(struct env *e); + + +int list(int argc, char **argv, struct env *e); diff --git a/src/libkiss.c b/src/libkiss.c @@ -160,7 +160,10 @@ setup_env(void) { if (t == NULL) die("PWD is not set"); e->pwd = t; - e->root = getenv("KISS_ROOT"); + t = getenv("KISS_ROOT"); + e->root = t ? t : ""; + + e->sys_db = concat(e->root, "/var/db/kiss/installed", NULL); time_t dt = time(NULL); struct tm *tm = localtime(&dt); @@ -291,6 +294,7 @@ destroy_env(struct env *e) { free(e->path[i]); free(e->path); + free(e->sys_db); free(e->pid); free(e); } diff --git a/src/list.c b/src/list.c @@ -0,0 +1,49 @@ +#include <dirent.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "kiss.h" + +int +list(int argc, char **argv, struct env *e) { + struct dirent *dp; + + if (argc == 1) { + DIR *d = opendir(e->sys_db); + if (d == NULL) die_perror(e->sys_db); + do { + errno = 0; + dp = readdir(d); + if (dp == NULL) { + if (errno == 0) { + /* end of dir */ + goto done; + } + closedir(d); + die_perror("readdir"); + } + if (dp->d_name[0] == '.') + continue; + + char *p = concat(e->sys_db, "/", dp->d_name, "/version", NULL); + + char *buf = NULL; + size_t bufn = 0; + FILE *f = fopen(p, "r"); + if (f == NULL) die_perror(p); + + if (getline(&buf, &bufn, f) == -1) die_perror("getline"); + char *sp = strchr(buf, ' '); + if (sp) *sp = '-'; + + printf("%s %s", dp->d_name, buf); + + free(p); + fclose(f); + } while (dp != NULL); +done: + closedir(d); + } + return 0; +} diff --git a/src/main.c b/src/main.c @@ -0,0 +1,26 @@ +#include <stdlib.h> +#include "kiss.h" + +noreturn void +usage(int r) { + mylog("kiss [l] [pkg]..."); + mylog("list List installed packages"); + exit(r); +} + +int +main(int argc, char **argv) { + if (argc < 2) usage(0); + + struct env *e = setup_env(); + switch (argv[1][0]) { + case 'l': + list(argc - 1, argv + 1, e); + break; + default: + /* TODO: external tools */ + usage(1); + } + destroy_env(e); + return 0; +}