diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-09 23:19:53 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-09 23:19:53 +0100 |
| commit | 1566b6faa8534118c3566188181367cd0868468f (patch) | |
| tree | 1de8d4b369efb5e592685a31088f798a6b63ffa1 /list.c | |
| parent | 349991bf6efe473ab9a5cbdae0a8114d72b997e3 (diff) | |
| download | crep-1566b6faa8534118c3566188181367cd0868468f.tar.gz | |
Added partial matching and introduced threads
Diffstat (limited to 'list.c')
| -rw-r--r-- | list.c | 64 |
1 files changed, 64 insertions, 0 deletions
@@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <dirent.h> +#include <sys/stat.h> +#include <string.h> + +#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 list_files_recursively(char *base_path, Node **head) { + char path[1000]; + struct dirent *dp; + DIR *dir = opendir(base_path); + + 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); + + 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; + + while (head != NULL) { + tmp = head; + head = head->next; + free(tmp->file_path); + free(tmp); + } +} + +int size_of_file_list(Node *head) { + int count = 0; + + Node *current = head; + while (current != NULL) { + count++; + current = current->next; + } + + return count; +} |
