dir.c 6.0 KB

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