home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / dif115as.zip / DIR.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  7KB  |  242 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. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27. $Header: e:/gnu/diff/RCS/dir.c 1.15.0.2 91/03/12 17:06:27 tho Exp $  */
  28.  
  29. #include "diff.h"
  30.  
  31. #ifndef __STDC__
  32. static int compare_names ();
  33. #endif /* !__STDC__ */
  34.  
  35. /* Read the directory named DIRNAME and return a sorted vector
  36.    of filenames for its contents.  NONEX nonzero means this directory is
  37.    known to be nonexistent, so return zero files.  */
  38.  
  39. struct dirdata
  40. {
  41.   int length;            /* # elements in `files' */
  42.   char **files;            /* Sorted names of files in the dir */
  43. };
  44.  
  45. #ifdef __STDC__
  46. static  struct dirdata dir_sort(char *, int);
  47. static  int compare_names(char * *, char * *);
  48. #endif /* __STDC__ */
  49.  
  50. static struct dirdata
  51. dir_sort (dirname, nonex)
  52.      char *dirname;
  53.      int nonex;
  54. {
  55.   register DIR *reading;
  56.   register struct direct *next;
  57.   struct dirdata dirdata;
  58.  
  59.   /* Address of block containing the files that are described.  */
  60.   char **files;
  61.  
  62.   /* Length of block that `files' points to, measured in files.  */
  63.   int nfiles;
  64.  
  65.   /* Index of first unused in `files'.  */
  66.   int files_index;
  67.  
  68.   if (nonex)
  69.     {
  70.       dirdata.length = 0;
  71.       dirdata.files = 0;
  72.       return dirdata;
  73.     }
  74.  
  75.   /* Open the directory and check for errors.  */
  76.   reading = opendir (dirname);
  77.   if (!reading)
  78.     {
  79.       perror_with_name (dirname);
  80.       dirdata.length = -1;
  81.       return dirdata;
  82.     }
  83.  
  84.   /* Initialize the table of filenames.  */
  85.  
  86.   nfiles = 100;
  87.   files = (char **) xmalloc (nfiles * sizeof (char *));
  88.   files_index = 0;
  89.  
  90.   /* Read the directory entries, and insert the subfiles
  91.      into the `files' table.  */
  92.  
  93.   while (next = readdir (reading))
  94.     {
  95.       /* Ignore the files `.' and `..' */
  96.       if (next->d_name[0] == '.'
  97.       && (next->d_name[1] == 0
  98.           || (next->d_name[1] == '.'
  99.           && next->d_name[2] == 0)))
  100.     continue;
  101.  
  102.       if (files_index == nfiles)
  103.     {
  104.       nfiles *= 2;
  105.       files
  106.         = (char **) xrealloc (files, sizeof (char *) * nfiles);
  107.     }
  108.       files[files_index++] = concat (next->d_name, "", "");
  109.     }
  110.  
  111.   closedir (reading);
  112.  
  113.   /* Sort the table.  */
  114.   qsort (files, files_index, sizeof (char *), compare_names);
  115.  
  116.   /* Return a description of location and length of the table.  */
  117.   dirdata.files = files;
  118.   dirdata.length = files_index;
  119.  
  120.   return dirdata;
  121. }
  122.  
  123. /* Sort the files now in the table.  */
  124.  
  125. static int
  126. compare_names (file1, file2)
  127.      char **file1, **file2;
  128. {
  129.   return strcmp (*file1, *file2);
  130. }
  131.  
  132. /* Compare the contents of two directories named NAME1 and NAME2.
  133.    This is a top-level routine; it does everything necessary for diff
  134.    on two directories.
  135.  
  136.    NONEX1 nonzero says directory NAME1 doesn't exist, but pretend it is
  137.    empty.  Likewise NONEX2.
  138.  
  139.    HANDLE_FILE is a caller-provided subroutine called to handle each file.
  140.    It gets five operands: dir and name (rel to original working dir) of file
  141.    in dir 1, dir and name pathname of file in dir 2, and the recursion depth.
  142.  
  143.    For a file that appears in only one of the dirs, one of the name-args
  144.    to HANDLE_FILE is zero.
  145.  
  146.    DEPTH is the current depth in recursion.
  147.  
  148.    Returns the maximum of all the values returned by HANDLE_FILE,
  149.    or 2 if trouble is encountered in opening files.  */
  150.  
  151. #ifdef MSDOS
  152. int
  153. diff_dirs (name1, name2, handle_file, depth, nonex1, nonex2)
  154.      char *name1, *name2;
  155.      int (*handle_file) (char *, char *, char *, char *, int);
  156.      int depth, nonex1, nonex2;
  157. #else
  158. int
  159. diff_dirs (name1, name2, handle_file, depth, nonex1, nonex2)
  160.      char *name1, *name2;
  161.      int (*handle_file) ();
  162.      int nonex1, nonex2;
  163. #endif
  164. {
  165.   struct dirdata data1, data2;
  166.   register int i1, i2;
  167.   int val = 0;
  168.   int v1;
  169.  
  170.   /* Get sorted contents of both dirs.  */
  171.   data1 = dir_sort (name1, nonex1);
  172.   data2 = dir_sort (name2, nonex2);
  173.   if (data1.length == -1 || data2.length == -1)
  174.     {
  175.       if (data1.length >= 0)
  176.     free (data1.files);
  177.       if (data2.length >= 0)
  178.     free (data2.files);
  179.       return 2;
  180.     }
  181.  
  182.   i1 = 0;
  183.   i2 = 0;
  184.  
  185.   /* If -Sname was specified, and this is the topmost level of comparison,
  186.      ignore all file names less than the specified starting name.  */
  187.  
  188.   if (dir_start_file && depth == 0)
  189.     {
  190.       while (i1 < data1.length && strcmp (data1.files[i1], dir_start_file) < 0)
  191.     i1++;
  192.       while (i2 < data2.length && strcmp (data2.files[i2], dir_start_file) < 0)
  193.     i2++;
  194.     }
  195.  
  196.   /* Loop while files remain in one or both dirs.  */
  197.   while (i1 < data1.length || i2 < data2.length)
  198.     {
  199.       int nameorder;
  200.  
  201.       /* Compare next name in dir 1 with next name in dir 2.
  202.      At the end of a dir,
  203.      pretend the "next name" in that dir is very large.  */
  204.  
  205.       if (i1 == data1.length)
  206.     nameorder = 1;
  207.       else if (i2 == data2.length)
  208.     nameorder = -1;
  209.       else
  210.     nameorder = strcmp (data1.files[i1], data2.files[i2]);
  211.  
  212.       if (nameorder == 0)
  213.     {
  214.       /* We have found a file (or subdir) in common between both dirs.
  215.          Compare the two files.  */
  216.       v1 = (*handle_file) (name1, data1.files[i1], name2, data2.files[i2],
  217.                    depth + 1);
  218.       i1++, i2++;
  219.     }
  220.       if (nameorder < 0)
  221.     {
  222.       /* Next filename in dir 1 is less; that is a file in dir 1 only.  */
  223.       v1 = (*handle_file) (name1, data1.files[i1], name2, 0, depth + 1);
  224.       i1++;
  225.     }
  226.       if (nameorder > 0)
  227.     {
  228.       /* Next filename in dir 2 is less; that is a file in dir 2 only.  */
  229.       v1 = (*handle_file) (name1, 0, name2, data2.files[i2], depth + 1);
  230.       i2++;
  231.     }
  232.       if (v1 > val)
  233.     val = v1;
  234.     }
  235.   if (data1.files)
  236.     free (data1.files);
  237.   if (data2.files)
  238.     free (data2.files);
  239.  
  240.   return val;
  241. }
  242.