home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / dir.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  6KB  |  217 lines

  1. /* Read, sort and compare two directories.  Used for GNU DIFF.
  2.    Copyright (C) 1988, 1989 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 1, 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. static int compare_names ();
  23.  
  24. /* Read the directory named DIRNAME and return a sorted vector
  25.    of filenames for its contents.  NONEX nonzero means this directory is
  26.    known to be nonexistent, so return zero files.  */
  27.  
  28. struct dirdata
  29. {
  30.   int length;            /* # elements in `files' */
  31.   char **files;            /* Sorted names of files in the dir */
  32. };
  33.  
  34. static struct dirdata
  35. dir_sort (dirname, nonex)
  36.      char *dirname;
  37.      int nonex;
  38. {
  39.   register DIR *reading;
  40.   register struct direct *next;
  41.   struct dirdata dirdata;
  42.   int compare_names ();
  43.  
  44.   /* Address of block containing the files that are described.  */
  45.   char **files;
  46.  
  47.   /* Length of block that `files' points to, measured in files.  */
  48.   int nfiles;
  49.  
  50.   /* Index of first unused in `files'.  */
  51.   int files_index;
  52.  
  53.   if (nonex)
  54.     {
  55.       dirdata.length = 0;
  56.       dirdata.files = 0;
  57.       return dirdata;
  58.     }
  59.  
  60.   /* Open the directory and check for errors.  */
  61.   reading = opendir (dirname);
  62.   if (!reading)
  63.     {
  64.       perror_with_name (dirname);
  65.       dirdata.length = -1;
  66.       return dirdata;
  67.     }
  68.  
  69.   /* Initialize the table of filenames.  */
  70.  
  71.   nfiles = 100;
  72.   files = (char **) xmalloc (nfiles * sizeof (char *));
  73.   files_index = 0;
  74.  
  75.   /* Read the directory entries, and insert the subfiles
  76.      into the `files' table.  */
  77.  
  78.   while (next = readdir (reading))
  79.     {
  80.       /* Ignore the files `.' and `..' */
  81.       if (next->d_name[0] == '.'
  82.       && (next->d_name[1] == 0
  83.           || (next->d_name[1] == '.'
  84.           && next->d_name[2] == 0)))
  85.     continue;
  86.  
  87.       if (files_index == nfiles)
  88.     {
  89.       nfiles *= 2;
  90.       files
  91.         = (char **) xrealloc (files, sizeof (char *) * nfiles);
  92.     }
  93.       files[files_index++] = concat (next->d_name, "", "");
  94.     }
  95.  
  96.   closedir (reading);
  97.  
  98.   /* Sort the table.  */
  99.   qsort (files, files_index, sizeof (char *), compare_names);
  100.  
  101.   /* Return a description of location and length of the table.  */
  102.   dirdata.files = files;
  103.   dirdata.length = files_index;
  104.  
  105.   return dirdata;
  106. }
  107.  
  108. /* Sort the files now in the table.  */
  109.  
  110. static int
  111. compare_names (file1, file2)
  112.      char **file1, **file2;
  113. {
  114.   return strcmp (*file1, *file2);
  115. }
  116.  
  117. /* Compare the contents of two directories named NAME1 and NAME2.
  118.    This is a top-level routine; it does everything necessary for diff
  119.    on two directories.
  120.  
  121.    NONEX1 nonzero says directory NAME1 doesn't exist, but pretend it is
  122.    empty.  Likewise NONEX2.
  123.  
  124.    HANDLE_FILE is a caller-provided subroutine called to handle each file.
  125.    It gets five operands: dir and name (rel to original working dir) of file
  126.    in dir 1, dir and name pathname of file in dir 2, and the recursion depth.
  127.  
  128.    For a file that appears in only one of the dirs, one of the name-args
  129.    to HANDLE_FILE is zero.
  130.  
  131.    DEPTH is the current depth in recursion.
  132.  
  133.    Returns the maximum of all the values returned by HANDLE_FILE,
  134.    or 2 if trouble is encountered in opening files.  */
  135.  
  136. int
  137. diff_dirs (name1, name2, handle_file, depth, nonex1, nonex2)
  138.      char *name1, *name2;
  139.      int (*handle_file) ();
  140.      int nonex1, nonex2;
  141. {
  142.   struct dirdata data1, data2;
  143.   register int i1, i2;
  144.   int val = 0;
  145.   int v1;
  146.  
  147.   /* Get sorted contents of both dirs.  */
  148.   data1 = dir_sort (name1, nonex1);
  149.   data2 = dir_sort (name2, nonex2);
  150.   if (data1.length == -1 || data2.length == -1)
  151.     {
  152.       if (data1.length >= 0)
  153.     free (data1.files);
  154.       if (data2.length >= 0)
  155.     free (data2.files);
  156.       return 2;
  157.     }
  158.  
  159.   i1 = 0;
  160.   i2 = 0;
  161.  
  162.   /* If -Sname was specified, and this is the topmost level of comparison,
  163.      ignore all file names less than the specified starting name.  */
  164.  
  165.   if (dir_start_file && depth == 0)
  166.     {
  167.       while (i1 < data1.length && strcmp (data1.files[i1], dir_start_file) < 0)
  168.     i1++;
  169.       while (i2 < data2.length && strcmp (data2.files[i2], dir_start_file) < 0)
  170.     i2++;
  171.     }
  172.  
  173.   /* Loop while files remain in one or both dirs.  */
  174.   while (i1 < data1.length || i2 < data2.length)
  175.     {
  176.       int nameorder;
  177.  
  178.       /* Compare next name in dir 1 with next name in dir 2.
  179.      At the end of a dir,
  180.      pretend the "next name" in that dir is very large.  */
  181.  
  182.       if (i1 == data1.length)
  183.     nameorder = 1;
  184.       else if (i2 == data2.length)
  185.     nameorder = -1;
  186.       else
  187.     nameorder = strcmp (data1.files[i1], data2.files[i2]);
  188.  
  189.       if (nameorder == 0)
  190.     {
  191.       /* We have found a file (or subdir) in common between both dirs.
  192.          Compare the two files.  */
  193.       v1 = (*handle_file) (name1, data1.files[i1], name2, data2.files[i2],
  194.                    depth + 1);
  195.       i1++, i2++;
  196.     }
  197.       if (nameorder < 0)
  198.     {
  199.       /* Next filename in dir 1 is less; that is a file in dir 1 only.  */
  200.       v1 = (*handle_file) (name1, data1.files[i1], name2, 0, depth + 1);
  201.       i1++;
  202.     }
  203.       if (nameorder > 0)
  204.     {
  205.       /* Next filename in dir 2 is less; that is a file in dir 2 only.  */
  206.       v1 = (*handle_file) (name1, 0, name2, data2.files[i2], depth + 1);
  207.       i2++;
  208.     }
  209.       if (v1 > val)
  210.     val = v1;
  211.     }
  212.   free (data1.files);
  213.   free (data2.files);
  214.  
  215.   return val;
  216. }
  217.