diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-13 05:30:27 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-13 05:30:27 +0100 |
| commit | 0e951a0f8831ac2885fe64531438eb28202f6e53 (patch) | |
| tree | ef9e76c8646c1225db2e542bef775325d9db8a39 /list.c | |
| parent | ab52e851c95ebe2fa1ee4c2a5dcbc826bbb41769 (diff) | |
| download | crep-0e951a0f8831ac2885fe64531438eb28202f6e53.tar.gz | |
Move to Chromium styleguide
Diffstat (limited to 'list.c')
| -rw-r--r-- | list.c | 89 |
1 files changed, 45 insertions, 44 deletions
| @@ -1,64 +1,65 @@ | |||
| 1 | #include <dirent.h> | ||
| 1 | #include <stdio.h> | 2 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 3 | #include <dirent.h> | ||
| 4 | #include <sys/stat.h> | ||
| 5 | #include <string.h> | 4 | #include <string.h> |
| 5 | #include <sys/stat.h> | ||
| 6 | 6 | ||
| 7 | #include "list.h" | 7 | #include "list.h" |
| 8 | 8 | ||
| 9 | void add_file_path(Node **head, char *file_path) { | 9 | void add_file_path(Node** head, char* file_path) { |
| 10 | Node *new = (Node *)malloc(sizeof(Node)); | 10 | Node* new = (Node*)malloc(sizeof(Node)); |
| 11 | new->file_path = strdup(file_path); | 11 | new->file_path = strdup(file_path); |
| 12 | new->next = *head; | 12 | new->next = *head; |
| 13 | *head = new; | 13 | *head = new; |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | void list_files_recursively(char *base_path, Node **head) { | 16 | void list_files_recursively(char* base_path, Node** head) { |
| 17 | char path[1000]; | 17 | char path[1000]; |
| 18 | struct dirent *dp; | 18 | struct dirent* dp; |
| 19 | DIR *dir = opendir(base_path); | 19 | DIR* dir = opendir(base_path); |
| 20 | 20 | ||
| 21 | if (!dir) return; | 21 | if (!dir) |
| 22 | return; | ||
| 22 | 23 | ||
| 23 | while ((dp = readdir(dir)) != NULL) { | 24 | while ((dp = readdir(dir)) != NULL) { |
| 24 | if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) { | 25 | if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) { |
| 25 | strcpy(path, base_path); | 26 | strcpy(path, base_path); |
| 26 | strcat(path, "/"); | 27 | strcat(path, "/"); |
| 27 | strcat(path, dp->d_name); | 28 | strcat(path, dp->d_name); |
| 28 | 29 | ||
| 29 | struct stat statbuf; | 30 | struct stat statbuf; |
| 30 | if (stat(path, &statbuf) != -1) { | 31 | if (stat(path, &statbuf) != -1) { |
| 31 | if (S_ISDIR(statbuf.st_mode)) { | 32 | if (S_ISDIR(statbuf.st_mode)) { |
| 32 | list_files_recursively(path, head); | 33 | list_files_recursively(path, head); |
| 33 | } else { | 34 | } else { |
| 34 | add_file_path(head, path); | 35 | add_file_path(head, path); |
| 35 | } | 36 | } |
| 36 | } | 37 | } |
| 37 | } | 38 | } |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 40 | closedir(dir); | 41 | closedir(dir); |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 43 | void free_file_list(Node *head) { | 44 | void free_file_list(Node* head) { |
| 44 | Node *tmp; | 45 | Node* tmp; |
| 45 | 46 | ||
| 46 | while (head != NULL) { | 47 | while (head != NULL) { |
| 47 | tmp = head; | 48 | tmp = head; |
| 48 | head = head->next; | 49 | head = head->next; |
| 49 | free(tmp->file_path); | 50 | free(tmp->file_path); |
| 50 | free(tmp); | 51 | free(tmp); |
| 51 | } | 52 | } |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | int size_of_file_list(Node *head) { | 55 | int size_of_file_list(Node* head) { |
| 55 | int count = 0; | 56 | int count = 0; |
| 56 | 57 | ||
| 57 | Node *current = head; | 58 | Node* current = head; |
| 58 | while (current != NULL) { | 59 | while (current != NULL) { |
| 59 | count++; | 60 | count++; |
| 60 | current = current->next; | 61 | current = current->next; |
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | return count; | 64 | return count; |
| 64 | } | 65 | } |
