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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* Read, sort and compare two directories. Used for GNU DIFF.
Copyright (C) 1988, 1989 Free Software Foundation, Inc.
This file is part of GNU DIFF.
GNU DIFF is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU DIFF; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "diff.h"
static int compare_names ();
/* Read the directory named DIRNAME and return a sorted vector
of filenames for its contents. NONEX nonzero means this directory is
known to be nonexistent, so return zero files. */
struct dirdata
{
int length; /* # elements in `files' */
char **files; /* Sorted names of files in the dir */
};
static struct dirdata
dir_sort (dirname, nonex)
char *dirname;
int nonex;
{
register DIR *reading;
register struct direct *next;
struct dirdata dirdata;
/* Address of block containing the files that are described. */
char **files;
/* Length of block that `files' points to, measured in files. */
int nfiles;
/* Index of first unused in `files'. */
int files_index;
if (nonex)
{
dirdata.length = 0;
dirdata.files = 0;
return dirdata;
}
/* Open the directory and check for errors. */
reading = opendir (dirname);
if (!reading)
{
perror_with_name (dirname);
dirdata.length = -1;
return dirdata;
}
/* Initialize the table of filenames. */
nfiles = 100;
files = (char **) xmalloc (nfiles * sizeof (char *));
files_index = 0;
/* Read the directory entries, and insert the subfiles
into the `files' table. */
while (next = readdir (reading))
{
/* Ignore the files `.' and `..' */
if (next->d_name[0] == '.'
&& (next->d_name[1] == 0
|| (next->d_name[1] == '.'
&& next->d_name[2] == 0)))
continue;
if (files_index == nfiles)
{
nfiles *= 2;
files
= (char **) xrealloc (files, sizeof (char *) * nfiles);
}
files[files_index++] = concat (next->d_name, "", "");
}
closedir (reading);
/* Sort the table. */
qsort (files, files_index, sizeof (char *), compare_names);
/* Return a description of location and length of the table. */
dirdata.files = files;
dirdata.length = files_index;
return dirdata;
}
/* Sort the files now in the table. */
static int
compare_names (file1, file2)
char **file1, **file2;
{
return strcmp (*file1, *file2);
}
/* Compare the contents of two directories named NAME1 and NAME2.
This is a top-level routine; it does everything necessary for diff
on two directories.
NONEX1 nonzero says directory NAME1 doesn't exist, but pretend it is
empty. Likewise NONEX2.
HANDLE_FILE is a caller-provided subroutine called to handle each file.
It gets five operands: dir and name (rel to original working dir) of file
in dir 1, dir and name pathname of file in dir 2, and the recursion depth.
For a file that appears in only one of the dirs, one of the name-args
to HANDLE_FILE is zero.
DEPTH is the current depth in recursion.
Returns the maximum of all the values returned by HANDLE_FILE,
or 2 if trouble is encountered in opening files. */
int
diff_dirs (name1, name2, handle_file, depth, nonex1, nonex2)
char *name1, *name2;
int (*handle_file) ();
int nonex1, nonex2;
{
struct dirdata data1, data2;
register int i1, i2;
int val = 0;
int v1;
/* Get sorted contents of both dirs. */
data1 = dir_sort (name1, nonex1);
data2 = dir_sort (name2, nonex2);
if (data1.length == -1 || data2.length == -1)
{
if (data1.length >= 0)
free (data1.files);
if (data2.length >= 0)
free (data2.files);
return 2;
}
i1 = 0;
i2 = 0;
/* If -Sname was specified, and this is the topmost level of comparison,
ignore all file names less than the specified starting name. */
if (dir_start_file && depth == 0)
{
while (i1 < data1.length && strcmp (data1.files[i1], dir_start_file) < 0)
i1++;
while (i2 < data2.length && strcmp (data2.files[i2], dir_start_file) < 0)
i2++;
}
/* Loop while files remain in one or both dirs. */
while (i1 < data1.length || i2 < data2.length)
{
int nameorder;
/* Compare next name in dir 1 with next name in dir 2.
At the end of a dir,
pretend the "next name" in that dir is very large. */
if (i1 == data1.length)
nameorder = 1;
else if (i2 == data2.length)
nameorder = -1;
else
nameorder = strcmp (data1.files[i1], data2.files[i2]);
if (nameorder == 0)
{
/* We have found a file (or subdir) in common between both dirs.
Compare the two files. */
v1 = (*handle_file) (name1, data1.files[i1], name2, data2.files[i2],
depth + 1);
i1++, i2++;
}
if (nameorder < 0)
{
/* Next filename in dir 1 is less; that is a file in dir 1 only. */
v1 = (*handle_file) (name1, data1.files[i1], name2, 0, depth + 1);
i1++;
}
if (nameorder > 0)
{
/* Next filename in dir 2 is less; that is a file in dir 2 only. */
v1 = (*handle_file) (name1, 0, name2, data2.files[i2], depth + 1);
i2++;
}
if (v1 > val)
val = v1;
}
if (data1.files)
free (data1.files);
if (data2.files)
free (data2.files);
return val;
}
|