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

  1. /* readable.c: check if a filename is a readable non-directory file.
  2.  
  3. Copyright (C) 1993, 95, 96 Karl Berry.
  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. #include <kpathsea/c-stat.h>
  21. #include <kpathsea/readable.h>
  22. #include <kpathsea/tex-hush.h>
  23. #include <kpathsea/truncate.h>
  24.  
  25.  
  26. /* If access can read FN, run stat (assigning to stat buffer ST) and
  27.    check that fn is not a directory.  Don't check for just being a
  28.    regular file, as it is potentially useful to read fifo's or some
  29.    kinds of devices.  */
  30.  
  31. #ifdef __DJGPP__
  32. /* `stat' is way too expensive for such a simple job.  */
  33. #define READABLE(fn, st) \
  34.   (access (fn, R_OK) == 0 && access (fn, D_OK) == -1)
  35. #elif WIN32
  36. #define READABLE(fn, st) \
  37.   (GetFileAttributes(fn) != 0xFFFFFFFF && \
  38.    !(GetFileAttributes(fn) & FILE_ATTRIBUTE_DIRECTORY))
  39. #else
  40. #define READABLE(fn, st) \
  41.   (access (fn, R_OK) == 0 && stat (fn, &(st)) == 0 && !S_ISDIR (st.st_mode))
  42. #endif
  43.  
  44. /* POSIX invented the brain-damage of not necessarily truncating
  45.    filename components; the system's behavior is defined by the value of
  46.    the symbol _POSIX_NO_TRUNC, but you can't change it dynamically!
  47.    
  48.    Generic const return warning.  See extend-fname.c.  */
  49.  
  50. string
  51. kpse_readable_file P1C(const_string, name)
  52. {
  53.   struct stat st;
  54.   string ret;
  55.   
  56.   if (READABLE (name, st)) {
  57.     ret = (string) name;
  58.  
  59. #ifdef ENAMETOOLONG
  60.   } else if (errno == ENAMETOOLONG) {
  61.     ret = kpse_truncate_filename (name);
  62.  
  63.     /* Perhaps some other error will occur with the truncated name, so
  64.        let's call access again.  */
  65.     if (!READABLE (ret, st))
  66.       { /* Failed.  */
  67.         if (ret != name) free (ret);
  68.         ret = NULL;
  69.       }
  70. #endif /* ENAMETOOLONG */
  71.  
  72.   } else { /* Some other error.  */
  73.     if (errno == EACCES) { /* Maybe warn them if permissions are bad.  */
  74.       if (!kpse_tex_hush ("readable")) {
  75.         perror (name);
  76.       }
  77.     }
  78.     ret = NULL;
  79.   }
  80.   
  81.   return ret;
  82. }
  83.