home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / dir.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  3KB  |  94 lines

  1. /* dir.c: directory operations.
  2.  
  3. Copyright (C) 1992, 93, 94, 95 Free Software Foundation, Inc.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library 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 GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-dir.h>
  22. #include <kpathsea/c-stat.h>
  23. #include <kpathsea/hash.h>
  24.  
  25.  
  26. /* Return true if FN is a directory or a symlink to a directory,
  27.    false if not. */
  28.  
  29. boolean
  30. dir_p P1C(const_string, fn)
  31. {
  32. #ifdef WIN32
  33.   int fa = GetFileAttributes(fn);
  34.   return (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
  35. #else
  36.   struct stat stats;
  37.   return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
  38. #endif
  39. }
  40.  
  41. #ifndef WIN32
  42.  
  43. /* Return -1 if FN isn't a directory, else its number of links.
  44.    Duplicate the call to stat; no need to incur overhead of a function
  45.    call for that little bit of cleanliness. */
  46.  
  47. int
  48. dir_links P1C(const_string, fn)
  49. {
  50.   static hash_table_type link_table;
  51.   string *hash_ret;
  52.   long ret;
  53.   
  54.   if (link_table.size == 0)
  55.     link_table = hash_create (457);
  56.  
  57. #ifdef KPSE_DEBUG
  58.   /* This is annoying, but since we're storing integers as pointers, we
  59.      can't print them as strings.  */
  60.   if (KPSE_DEBUG_P (KPSE_DEBUG_HASH))
  61.     kpse_debug_hash_lookup_int = true;
  62. #endif
  63.  
  64.   hash_ret = hash_lookup (link_table, fn);
  65.   
  66. #ifdef KPSE_DEBUG
  67.   if (KPSE_DEBUG_P (KPSE_DEBUG_HASH))
  68.     kpse_debug_hash_lookup_int = false;
  69. #endif
  70.  
  71.   /* Have to cast the int we need to/from the const_string that the hash
  72.      table stores for values. Let's hope an int fits in a pointer.  */
  73.   if (hash_ret)
  74.     ret = (long) *hash_ret;
  75.   else
  76.     {
  77.       struct stat stats;
  78.       ret = stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode)
  79.             ? stats.st_nlink : -1;
  80.  
  81.       /* It's up to us to copy the value.  */
  82.       hash_insert (&link_table, xstrdup (fn), (const_string) ret);
  83.       
  84. #ifdef KPSE_DEBUG
  85.       if (KPSE_DEBUG_P (KPSE_DEBUG_STAT))
  86.         DEBUGF2 ("dir_links(%s) => %ld\n", fn, ret);
  87. #endif
  88.     }
  89.  
  90.   return ret;
  91. }
  92.  
  93. #endif /* !WIN32 */
  94.