1 /* Read, sort and compare two directories. Used for GNU DIFF.
2 Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
20 /* Read the directory named by DIR and store into DIRDATA a sorted vector
21 of filenames for its contents. DIR->desc == -1 means this directory is
22 known to be nonexistent, so set DIRDATA to an empty vector.
23 Return -1 (setting errno) if error, 0 otherwise. */
27 char const **names; /* Sorted names of files in dir, 0-terminated. */
28 char *data; /* Allocated storage for file names. */
31 static int compare_names PARAMS((void const *, void const *));
32 static int dir_sort PARAMS((struct file_data const *, struct dirdata *));
35 #define CLOSEDIR_VOID 1
39 dir_sort (dir, dirdata)
40 struct file_data const *dir;
41 struct dirdata *dirdata;
43 register struct dirent *next;
46 /* Address of block containing the files that are described. */
49 /* Number of files in directory. */
52 /* Allocated and used storage for file name data. */
54 size_t data_alloc, data_used;
63 /* Open the directory and check for errors. */
64 register DIR *reading = CVS_OPENDIR (dir->name);
68 /* Initialize the table of filenames. */
70 data_alloc = max (1, (size_t) dir->stat.st_size);
72 dirdata->data = data = xmalloc (data_alloc);
74 /* Read the directory entries, and insert the subfiles
75 into the `data' table. */
77 while ((errno = 0, (next = CVS_READDIR (reading)) != 0))
79 char *d_name = next->d_name;
80 size_t d_size = NAMLEN (next) + 1;
82 /* Ignore the files `.' and `..' */
84 && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
87 if (excluded_filename (d_name))
90 while (data_alloc < data_used + d_size)
91 dirdata->data = data = xrealloc (data, data_alloc *= 2);
92 memcpy (data + data_used, d_name, d_size);
99 CVS_CLOSEDIR (reading);
104 CVS_CLOSEDIR (reading);
106 if (CVS_CLOSEDIR (reading) != 0)
111 /* Create the `names' table from the `data' table. */
112 dirdata->names = names = (char const **) xmalloc (sizeof (char *)
114 for (i = 0; i < nnames; i++)
117 data += strlen (data) + 1;
121 /* Sort the table. */
122 qsort (names, nnames, sizeof (char *), compare_names);
127 /* Sort the files now in the table. */
130 compare_names (file1, file2)
131 void const *file1, *file2;
133 return filename_cmp (* (char const *const *) file1,
134 * (char const *const *) file2);
137 /* Compare the contents of two directories named in FILEVEC[0] and FILEVEC[1].
138 This is a top-level routine; it does everything necessary for diff
141 FILEVEC[0].desc == -1 says directory FILEVEC[0] doesn't exist,
142 but pretend it is empty. Likewise for FILEVEC[1].
144 HANDLE_FILE is a caller-provided subroutine called to handle each file.
145 It gets five operands: dir and name (rel to original working dir) of file
146 in dir 0, dir and name pathname of file in dir 1, and the recursion depth.
148 For a file that appears in only one of the dirs, one of the name-args
149 to HANDLE_FILE is zero.
151 DEPTH is the current depth in recursion, used for skipping top-level
152 files by the -S option.
154 Returns the maximum of all the values returned by HANDLE_FILE,
155 or 2 if trouble is encountered in opening files. */
158 diff_dirs (filevec, handle_file, depth)
159 struct file_data const filevec[];
160 int (*handle_file) PARAMS((char const *, char const *, char const *, char const *, int));
163 struct dirdata dirdata[2];
164 int val = 0; /* Return value. */
167 /* Get sorted contents of both dirs. */
168 for (i = 0; i < 2; i++)
169 if (dir_sort (&filevec[i], &dirdata[i]) != 0)
171 perror_with_name (filevec[i].name);
177 register char const * const *names0 = dirdata[0].names;
178 register char const * const *names1 = dirdata[1].names;
179 char const *name0 = filevec[0].name;
180 char const *name1 = filevec[1].name;
182 /* If `-S name' was given, and this is the topmost level of comparison,
183 ignore all file names less than the specified starting name. */
185 if (dir_start_file && depth == 0)
187 while (*names0 && filename_cmp (*names0, dir_start_file) < 0)
189 while (*names1 && filename_cmp (*names1, dir_start_file) < 0)
193 /* Loop while files remain in one or both dirs. */
194 while (*names0 || *names1)
196 /* Compare next name in dir 0 with next name in dir 1.
198 pretend the "next name" in that dir is very large. */
199 int nameorder = (!*names0 ? 1 : !*names1 ? -1
200 : filename_cmp (*names0, *names1));
201 int v1 = (*handle_file) (name0, 0 < nameorder ? 0 : *names0++,
202 name1, nameorder < 0 ? 0 : *names1++,
209 for (i = 0; i < 2; i++)
211 if (dirdata[i].names)
212 free (dirdata[i].names);
214 free (dirdata[i].data);