ckiss

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

commit 586535a4c54b67f314cc0d5a6ec24338aa27da9e
parent 55f587ec2b698c13db8296be3fc60ed321ce0911
Author: aabacchus <ben@bvnf.space>
Date:   Sat, 22 Apr 2023 04:19:43 +0100

append sys_db to KISS_PATH for search

Diffstat:
MREADME | 2+-
Msrc/ckiss.h | 6++++++
Msrc/search.c | 10++++++++--
Msrc/utils.c | 34+++++++++++++++++++++++++---------
4 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/README b/README @@ -14,7 +14,7 @@ An implementation of the kiss package manager in C. [ ] install [x] list [ ] remove -[.] search +[x] search [ ] update [ ] upgrade [x] version diff --git a/src/ckiss.h b/src/ckiss.h @@ -50,11 +50,17 @@ noreturn void die_perror(const char *s); * terminated with a NULL. Returned string must be freed by caller.*/ char *concat(char *s, ...); +/* Walk an array to find its length, excluding the terminating NULL record. */ +size_t arr_len(char **arr); + /* Appends to *arr (allocs if NULL). If you don't know the length of arr but it * has a terminating NULL, supply an n < 0. If s should be strdup'd then set dup * to true. */ char **append_to_array(char ***arr, char *s, int n, bool dup); +/* Make a deep copy of an array. */ +char ** arr_copy(char **arr); + /* splits s by any delimiters in sep into an array of strings. Each string and * the array must be freed by the caller. */ char **split(char *s, char *sep); diff --git a/src/search.c b/src/search.c @@ -2,17 +2,23 @@ #include <stdlib.h> #include "ckiss.h" -/* TODO: append sys_db to kiss_path */ int search(int argc, char **argv, struct env *e) { + /* make a copy of kiss_path and append sys_db to it */ + char **p = arr_copy(e->kiss_path); + append_to_array(&p, e->sys_db, -1, true); + for (int i = 1; i < argc; i++) { - char **s = find_in_path(argv[i], e->kiss_path, S_IFDIR, false, true); + char **s = find_in_path(argv[i], p, S_IFDIR, false, true); + if (s == NULL) die2(argv[i], "not found"); + for (int j = 0; s[j] != NULL; j++) { printf("%s\n", s[j]); free(s[j]); } + free(s); } return 0; diff --git a/src/utils.c b/src/utils.c @@ -92,18 +92,22 @@ concat(char *s, ...) { return c; } +size_t +arr_len(char **arr) { + size_t n = 0; + if (arr) { + while (*arr != NULL) { + arr++; + n++; + } + } + return n; +} + char ** append_to_array(char ***arr, char *s, int n, bool dup) { if (n < 0) { - /* need to walk it to find out how many existing entries. */ - n = 0; - if (*arr) { - char **t = *arr; - while (*t != NULL) { - t++; - n++; - } - } + n = arr_len(*arr); } char **tmp = realloc(*arr, sizeof(char *) * ++n); if (tmp == NULL) { @@ -116,6 +120,18 @@ append_to_array(char ***arr, char *s, int n, bool dup) { } char ** +arr_copy(char **arr) { + size_t n = arr_len(arr); + char **s = malloc(sizeof(char *) * (n + 1)); + if (s == NULL) + die_perror("malloc"); + for (size_t i = 0; i < n; i++) + s[i] = strdup(arr[i]); + s[n] = NULL; + return s; +} + +char ** split(char *s, char *sep) { if (s == NULL) return NULL;