diff options
| author | hachem <im@hachem.wtf> | 2026-09-02 01:37:47 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-02 01:37:47 +0200 |
| commit | d15a15ae9f86e478f20a013c77d2a08c5fa03bd2 (patch) | |
| tree | 51c623fe68ed04b1ac863f267d4c00f6a6433dce | |
| parent | 0699f07fb537d108a513a90ce44332138b5570ed (diff) | |
feat: page enumeration
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | src/main.c | 58 |
2 files changed, 49 insertions, 11 deletions
@@ -1,6 +1,6 @@ bin/ -compile_options.json +compile_commands.json compile_flags.txt .clangd @@ -1,6 +1,41 @@ +#include <dirent.h> #include <stdio.h> #include <string.h> +static void list_pages(const char* section) +{ + DIR* section_stream = opendir(section); + if (section_stream == NULL) + return; + + for (const struct dirent* entry = readdir(section_stream); + entry != NULL; + entry = readdir(section_stream)) + if (entry->d_name[0] != '.') + (void) printf(" page: %s/%s\n", section, entry->d_name); + + (void) closedir(section_stream); +} + +static void list_sections(const char* directory) +{ + DIR* directory_stream = opendir(directory); + if (directory_stream == NULL) + return; + + for (const struct dirent* entry = readdir(directory_stream); + entry != NULL; + entry = readdir(directory_stream)) + if (strncmp(entry->d_name, "man", 3) == 0) + { + char section[4096]; + (void) snprintf(section, sizeof(section), "%s/%s", directory, entry->d_name); + list_pages(section); + } + + (void) closedir(directory_stream); +} + int main(int argc, char** argv) { if (argc != 2) @@ -13,30 +48,33 @@ int main(int argc, char** argv) (void) printf("searching for: %s\n", query); - FILE* pipe = popen("manpath", "r"); - if (pipe == NULL) + FILE* manpath_pipe = popen("manpath", "r"); + if (manpath_pipe == NULL) { (void) fprintf(stderr, "jafm: failed to run manpath\n"); return 1; } char buffer[4096]; - if (fgets(buffer, sizeof(buffer), pipe) == NULL) + if (fgets(buffer, sizeof(buffer), manpath_pipe) == NULL) { (void) fprintf(stderr, "jafm: failed to read manpath\n"); - (void) pclose(pipe); + (void) pclose(manpath_pipe); return 1; } - (void) pclose(pipe); + (void) pclose(manpath_pipe); buffer[strcspn(buffer, "\n")] = '\0'; - char* saveptr = NULL; - for (char* dir = strtok_r(buffer, ":", &saveptr); - dir != NULL; - dir = strtok_r(NULL, ":", &saveptr)) - (void) printf("man dir: %s\n", dir); + char* save_pointer = NULL; + for (const char* directory = strtok_r(buffer, ":", &save_pointer); + directory != NULL; + directory = strtok_r(NULL, ":", &save_pointer)) + { + (void) printf("man dir: %s\n", directory); + list_sections(directory); + } return 0; } |
