From: Francois Fleuret Date: Sun, 7 Mar 2010 14:44:55 +0000 (+0100) Subject: Directory scan now works. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=85eb33af87f8e0cfdb08cb888adbd1e5cc02a540;p=finddup.git Directory scan now works. --- diff --git a/finddup.c b/finddup.c index 0d5d5af..d3e5a12 100644 --- a/finddup.c +++ b/finddup.c @@ -147,26 +147,12 @@ struct file_list { struct file_with_size *head, *tail; }; -void merge(struct *file_list list1, struct *file_list list2) { - if(list1->head && list2->head) { - struct file_list result; - result.head = list1.head; - } else { - if(list1.head) { - return list1; - } else { - return list2; - } - } -} - -struct file_with_size *create(char *name, struct file_with_size *current) { - struct file_with_size *result; - result = safe_malloc(sizeof(struct file_with_size)); - result->filename = strdup(name); - result->size = file_or_dir_size(name); - result->next = current; - return result; +void create(struct file_list *list, const char *name) { + list->head = safe_malloc(sizeof(struct file_with_size)); + list->tail = 0; + list->head->filename = strdup(name); + list->head->size = file_or_dir_size(name); + list->head->next = 0; } void destroy(struct file_with_size *node) { @@ -240,23 +226,20 @@ int same_files(struct file_with_size *f1, struct file_with_size *f2) { /**********************************************************************/ -struct file_list scan_directory(const char *name) { +struct file_with_size *scan_directory(struct file_with_size *tail, const char *name) { DIR *dir; struct dirent *dir_e; - struct file_list result, sublist; struct stat dummy; + struct file_with_size *tmp; char subname[BUFFER_SIZE]; - result.head = 0; - result.tail = 0; - if(lstat(name, &dummy) != 0) { fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno)); exit(EXIT_FAILURE); } if(S_ISLNK(dummy.st_mode)) { - return result; + return tail; } dir = opendir(name); @@ -265,24 +248,37 @@ struct file_list scan_directory(const char *name) { while((dir_e = readdir(dir))) { if(!ignore_entry(dir_e->d_name)) { snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name); - sublist = scan_directory(subname); - result = concat(result, sublist); + tail = scan_directory(tail, subname); } } closedir(dir); } else { if(S_ISREG(dummy.st_mode)) { - result += dummy.st_size; + tmp = safe_malloc(sizeof(struct file_with_size)); + tmp->next = tail; + tmp->filename = strdup(name); + tmp->size = file_or_dir_size(name); + tail = tmp; } } - return result; + return tail; } void start(const char *dirname1, const char *dirname2) { struct file_with_size *list1, *list2; - list1 = scan_directory(dirname1); - list2 = scan_directory(dirname2); + struct file_with_size *node; + list1 = scan_directory(0, dirname1); + list2 = scan_directory(0, dirname2); + + for(node = list1; node; node = node->next) { + printf("list1: %s\n", node->filename); + } + + for(node = list2; node; node = node->next) { + printf("list2: %s\n", node->filename); + } + destroy(list1); destroy(list2); } @@ -316,8 +312,8 @@ int main(int argc, char **argv) { } } - if (optind + 2 < argc) { - start(argv[optind + 1], argv[optind + 2]); + if(optind + 1 < argc) { + start(argv[optind], argv[optind + 1]); } else { fprintf(stderr, "%s [OPTIONS] \n", argv[0]); exit(EXIT_FAILURE);