hachem.wtf
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-02 02:10:20 +0200
committerhachem <im@hachem.wtf>2026-09-02 02:10:20 +0200
commitbcb73dd4c9a20db6a4a96174476b30dbda236e08 (patch)
tree5f5701080f3b5d22385a6f1f8bd38991dc0423b4
parent9e16bbab28bc8742dfff36636c51aa2da5c9c9de (diff)
feat: collect matches into a numbered list
-rw-r--r--src/main.c60
1 files changed, 51 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index c87bc0d..bc29a4c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,12 +1,52 @@
+#define _POSIX_C_SOURCE 200809L
+
#include <dirent.h>
#include <zlib.h>
#include <stdbool.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#define LINE_SIZE 8192
+struct MatchList
+{
+ char** paths;
+ size_t count;
+ size_t capacity;
+};
+
+static bool match_list_add(struct MatchList* matches, const char* path)
+{
+ if (matches->count == matches->capacity)
+ {
+ size_t new_capacity = matches->capacity == 0 ? 16 : matches->capacity * 2;
+ char** grown = realloc(matches->paths, new_capacity * sizeof(char*));
+ if (grown == NULL)
+ return false;
+
+ matches->paths = grown;
+ matches->capacity = new_capacity;
+ }
+
+ char* copy = strdup(path);
+ if (copy == NULL)
+ return false;
+
+ matches->paths[matches->count] = copy;
+ matches->count++;
+ return true;
+}
+
+static void match_list_free(struct MatchList* matches)
+{
+ for (size_t index = 0; index < matches->count; index++)
+ free(matches->paths[index]);
+
+ free(matches->paths);
+}
+
static bool ends_with(const char* string, const char* suffix)
{
size_t string_length = strlen(string);
@@ -64,7 +104,7 @@ static bool page_contains(const char* path, const char* query)
return plain_page_contains(path, query);
}
-static void list_pages(const char* section, const char* query)
+static void list_pages(const char* section, const char* query, struct MatchList* matches)
{
DIR* section_stream = opendir(section);
if (section_stream == NULL)
@@ -78,13 +118,13 @@ static void list_pages(const char* section, const char* query)
char path[4096];
(void) snprintf(path, sizeof(path), "%s/%s", section, entry->d_name);
if (page_contains(path, query))
- (void) printf(" match: %s\n", path);
+ (void) match_list_add(matches, path);
}
(void) closedir(section_stream);
}
-static void list_sections(const char* directory, const char* query)
+static void list_sections(const char* directory, const char* query, struct MatchList* matches)
{
DIR* directory_stream = opendir(directory);
if (directory_stream == NULL)
@@ -97,7 +137,7 @@ static void list_sections(const char* directory, const char* query)
{
char section[4096];
(void) snprintf(section, sizeof(section), "%s/%s", directory, entry->d_name);
- list_pages(section, query);
+ list_pages(section, query, matches);
}
(void) closedir(directory_stream);
@@ -113,8 +153,6 @@ int main(int argc, char** argv)
const char* query = argv[1];
- (void) printf("searching for: %s\n", query);
-
FILE* manpath_pipe = popen("manpath", "r");
if (manpath_pipe == NULL)
{
@@ -134,13 +172,17 @@ int main(int argc, char** argv)
buffer[strcspn(buffer, "\n")] = '\0';
+ struct MatchList matches = {0};
+
char* save_pointer = NULL;
for (const char* directory = strtok_r(buffer, ":", &save_pointer);
directory != NULL;
directory = strtok_r(NULL, ":", &save_pointer))
- {
- list_sections(directory, query);
- }
+ list_sections(directory, query, &matches);
+
+ for (size_t index = 0; index < matches.count; index++)
+ (void) printf("%zu) %s\n", index + 1, matches.paths[index]);
+ match_list_free(&matches);
return 0;
}