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 */ 17 18 #include "diff.h" 19 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. */ 24 25 struct dirdata 26 { 27 char const **names; /* Sorted names of files in dir, 0-terminated. */ 28 char *data; /* Allocated storage for file names. */ 29 }; 30 31 static int compare_names PARAMS((void const *, void const *)); 32 static int dir_sort PARAMS((struct file_data const *, struct dirdata *)); 33 34 #ifdef _WIN32 35 #define CLOSEDIR_VOID 1 36 #endif 37 38 static int 39 dir_sort (dir, dirdata) 40 struct file_data const *dir; 41 struct dirdata *dirdata; 42 { 43 register struct dirent *next; 44 register int i; 45 46 /* Address of block containing the files that are described. */ 47 char const **names; 48 49 /* Number of files in directory. */ 50 size_t nnames; 51 52 /* Allocated and used storage for file name data. */ 53 char *data; 54 size_t data_alloc, data_used; 55 56 dirdata->names = 0; 57 dirdata->data = 0; 58 nnames = 0; 59 data = 0; 60 61 if (dir->desc != -1) 62 { 63 /* Open the directory and check for errors. */ 64 register DIR *reading = CVS_OPENDIR (dir->name); 65 if (!reading) 66 return -1; 67 68 /* Initialize the table of filenames. */ 69 70 data_alloc = max (1, (size_t) dir->stat.st_size); 71 data_used = 0; 72 dirdata->data = data = xmalloc (data_alloc); 73 74 /* Read the directory entries, and insert the subfiles 75 into the `data' table. */ 76 77 while ((errno = 0, (next = CVS_READDIR (reading)) != 0)) 78 { 79 char *d_name = next->d_name; 80 size_t d_size = NAMLEN (next) + 1; 81 82 /* Ignore the files `.' and `..' */ 83 if (d_name[0] == '.' 84 && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0))) 85 continue; 86 87 if (excluded_filename (d_name)) 88 continue; 89 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); 93 data_used += d_size; 94 nnames++; 95 } 96 if (errno) 97 { 98 int e = errno; 99 CVS_CLOSEDIR (reading); 100 errno = e; 101 return -1; 102 } 103 #if CLOSEDIR_VOID 104 CVS_CLOSEDIR (reading); 105 #else 106 if (CVS_CLOSEDIR (reading) != 0) 107 return -1; 108 #endif 109 } 110 111 /* Create the `names' table from the `data' table. */ 112 dirdata->names = names = (char const **) xmalloc (sizeof (char *) 113 * (nnames + 1)); 114 for (i = 0; i < nnames; i++) 115 { 116 names[i] = data; 117 data += strlen (data) + 1; 118 } 119 names[nnames] = 0; 120 121 /* Sort the table. */ 122 qsort (names, nnames, sizeof (char *), compare_names); 123 124 return 0; 125 } 126 127 /* Sort the files now in the table. */ 128 129 static int 130 compare_names (file1, file2) 131 void const *file1, *file2; 132 { 133 return filename_cmp (* (char const *const *) file1, 134 * (char const *const *) file2); 135 } 136 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 139 on two directories. 140 141 FILEVEC[0].desc == -1 says directory FILEVEC[0] doesn't exist, 142 but pretend it is empty. Likewise for FILEVEC[1]. 143 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. 147 148 For a file that appears in only one of the dirs, one of the name-args 149 to HANDLE_FILE is zero. 150 151 DEPTH is the current depth in recursion, used for skipping top-level 152 files by the -S option. 153 154 Returns the maximum of all the values returned by HANDLE_FILE, 155 or 2 if trouble is encountered in opening files. */ 156 157 int 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)); 161 int depth; 162 { 163 struct dirdata dirdata[2]; 164 int val = 0; /* Return value. */ 165 int i; 166 167 /* Get sorted contents of both dirs. */ 168 for (i = 0; i < 2; i++) 169 if (dir_sort (&filevec[i], &dirdata[i]) != 0) 170 { 171 perror_with_name (filevec[i].name); 172 val = 2; 173 } 174 175 if (val == 0) 176 { 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; 181 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. */ 184 185 if (dir_start_file && depth == 0) 186 { 187 while (*names0 && filename_cmp (*names0, dir_start_file) < 0) 188 names0++; 189 while (*names1 && filename_cmp (*names1, dir_start_file) < 0) 190 names1++; 191 } 192 193 /* Loop while files remain in one or both dirs. */ 194 while (*names0 || *names1) 195 { 196 /* Compare next name in dir 0 with next name in dir 1. 197 At the end of a dir, 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++, 203 depth + 1); 204 if (v1 > val) 205 val = v1; 206 } 207 } 208 209 for (i = 0; i < 2; i++) 210 { 211 if (dirdata[i].names) 212 free (dirdata[i].names); 213 if (dirdata[i].data) 214 free (dirdata[i].data); 215 } 216 217 return val; 218 } 219