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) {
/**********************************************************************/
-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);
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);
}
}
}
- 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] <dir1> <dir2>\n", argv[0]);
exit(EXIT_FAILURE);