summaryrefslogtreecommitdiff
path: root/list.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:22:16 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:22:16 +0100
commitc7ab12bba64d9c20ccd79b132dac475f7bc3923e (patch)
treeabf2891f9bd1bfa549ed460b288e2c19348bc230 /list.c
parent2b3d92e401f0065e440b51da9a6532695b37ef84 (diff)
downloadcrep-c7ab12bba64d9c20ccd79b132dac475f7bc3923e.tar.gz
Re-enable multi-threading
Diffstat (limited to 'list.c')
-rw-r--r--list.c98
1 files changed, 55 insertions, 43 deletions
diff --git a/list.c b/list.c
index f958ea2..8c5b478 100644
--- a/list.c
+++ b/list.c
@@ -6,60 +6,72 @@
#include "list.h"
-void add_file_path(Node** head, char* file_path) {
- Node* new = (Node*)malloc(sizeof(Node));
- new->file_path = strdup(file_path);
- new->next = *head;
- *head = new;
+void add_file_path(Node **head, char *file_path) {
+ Node *new = (Node *)malloc(sizeof(Node));
+ if (new == NULL) {
+ perror("malloc");
+ exit(EXIT_FAILURE);
+ }
+ new->file_path = strdup(file_path);
+ if (new->file_path == NULL) {
+ perror("strdup");
+ free(new);
+ exit(EXIT_FAILURE);
+ }
+ new->next = *head;
+ *head = new;
}
-void list_files_recursively(char* base_path, Node** head) {
- char path[1000];
- struct dirent* dp;
- DIR* dir = opendir(base_path);
+void list_files_recursively(char *base_path, Node **head) {
+ char path[2048];
+ struct dirent *dp;
+ DIR *dir = opendir(base_path);
- if (!dir)
- return;
+ if (!dir)
+ return;
- while ((dp = readdir(dir)) != NULL) {
- if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
- strcpy(path, base_path);
- strcat(path, "/");
- strcat(path, dp->d_name);
+ while ((dp = readdir(dir)) != NULL) {
+ if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
+ int ret = snprintf(path, sizeof(path), "%s%s%s", base_path, (base_path[strlen(base_path) - 1] == '/' ? "" : "/"), dp->d_name);
- struct stat statbuf;
- if (stat(path, &statbuf) != -1) {
- if (S_ISDIR(statbuf.st_mode)) {
- list_files_recursively(path, head);
- } else {
- add_file_path(head, path);
- }
- }
- }
- }
+ if (ret >= (int)sizeof(path)) {
+ fprintf(stderr, "Path too long: %s/%s\n", base_path, dp->d_name);
+ continue;
+ }
- closedir(dir);
+ struct stat statbuf;
+ if (stat(path, &statbuf) != -1) {
+ if (S_ISDIR(statbuf.st_mode)) {
+ list_files_recursively(path, head);
+ } else {
+ add_file_path(head, path);
+ }
+ }
+ }
+ }
+
+ closedir(dir);
}
-void free_file_list(Node* head) {
- Node* tmp;
+void free_file_list(Node *head) {
+ Node *tmp;
- while (head != NULL) {
- tmp = head;
- head = head->next;
- free(tmp->file_path);
- free(tmp);
- }
+ while (head != NULL) {
+ tmp = head;
+ head = head->next;
+ free(tmp->file_path);
+ free(tmp);
+ }
}
-int size_of_file_list(Node* head) {
- int count = 0;
+int size_of_file_list(Node *head) {
+ int count = 0;
- Node* current = head;
- while (current != NULL) {
- count++;
- current = current->next;
- }
+ Node *current = head;
+ while (current != NULL) {
+ count++;
+ current = current->next;
+ }
- return count;
+ return count;
}