1*86d7f5d3SJohn Marino /* Read, sort and compare two directories. Used for GNU DIFF.
2*86d7f5d3SJohn Marino Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
3*86d7f5d3SJohn Marino
4*86d7f5d3SJohn Marino This file is part of GNU DIFF.
5*86d7f5d3SJohn Marino
6*86d7f5d3SJohn Marino GNU DIFF is free software; you can redistribute it and/or modify
7*86d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
8*86d7f5d3SJohn Marino the Free Software Foundation; either version 2, or (at your option)
9*86d7f5d3SJohn Marino any later version.
10*86d7f5d3SJohn Marino
11*86d7f5d3SJohn Marino GNU DIFF is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*86d7f5d3SJohn Marino GNU General Public License for more details.
15*86d7f5d3SJohn Marino
16*86d7f5d3SJohn Marino */
17*86d7f5d3SJohn Marino
18*86d7f5d3SJohn Marino #include "diff.h"
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino /* Read the directory named by DIR and store into DIRDATA a sorted vector
21*86d7f5d3SJohn Marino of filenames for its contents. DIR->desc == -1 means this directory is
22*86d7f5d3SJohn Marino known to be nonexistent, so set DIRDATA to an empty vector.
23*86d7f5d3SJohn Marino Return -1 (setting errno) if error, 0 otherwise. */
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino struct dirdata
26*86d7f5d3SJohn Marino {
27*86d7f5d3SJohn Marino char const **names; /* Sorted names of files in dir, 0-terminated. */
28*86d7f5d3SJohn Marino char *data; /* Allocated storage for file names. */
29*86d7f5d3SJohn Marino };
30*86d7f5d3SJohn Marino
31*86d7f5d3SJohn Marino static int compare_names PARAMS((void const *, void const *));
32*86d7f5d3SJohn Marino static int dir_sort PARAMS((struct file_data const *, struct dirdata *));
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino #ifdef _WIN32
35*86d7f5d3SJohn Marino #define CLOSEDIR_VOID 1
36*86d7f5d3SJohn Marino #endif
37*86d7f5d3SJohn Marino
38*86d7f5d3SJohn Marino static int
dir_sort(dir,dirdata)39*86d7f5d3SJohn Marino dir_sort (dir, dirdata)
40*86d7f5d3SJohn Marino struct file_data const *dir;
41*86d7f5d3SJohn Marino struct dirdata *dirdata;
42*86d7f5d3SJohn Marino {
43*86d7f5d3SJohn Marino register struct dirent *next;
44*86d7f5d3SJohn Marino register int i;
45*86d7f5d3SJohn Marino
46*86d7f5d3SJohn Marino /* Address of block containing the files that are described. */
47*86d7f5d3SJohn Marino char const **names;
48*86d7f5d3SJohn Marino
49*86d7f5d3SJohn Marino /* Number of files in directory. */
50*86d7f5d3SJohn Marino size_t nnames;
51*86d7f5d3SJohn Marino
52*86d7f5d3SJohn Marino /* Allocated and used storage for file name data. */
53*86d7f5d3SJohn Marino char *data;
54*86d7f5d3SJohn Marino size_t data_alloc, data_used;
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino dirdata->names = 0;
57*86d7f5d3SJohn Marino dirdata->data = 0;
58*86d7f5d3SJohn Marino nnames = 0;
59*86d7f5d3SJohn Marino data = 0;
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino if (dir->desc != -1)
62*86d7f5d3SJohn Marino {
63*86d7f5d3SJohn Marino /* Open the directory and check for errors. */
64*86d7f5d3SJohn Marino register DIR *reading = CVS_OPENDIR (dir->name);
65*86d7f5d3SJohn Marino if (!reading)
66*86d7f5d3SJohn Marino return -1;
67*86d7f5d3SJohn Marino
68*86d7f5d3SJohn Marino /* Initialize the table of filenames. */
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino data_alloc = max (1, (size_t) dir->stat.st_size);
71*86d7f5d3SJohn Marino data_used = 0;
72*86d7f5d3SJohn Marino dirdata->data = data = xmalloc (data_alloc);
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino /* Read the directory entries, and insert the subfiles
75*86d7f5d3SJohn Marino into the `data' table. */
76*86d7f5d3SJohn Marino
77*86d7f5d3SJohn Marino while ((errno = 0, (next = CVS_READDIR (reading)) != 0))
78*86d7f5d3SJohn Marino {
79*86d7f5d3SJohn Marino char *d_name = next->d_name;
80*86d7f5d3SJohn Marino size_t d_size = NAMLEN (next) + 1;
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino /* Ignore the files `.' and `..' */
83*86d7f5d3SJohn Marino if (d_name[0] == '.'
84*86d7f5d3SJohn Marino && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
85*86d7f5d3SJohn Marino continue;
86*86d7f5d3SJohn Marino
87*86d7f5d3SJohn Marino if (excluded_filename (d_name))
88*86d7f5d3SJohn Marino continue;
89*86d7f5d3SJohn Marino
90*86d7f5d3SJohn Marino while (data_alloc < data_used + d_size)
91*86d7f5d3SJohn Marino dirdata->data = data = xrealloc (data, data_alloc *= 2);
92*86d7f5d3SJohn Marino memcpy (data + data_used, d_name, d_size);
93*86d7f5d3SJohn Marino data_used += d_size;
94*86d7f5d3SJohn Marino nnames++;
95*86d7f5d3SJohn Marino }
96*86d7f5d3SJohn Marino if (errno)
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino int e = errno;
99*86d7f5d3SJohn Marino CVS_CLOSEDIR (reading);
100*86d7f5d3SJohn Marino errno = e;
101*86d7f5d3SJohn Marino return -1;
102*86d7f5d3SJohn Marino }
103*86d7f5d3SJohn Marino #if CLOSEDIR_VOID
104*86d7f5d3SJohn Marino CVS_CLOSEDIR (reading);
105*86d7f5d3SJohn Marino #else
106*86d7f5d3SJohn Marino if (CVS_CLOSEDIR (reading) != 0)
107*86d7f5d3SJohn Marino return -1;
108*86d7f5d3SJohn Marino #endif
109*86d7f5d3SJohn Marino }
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marino /* Create the `names' table from the `data' table. */
112*86d7f5d3SJohn Marino dirdata->names = names = (char const **) xmalloc (sizeof (char *)
113*86d7f5d3SJohn Marino * (nnames + 1));
114*86d7f5d3SJohn Marino for (i = 0; i < nnames; i++)
115*86d7f5d3SJohn Marino {
116*86d7f5d3SJohn Marino names[i] = data;
117*86d7f5d3SJohn Marino data += strlen (data) + 1;
118*86d7f5d3SJohn Marino }
119*86d7f5d3SJohn Marino names[nnames] = 0;
120*86d7f5d3SJohn Marino
121*86d7f5d3SJohn Marino /* Sort the table. */
122*86d7f5d3SJohn Marino qsort (names, nnames, sizeof (char *), compare_names);
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino return 0;
125*86d7f5d3SJohn Marino }
126*86d7f5d3SJohn Marino
127*86d7f5d3SJohn Marino /* Sort the files now in the table. */
128*86d7f5d3SJohn Marino
129*86d7f5d3SJohn Marino static int
compare_names(file1,file2)130*86d7f5d3SJohn Marino compare_names (file1, file2)
131*86d7f5d3SJohn Marino void const *file1, *file2;
132*86d7f5d3SJohn Marino {
133*86d7f5d3SJohn Marino return filename_cmp (* (char const *const *) file1,
134*86d7f5d3SJohn Marino * (char const *const *) file2);
135*86d7f5d3SJohn Marino }
136*86d7f5d3SJohn Marino
137*86d7f5d3SJohn Marino /* Compare the contents of two directories named in FILEVEC[0] and FILEVEC[1].
138*86d7f5d3SJohn Marino This is a top-level routine; it does everything necessary for diff
139*86d7f5d3SJohn Marino on two directories.
140*86d7f5d3SJohn Marino
141*86d7f5d3SJohn Marino FILEVEC[0].desc == -1 says directory FILEVEC[0] doesn't exist,
142*86d7f5d3SJohn Marino but pretend it is empty. Likewise for FILEVEC[1].
143*86d7f5d3SJohn Marino
144*86d7f5d3SJohn Marino HANDLE_FILE is a caller-provided subroutine called to handle each file.
145*86d7f5d3SJohn Marino It gets five operands: dir and name (rel to original working dir) of file
146*86d7f5d3SJohn Marino in dir 0, dir and name pathname of file in dir 1, and the recursion depth.
147*86d7f5d3SJohn Marino
148*86d7f5d3SJohn Marino For a file that appears in only one of the dirs, one of the name-args
149*86d7f5d3SJohn Marino to HANDLE_FILE is zero.
150*86d7f5d3SJohn Marino
151*86d7f5d3SJohn Marino DEPTH is the current depth in recursion, used for skipping top-level
152*86d7f5d3SJohn Marino files by the -S option.
153*86d7f5d3SJohn Marino
154*86d7f5d3SJohn Marino Returns the maximum of all the values returned by HANDLE_FILE,
155*86d7f5d3SJohn Marino or 2 if trouble is encountered in opening files. */
156*86d7f5d3SJohn Marino
157*86d7f5d3SJohn Marino int
diff_dirs(filevec,handle_file,depth)158*86d7f5d3SJohn Marino diff_dirs (filevec, handle_file, depth)
159*86d7f5d3SJohn Marino struct file_data const filevec[];
160*86d7f5d3SJohn Marino int (*handle_file) PARAMS((char const *, char const *, char const *, char const *, int));
161*86d7f5d3SJohn Marino int depth;
162*86d7f5d3SJohn Marino {
163*86d7f5d3SJohn Marino struct dirdata dirdata[2];
164*86d7f5d3SJohn Marino int val = 0; /* Return value. */
165*86d7f5d3SJohn Marino int i;
166*86d7f5d3SJohn Marino
167*86d7f5d3SJohn Marino /* Get sorted contents of both dirs. */
168*86d7f5d3SJohn Marino for (i = 0; i < 2; i++)
169*86d7f5d3SJohn Marino if (dir_sort (&filevec[i], &dirdata[i]) != 0)
170*86d7f5d3SJohn Marino {
171*86d7f5d3SJohn Marino perror_with_name (filevec[i].name);
172*86d7f5d3SJohn Marino val = 2;
173*86d7f5d3SJohn Marino }
174*86d7f5d3SJohn Marino
175*86d7f5d3SJohn Marino if (val == 0)
176*86d7f5d3SJohn Marino {
177*86d7f5d3SJohn Marino register char const * const *names0 = dirdata[0].names;
178*86d7f5d3SJohn Marino register char const * const *names1 = dirdata[1].names;
179*86d7f5d3SJohn Marino char const *name0 = filevec[0].name;
180*86d7f5d3SJohn Marino char const *name1 = filevec[1].name;
181*86d7f5d3SJohn Marino
182*86d7f5d3SJohn Marino /* If `-S name' was given, and this is the topmost level of comparison,
183*86d7f5d3SJohn Marino ignore all file names less than the specified starting name. */
184*86d7f5d3SJohn Marino
185*86d7f5d3SJohn Marino if (dir_start_file && depth == 0)
186*86d7f5d3SJohn Marino {
187*86d7f5d3SJohn Marino while (*names0 && filename_cmp (*names0, dir_start_file) < 0)
188*86d7f5d3SJohn Marino names0++;
189*86d7f5d3SJohn Marino while (*names1 && filename_cmp (*names1, dir_start_file) < 0)
190*86d7f5d3SJohn Marino names1++;
191*86d7f5d3SJohn Marino }
192*86d7f5d3SJohn Marino
193*86d7f5d3SJohn Marino /* Loop while files remain in one or both dirs. */
194*86d7f5d3SJohn Marino while (*names0 || *names1)
195*86d7f5d3SJohn Marino {
196*86d7f5d3SJohn Marino /* Compare next name in dir 0 with next name in dir 1.
197*86d7f5d3SJohn Marino At the end of a dir,
198*86d7f5d3SJohn Marino pretend the "next name" in that dir is very large. */
199*86d7f5d3SJohn Marino int nameorder = (!*names0 ? 1 : !*names1 ? -1
200*86d7f5d3SJohn Marino : filename_cmp (*names0, *names1));
201*86d7f5d3SJohn Marino int v1 = (*handle_file) (name0, 0 < nameorder ? 0 : *names0++,
202*86d7f5d3SJohn Marino name1, nameorder < 0 ? 0 : *names1++,
203*86d7f5d3SJohn Marino depth + 1);
204*86d7f5d3SJohn Marino if (v1 > val)
205*86d7f5d3SJohn Marino val = v1;
206*86d7f5d3SJohn Marino }
207*86d7f5d3SJohn Marino }
208*86d7f5d3SJohn Marino
209*86d7f5d3SJohn Marino for (i = 0; i < 2; i++)
210*86d7f5d3SJohn Marino {
211*86d7f5d3SJohn Marino if (dirdata[i].names)
212*86d7f5d3SJohn Marino free (dirdata[i].names);
213*86d7f5d3SJohn Marino if (dirdata[i].data)
214*86d7f5d3SJohn Marino free (dirdata[i].data);
215*86d7f5d3SJohn Marino }
216*86d7f5d3SJohn Marino
217*86d7f5d3SJohn Marino return val;
218*86d7f5d3SJohn Marino }
219