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