home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / kpathsea / readable.c < prev    next >
C/C++ Source or Header  |  1994-03-16  |  2KB  |  72 lines

  1. /* readable.c: check if a filename is a readable regular file.
  2.  
  3. Copyright (C) 1993 Karl Berry.
  4.  
  5. This program 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 2, or (at your option)
  8. any later version.
  9.  
  10. This program 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 this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-stat.h>
  22. #include <kpathsea/readable.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 a regular file.  */
  28.  
  29. #define READABLE(fn, st) \
  30.   (access (fn, R_OK) == 0 && stat (fn, &(st)) == 0 && S_ISREG (st.st_mode))
  31.  
  32.  
  33. /* POSIX invented the brain-damage of not necessarily truncating
  34.    filename components; the system's behavior is defined by the value of
  35.    the symbol _POSIX_NO_TRUNC, but you can't change it dynamically!
  36.    
  37.    Generic const return warning.  See extend-fname.c.  */
  38.  
  39. string
  40. kpse_readable_file P1C(const_string, name)
  41. {
  42.   struct stat st;
  43.   string ret;
  44.   
  45.   if (READABLE (name, st))
  46.     ret = (string) name;
  47.  
  48. #ifdef ENAMETOOLONG
  49.   else if (errno == ENAMETOOLONG)
  50.     {
  51.       ret = kpse_truncate_filename (name);
  52.  
  53.       /* Perhaps some other error will occur with the truncated name, so
  54.          let's call access again.  */
  55.       if (!READABLE (ret, st))
  56.         { /* Failed.  */
  57.           if (ret != name) free (ret);
  58.           ret = NULL;
  59.         }
  60.     }
  61. #endif
  62.  
  63.   else /* Some other error.  */
  64.     { 
  65.       if (errno == EACCES) /* Warn them if permissions are bad.  */
  66.         perror (name);
  67.       ret = NULL;
  68.     }
  69.   
  70.   return ret;
  71. }
  72.