Home | History | Annotate | Line # | Download | only in diff
      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].
    139    This is a top-level routine; it does everything necessary for diff
    140    on two directories.
    141 
    142    FILEVEC[0].desc == -1 says directory FILEVEC[0] doesn't exist,
    143    but pretend it is empty.  Likewise for FILEVEC[1].
    144 
    145    HANDLE_FILE is a caller-provided subroutine called to handle each file.
    146    It gets five operands: dir and name (rel to original working dir) of file
    147    in dir 0, dir and name pathname of file in dir 1, and the recursion depth.
    148 
    149    For a file that appears in only one of the dirs, one of the name-args
    150    to HANDLE_FILE is zero.
    151 
    152    DEPTH is the current depth in recursion, used for skipping top-level
    153    files by the -S option.
    154 
    155    Returns the maximum of all the values returned by HANDLE_FILE,
    156    or 2 if trouble is encountered in opening files.  */
    157 
    158 int
    159 diff_dirs (filevec, handle_file, depth)
    160      struct file_data const filevec[];
    161      int (*handle_file) PARAMS((char const *, char const *, char const *, char const *, int));
    162      int depth;
    163 {
    164   struct dirdata dirdata[2];
    165   int val = 0;			/* Return value.  */
    166   int i;
    167 
    168   /* Get sorted contents of both dirs.  */
    169   for (i = 0; i < 2; i++)
    170     if (dir_sort (&filevec[i], &dirdata[i]) != 0)
    171       {
    172 	perror_with_name (filevec[i].name);
    173 	val = 2;
    174       }
    175 
    176   if (val == 0)
    177     {
    178       register char const * const *names0 = dirdata[0].names;
    179       register char const * const *names1 = dirdata[1].names;
    180       char const *name0 = filevec[0].name;
    181       char const *name1 = filevec[1].name;
    182 
    183       /* If `-S name' was given, and this is the topmost level of comparison,
    184 	 ignore all file names less than the specified starting name.  */
    185 
    186       if (dir_start_file && depth == 0)
    187 	{
    188 	  while (*names0 && filename_cmp (*names0, dir_start_file) < 0)
    189 	    names0++;
    190 	  while (*names1 && filename_cmp (*names1, dir_start_file) < 0)
    191 	    names1++;
    192 	}
    193 
    194       /* Loop while files remain in one or both dirs.  */
    195       while (*names0 || *names1)
    196 	{
    197 	  /* Compare next name in dir 0 with next name in dir 1.
    198 	     At the end of a dir,
    199 	     pretend the "next name" in that dir is very large.  */
    200 	  int nameorder = (!*names0 ? 1 : !*names1 ? -1
    201 			   : filename_cmp (*names0, *names1));
    202 	  int v1 = (*handle_file) (name0, 0 < nameorder ? 0 : *names0++,
    203 				   name1, nameorder < 0 ? 0 : *names1++,
    204 				   depth + 1);
    205 	  if (v1 > val)
    206 	    val = v1;
    207 	}
    208     }
    209 
    210   for (i = 0; i < 2; i++)
    211     {
    212       if (dirdata[i].names)
    213 	free (dirdata[i].names);
    214       if (dirdata[i].data)
    215 	free (dirdata[i].data);
    216     }
    217 
    218   return val;
    219 }
    220