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