home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / DIR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  10.4 KB  |  413 lines

  1. /* Directory hashing for GNU Make.
  2. Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/make/RCS/dir.c'v 3.58.0.1 90/07/17 00:59:03 tho Exp $
  34.  */
  35.  
  36. #include "make.h"
  37.  
  38. #ifdef MSDOS
  39.  
  40. #include "msd_dir.h"
  41. #define D_NAMLEN(d) ((d)->d_namlen)
  42.  
  43. #else /* not MSDOS */
  44.  
  45. #if    defined(USGr3) || defined(DIRENT)
  46.  
  47. #include <dirent.h>
  48. #define direct dirent
  49. #define    D_NAMLEN(d) strlen((d)->d_name)
  50.  
  51. #else    /* Not USGr3 and not DIRENT.  */
  52.  
  53. #define D_NAMLEN(d) ((d)->d_namlen)
  54. #    ifdef    USG
  55. #include "ndir.h"   /* Get ndir.h from the Emacs distribution.  */
  56. #    else    /* Not USG.  */
  57. #include <sys/dir.h>
  58. #    endif    /* USG.  */
  59.  
  60. #endif    /* USGr3 or DIRENT.  */
  61.  
  62. #endif /* not MSDOS */
  63.  
  64. /* Hash table of directories.  */
  65.  
  66. struct directory
  67.   {
  68.     struct directory *next;
  69.     char *name;            /* Name of the directory.  */
  70.     struct dirfile **files;    /* Files in this directory.  */
  71.     DIR *dirstream;        /* Stream reading this directory.  */
  72.   };
  73.  
  74. #ifndef    DIRECTORY_BUCKETS
  75. #define DIRECTORY_BUCKETS 23
  76. #endif
  77.  
  78. static struct directory *directories[DIRECTORY_BUCKETS];
  79.  
  80.  
  81. /* Never have more than this many directories open at once.  */
  82.  
  83. #define MAX_OPEN_DIRECTORIES 10
  84.  
  85. static unsigned int open_directories = 0;
  86.  
  87.  
  88. /* Hash table of files in each directory.  */
  89.  
  90. struct dirfile
  91.   {
  92.     struct dirfile *next;
  93.     char *name;            /* Name of the file.  */
  94.     char impossible;        /* This file is impossible.  */
  95.   };
  96.  
  97. #ifndef    DIRFILE_BUCKETS
  98. #define DIRFILE_BUCKETS 1007
  99. #endif
  100.  
  101. #ifdef MSDOS
  102. static  struct directory *find_directory (char *name);
  103. #endif /* MSDOS */
  104.  
  105. /* Find the directory named NAME and return its `struct directory'.  */
  106.  
  107. static struct directory *
  108. find_directory (name)
  109.      register char *name;
  110. {
  111.   register unsigned int hash = 0;
  112.   register char *p;
  113.   register struct directory *dir;
  114.  
  115.   for (p = name; *p != '\0'; ++p)
  116.     HASH (hash, *p);
  117.   hash %= DIRECTORY_BUCKETS;
  118.  
  119.   for (dir = directories[hash]; dir != 0; dir = dir->next)
  120.     if (streq (dir->name, name))
  121.       break;
  122.  
  123.   if (dir == 0)
  124.     {
  125.       /* The directory was not found.  Create a new entry
  126.      for it and start its directory stream reading.  */
  127.       dir = (struct directory *) xmalloc (sizeof (struct directory));
  128.       dir->next = directories[hash];
  129.       directories[hash] = dir;
  130.       dir->name = savestring (name, p - name);
  131.       dir->dirstream = opendir (name);
  132.       if (dir->dirstream == 0)
  133.     /* Couldn't open the directory.  Mark this by
  134.        setting the `files' member to a nil pointer.  */
  135.     dir->files = 0;
  136.       else
  137.     {
  138.       /* Allocate an array of hash buckets for files and zero it.  */
  139.       dir->files = (struct dirfile **)
  140.         xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
  141.       bzero ((char *) dir->files,
  142.          sizeof (struct dirfile) * DIRFILE_BUCKETS);
  143.  
  144.       /* Keep track of how many directories are open.  */
  145.       ++open_directories;
  146.       if (open_directories == MAX_OPEN_DIRECTORIES)
  147.         /* Read the entire directory and then close it.  */
  148.         (void) dir_file_exists_p (dir->name, (char *) 0);
  149.     }
  150.     }
  151.  
  152.   return dir;
  153. }
  154.  
  155. /* Return 1 if the name FILENAME in directory DIRNAME
  156.    is entered in the dir hash table.
  157.    FILENAME must contain no slashes.  */
  158.  
  159. int
  160. dir_file_exists_p (dirname, filename)
  161.      register char *dirname;
  162.      register char *filename;
  163. {
  164.   register unsigned int hash;
  165.   register char *p;
  166.   register struct directory *dir;
  167.   register struct dirfile *df;
  168.   register struct direct *d;
  169.  
  170.   dir = find_directory (dirname);
  171.  
  172.   if (dir->files == 0)
  173.     /* The directory could not be opened.  */
  174.     return 0;
  175.  
  176.   hash = 0;
  177.   if (filename != 0)
  178.     {
  179.       if (*filename == '\0')
  180.     /* Checking if the directory exists.  */
  181.     return 1;
  182.  
  183.       for (p = filename; *p != '\0'; ++p)
  184.     HASH (hash, *p);
  185.       hash %= DIRFILE_BUCKETS;
  186.  
  187.       /* Search the list of hashed files.  */
  188.  
  189.       for (df = dir->files[hash]; df != 0; df = df->next)
  190.     if (streq (df->name, filename))
  191.       return !df->impossible;
  192.     }
  193.  
  194.   /* The file was not found in the hashed list.
  195.      Try to read the directory further.  */
  196.  
  197.   if (dir->dirstream == 0)
  198.     /* The directory has been all read in.  */
  199.     return 0;
  200.  
  201.   while ((d = readdir (dir->dirstream)) != 0)
  202.     {
  203.       /* Enter the file in the hash table.  */
  204.       register unsigned int newhash = 0;
  205.       register unsigned int i;
  206.       for (i = 0; i < D_NAMLEN(d); ++i)
  207.     HASH (newhash, d->d_name[i]);
  208.       newhash %= DIRFILE_BUCKETS;
  209.       df = (struct dirfile *) xmalloc (sizeof (struct dirfile));
  210.       df->next = dir->files[newhash];
  211.       dir->files[newhash] = df;
  212.       df->name = savestring (d->d_name, D_NAMLEN(d));
  213.       df->impossible = 0;
  214.  
  215.       /* Check if the name matches the one we're searching for.  */
  216.       if (filename != 0
  217.       && newhash == hash && streq (d->d_name, filename))
  218.     return 1;
  219.     }
  220.  
  221.   /* If the directory has been completely read in,
  222.      close the stream and reset the pointer to nil.  */
  223.   if (d == 0)
  224.     {
  225.       --open_directories;
  226.       closedir (dir->dirstream);
  227.       dir->dirstream = 0;
  228.     }
  229.  
  230.   return 0;
  231. }
  232.  
  233. /* Return 1 if the file named NAME exists.  */
  234.  
  235. int
  236. file_exists_p (name)
  237.      register char *name;
  238. {
  239.   char *dirend;
  240.   char *dirname;
  241.  
  242. #ifndef    NO_ARCHIVES
  243.   if (ar_name (name))
  244.     return ar_member_date (name) != (time_t) -1;
  245. #endif
  246.  
  247.   dirend = rindex (name, '/');
  248.   if (dirend == 0)
  249.     return dir_file_exists_p (".", name);
  250.  
  251.   dirname = (char *) alloca (dirend - name + 1);
  252.   bcopy (name, dirname, dirend - name);
  253.   dirname[dirend - name] = '\0';
  254.   return dir_file_exists_p (dirname, dirend + 1);
  255. }
  256.  
  257. /* Mark FILENAME as `impossible' for `file_impossible_p'.
  258.    This means an attempt has been made to search for FILENAME
  259.    as an intermediate file, and it has failed.  */
  260.  
  261. void
  262. file_impossible (filename)
  263.      register char *filename;
  264. {
  265.   char *dirend;
  266.   register char *p = filename;
  267.   register unsigned int hash;
  268.   register struct directory *dir;
  269.   register struct dirfile *new;
  270.  
  271.   dirend = rindex (p, '/');
  272.   if (dirend == 0)
  273.     dir = find_directory (".");
  274.   else
  275.     {
  276.       char *dirname = (char *) alloca (dirend - p + 1);
  277.       bcopy (p, dirname, dirend - p);
  278.       dirname[dirend - p] = '\0';
  279.       dir = find_directory (dirname);
  280.       filename = p = dirend + 1;
  281.     }
  282.  
  283.   for (hash = 0; *p != '\0'; ++p)
  284.     HASH (hash, *p);
  285.   hash %= DIRFILE_BUCKETS;
  286.  
  287.   if (dir->files == 0)
  288.     {
  289.       /* The directory was not opened; we must allocate the hash buckets.  */
  290.       dir->files = (struct dirfile **)
  291.     xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
  292.       bzero ((char *) dir->files, sizeof (struct dirfile) * DIRFILE_BUCKETS);
  293.     }
  294.  
  295.   /* Make a new entry and put it in the table.  */
  296.  
  297.   new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
  298.   new->next = dir->files[hash];
  299.   dir->files[hash] = new;
  300.   new->name = savestring (filename, strlen (filename));
  301.   new->impossible = 1;
  302. }
  303.  
  304. /* Return nonzero if FILENAME has been marked impossible.  */
  305.  
  306. int
  307. file_impossible_p (filename)
  308.      char *filename;
  309. {
  310.   char *dirend;
  311.   register char *p = filename;
  312.   register unsigned int hash;
  313.   register struct directory *dir;
  314.   register struct dirfile *next;
  315.  
  316.   dirend = rindex (filename, '/');
  317.   if (dirend == 0)
  318.     dir = find_directory (".");
  319.   else
  320.     {
  321.       char *dirname = (char *) alloca (dirend - filename + 1);
  322.       bcopy (p, dirname, dirend - p);
  323.       dirname[dirend - p] = '\0';
  324.       dir = find_directory (dirname);
  325.       p = dirend + 1;
  326.     }
  327.  
  328.   if (dir->files == 0)
  329.     /* There are no files entered for this directory.  */
  330.     return 0;
  331.  
  332.   for (hash = 0; *p != '\0'; ++p)
  333.     HASH (hash, *p);
  334.   hash %= DIRFILE_BUCKETS;
  335.  
  336.   for (next = dir->files[hash]; next != 0; next = next->next)
  337.     if (streq (filename, next->name))
  338.       return next->impossible;
  339.  
  340.   return 0;
  341. }
  342.  
  343. /* Return the already allocated name in the
  344.    directory hash table that matches DIR.  */
  345.  
  346. char *
  347. dir_name (dir)
  348.      char *dir;
  349. {
  350.   return find_directory (dir)->name;
  351. }
  352.  
  353. /* Print the data base of directories.  */
  354.  
  355. void
  356. print_dir_data_base ()
  357. {
  358.   register unsigned int i, dirs, files, impossible;
  359.   register struct directory *dir;
  360.  
  361.   puts ("\n# Directories\n");
  362.  
  363.   dirs = files = impossible = 0;
  364.   for (i = 0; i < DIRECTORY_BUCKETS; ++i)
  365.     for (dir = directories[i]; dir != 0; dir = dir->next)
  366.       {
  367.     ++dirs;
  368.     if (dir->files == 0)
  369.       printf ("# %s: could not be opened.\n", dir->name);
  370.     else
  371.       {
  372.         register unsigned int f = 0, im = 0;
  373.         register unsigned int j;
  374.         register struct dirfile *df;
  375.         for (j = 0; j < DIRFILE_BUCKETS; ++j)
  376.           for (df = dir->files[j]; df != 0; df = df->next)
  377.         if (df->impossible)
  378.           ++im;
  379.         else
  380.           ++f;
  381.         printf ("# %s: ", dir->name);
  382.         if (f == 0)
  383.           fputs ("No", stdout);
  384.         else
  385.           printf ("%u", f);
  386.         fputs (" files, ", stdout);
  387.         if (im == 0)
  388.           fputs ("no", stdout);
  389.         else
  390.           printf ("%u", im);
  391.         fputs (" impossibilities", stdout);
  392.         if (dir->dirstream == 0)
  393.           puts (".");
  394.         else
  395.           puts (" so far.");
  396.         files += f;
  397.         impossible += im;
  398.       }
  399.       }
  400.  
  401.   fputs ("\n# ", stdout);
  402.   if (files == 0)
  403.     fputs ("No", stdout);
  404.   else
  405.     printf ("%u", files);
  406.   fputs (" files, ", stdout);
  407.   if (impossible == 0)
  408.     fputs ("no", stdout);
  409.   else
  410.     printf ("%u", impossible);
  411.   printf (" impossibilities in %u directories.\n", dirs);
  412. }
  413.