summaryrefslogtreecommitdiff
path: root/difftool/compare_dir.c
blob: 8d4a186dc7c03c667a6b4569d37483f6f1f3bfb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * compare_dir.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "difftool.h"

int compare_dir_entries(tree_node_t *a, tree_node_t *b)
{
	tree_node_t *ait = a->data.dir->children, *aprev = NULL;
	tree_node_t *bit = b->data.dir->children, *bprev = NULL;
	int ret, result = 0;
	char *path, arrow;

	while (ait != NULL && bit != NULL) {
		ret = strcmp(ait->name, bit->name);

		if (ret < 0) {
			result = 1;
			path = node_path(ait);
			if (path == NULL)
				return -1;
			fprintf(stdout, "< %s\n", path);
			free(path);

			if (aprev == NULL) {
				a->data.dir->children = ait->next;
				free(ait);
				ait = a->data.dir->children;
			} else {
				aprev->next = ait->next;
				free(ait);
				ait = aprev->next;
			}
		} else if (ret > 0) {
			result = 1;
			path = node_path(bit);
			if (path == NULL)
				return -1;
			fprintf(stdout, "> %s\n", path);
			free(path);

			if (bprev == NULL) {
				b->data.dir->children = bit->next;
				free(bit);
				bit = b->data.dir->children;
			} else {
				bprev->next = bit->next;
				free(bit);
				bit = bprev->next;
			}
		} else {
			aprev = ait;
			ait = ait->next;

			bprev = bit;
			bit = bit->next;
		}
	}

	if (ait != NULL || bit != NULL) {
		result = 1;

		if (ait != NULL) {
			if (aprev == NULL) {
				a->data.dir->children = NULL;
			} else {
				aprev->next = NULL;
			}
			arrow = '<';
		} else {
			if (bprev == NULL) {
				b->data.dir->children = NULL;
			} else {
				bprev->next = NULL;
			}
			arrow = '>';
			ait = bit;
		}

		while (ait != NULL) {
			path = node_path(ait);
			if (path == NULL) {
				result = -1;
			} else {
				fprintf(stdout, "%c %s\n", arrow, path);
				free(path);
			}
			aprev = ait;
			ait = ait->next;
			free(aprev);
		}
	}

	return result;
}