1993-03-21 12:45:37 +03:00
|
|
|
|
/* Read, sort and compare two directories. Used for GNU DIFF.
|
1994-11-01 23:38:40 +03:00
|
|
|
|
Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
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
|
1993-07-09 00:13:23 +04:00
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
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"
|
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Read the directory named by DIR and store into DIRDATA a sorted vector
|
|
|
|
|
of filenames for its contents. DIR->desc == -1 means this directory is
|
|
|
|
|
known to be nonexistent, so set DIRDATA to an empty vector.
|
|
|
|
|
Return -1 (setting errno) if error, 0 otherwise. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
struct dirdata
|
|
|
|
|
{
|
1993-09-16 21:39:00 +04:00
|
|
|
|
char const **names; /* Sorted names of files in dir, 0-terminated. */
|
1993-07-09 00:13:23 +04:00
|
|
|
|
char *data; /* Allocated storage for file names. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
};
|
|
|
|
|
|
1993-09-16 21:39:00 +04:00
|
|
|
|
static int compare_names PARAMS((void const *, void const *));
|
|
|
|
|
static int dir_sort PARAMS((struct file_data const *, struct dirdata *));
|
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
static int
|
|
|
|
|
dir_sort (dir, dirdata)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
struct file_data const *dir;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
struct dirdata *dirdata;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-09-16 21:39:00 +04:00
|
|
|
|
register struct dirent *next;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
register int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
/* Address of block containing the files that are described. */
|
1993-09-16 21:39:00 +04:00
|
|
|
|
char const **names;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Number of files in directory. */
|
1993-09-16 21:39:00 +04:00
|
|
|
|
size_t nnames;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Allocated and used storage for file name data. */
|
|
|
|
|
char *data;
|
|
|
|
|
size_t data_alloc, data_used;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-09-16 21:39:00 +04:00
|
|
|
|
dirdata->names = 0;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
dirdata->data = 0;
|
1993-09-16 21:39:00 +04:00
|
|
|
|
nnames = 0;
|
|
|
|
|
data = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
if (dir->desc != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Open the directory and check for errors. */
|
|
|
|
|
register DIR *reading = opendir (dir->name);
|
|
|
|
|
if (!reading)
|
|
|
|
|
return -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Initialize the table of filenames. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
data_alloc = max (1, (size_t) dir->stat.st_size);
|
|
|
|
|
data_used = 0;
|
1993-09-16 21:39:00 +04:00
|
|
|
|
dirdata->data = data = xmalloc (data_alloc);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Read the directory entries, and insert the subfiles
|
|
|
|
|
into the `data' table. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
while ((errno = 0, (next = readdir (reading)) != 0))
|
|
|
|
|
{
|
|
|
|
|
char *d_name = next->d_name;
|
1994-11-01 23:38:40 +03:00
|
|
|
|
size_t d_size = NAMLEN (next) + 1;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
|
|
|
|
|
/* Ignore the files `.' and `..' */
|
|
|
|
|
if (d_name[0] == '.'
|
|
|
|
|
&& (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (excluded_filename (d_name))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
while (data_alloc < data_used + d_size)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
dirdata->data = data = xrealloc (data, data_alloc *= 2);
|
|
|
|
|
memcpy (data + data_used, d_name, d_size);
|
1993-07-09 00:13:23 +04:00
|
|
|
|
data_used += d_size;
|
1993-09-16 21:39:00 +04:00
|
|
|
|
nnames++;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
}
|
|
|
|
|
if (errno)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-07-09 00:13:23 +04:00
|
|
|
|
int e = errno;
|
|
|
|
|
closedir (reading);
|
|
|
|
|
errno = e;
|
|
|
|
|
return -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
1994-11-01 23:38:40 +03:00
|
|
|
|
#if CLOSEDIR_VOID
|
1993-07-09 00:13:23 +04:00
|
|
|
|
closedir (reading);
|
|
|
|
|
#else
|
|
|
|
|
if (closedir (reading) != 0)
|
|
|
|
|
return -1;
|
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
1993-09-16 21:39:00 +04:00
|
|
|
|
/* Create the `names' table from the `data' table. */
|
|
|
|
|
dirdata->names = names = (char const **) xmalloc (sizeof (char *)
|
|
|
|
|
* (nnames + 1));
|
|
|
|
|
for (i = 0; i < nnames; i++)
|
1993-07-09 00:13:23 +04:00
|
|
|
|
{
|
1993-09-16 21:39:00 +04:00
|
|
|
|
names[i] = data;
|
1993-07-09 00:13:23 +04:00
|
|
|
|
data += strlen (data) + 1;
|
|
|
|
|
}
|
1993-09-16 21:39:00 +04:00
|
|
|
|
names[nnames] = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
/* Sort the table. */
|
1993-09-16 21:39:00 +04:00
|
|
|
|
qsort (names, nnames, sizeof (char *), compare_names);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sort the files now in the table. */
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
compare_names (file1, file2)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
void const *file1, *file2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1994-11-01 23:38:40 +03:00
|
|
|
|
return filename_cmp (* (char const *const *) file1,
|
|
|
|
|
* (char const *const *) file2);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Compare the contents of two directories named in FILEVEC[0] and FILEVEC[1].
|
1993-03-21 12:45:37 +03:00
|
|
|
|
This is a top-level routine; it does everything necessary for diff
|
|
|
|
|
on two directories.
|
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
FILEVEC[0].desc == -1 says directory FILEVEC[0] doesn't exist,
|
|
|
|
|
but pretend it is empty. Likewise for FILEVEC[1].
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
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
|
1993-07-09 00:13:23 +04:00
|
|
|
|
in dir 0, dir and name pathname of file in dir 1, and the recursion depth.
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
For a file that appears in only one of the dirs, one of the name-args
|
|
|
|
|
to HANDLE_FILE is zero.
|
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
DEPTH is the current depth in recursion, used for skipping top-level
|
|
|
|
|
files by the -S option.
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
Returns the maximum of all the values returned by HANDLE_FILE,
|
|
|
|
|
or 2 if trouble is encountered in opening files. */
|
|
|
|
|
|
|
|
|
|
int
|
1993-07-09 00:13:23 +04:00
|
|
|
|
diff_dirs (filevec, handle_file, depth)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
struct file_data const filevec[];
|
|
|
|
|
int (*handle_file) PARAMS((char const *, char const *, char const *, char const *, int));
|
1993-07-09 00:13:23 +04:00
|
|
|
|
int depth;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-07-09 00:13:23 +04:00
|
|
|
|
struct dirdata dirdata[2];
|
|
|
|
|
int val = 0; /* Return value. */
|
|
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
/* Get sorted contents of both dirs. */
|
1993-07-09 00:13:23 +04:00
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
|
if (dir_sort (&filevec[i], &dirdata[i]) != 0)
|
|
|
|
|
{
|
|
|
|
|
perror_with_name (filevec[i].name);
|
|
|
|
|
val = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-09-16 21:39:00 +04:00
|
|
|
|
register char const * const *names0 = dirdata[0].names;
|
|
|
|
|
register char const * const *names1 = dirdata[1].names;
|
|
|
|
|
char const *name0 = filevec[0].name;
|
|
|
|
|
char const *name1 = filevec[1].name;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* If `-S name' was given, and this is the topmost level of comparison,
|
|
|
|
|
ignore all file names less than the specified starting name. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
if (dir_start_file && depth == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1994-11-01 23:38:40 +03:00
|
|
|
|
while (*names0 && filename_cmp (*names0, dir_start_file) < 0)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
names0++;
|
1994-11-01 23:38:40 +03:00
|
|
|
|
while (*names1 && filename_cmp (*names1, dir_start_file) < 0)
|
1993-09-16 21:39:00 +04:00
|
|
|
|
names1++;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
1993-07-09 00:13:23 +04:00
|
|
|
|
|
|
|
|
|
/* Loop while files remain in one or both dirs. */
|
1993-09-16 21:39:00 +04:00
|
|
|
|
while (*names0 || *names1)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
{
|
1993-07-09 00:13:23 +04:00
|
|
|
|
/* Compare next name in dir 0 with next name in dir 1.
|
|
|
|
|
At the end of a dir,
|
|
|
|
|
pretend the "next name" in that dir is very large. */
|
1993-09-16 21:39:00 +04:00
|
|
|
|
int nameorder = (!*names0 ? 1 : !*names1 ? -1
|
1994-11-01 23:38:40 +03:00
|
|
|
|
: filename_cmp (*names0, *names1));
|
1993-09-16 21:39:00 +04:00
|
|
|
|
int v1 = (*handle_file) (name0, 0 < nameorder ? 0 : *names0++,
|
|
|
|
|
name1, nameorder < 0 ? 0 : *names1++,
|
1993-07-09 00:13:23 +04:00
|
|
|
|
depth + 1);
|
|
|
|
|
if (v1 > val)
|
|
|
|
|
val = v1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
1993-09-16 21:39:00 +04:00
|
|
|
|
|
1993-07-09 00:13:23 +04:00
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
|
{
|
1993-09-16 21:39:00 +04:00
|
|
|
|
if (dirdata[i].names)
|
|
|
|
|
free (dirdata[i].names);
|
1993-07-09 00:13:23 +04:00
|
|
|
|
if (dirdata[i].data)
|
|
|
|
|
free (dirdata[i].data);
|
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|