Back to index

tetex-bin  3.0
readable.c
Go to the documentation of this file.
00001 /* readable.c: check if a filename is a readable non-directory file.
00002 
00003 Copyright (C) 1993, 95, 96 Karl Berry.
00004 Copyright (C) 2000 Olaf Weber.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Library General Public
00008 License as published by the Free Software Foundation; either
00009 version 2 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Library General Public License for more details.
00015 
00016 You should have received a copy of the GNU Library General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #include <kpathsea/config.h>
00021 #include <kpathsea/c-stat.h>
00022 #include <kpathsea/pathsearch.h>
00023 #include <kpathsea/readable.h>
00024 #include <kpathsea/tex-hush.h>
00025 #include <kpathsea/truncate.h>
00026 
00027 
00028 /* If access can read FN, run stat (assigning to stat buffer ST) and
00029    check that fn is not a directory.  Don't check for just being a
00030    regular file, as it is potentially useful to read fifo's or some
00031    kinds of devices.  */
00032 
00033 #ifdef __DJGPP__
00034 /* `stat' is way too expensive for such a simple job.  */
00035 #define READABLE(fn, st) \
00036   (access (fn, R_OK) == 0 && access (fn, D_OK) == -1)
00037 #elif WIN32
00038 /* Warning: st must be an unsigned int under Win32 */
00039 static boolean
00040 READABLE(const_string fn, unsigned int st)
00041 {
00042   if ((st = GetFileAttributes(fn)) != 0xFFFFFFFF) {
00043       /* succeeded */
00044       errno = 0;
00045   } else {
00046       switch(GetLastError()) {
00047       case ERROR_BUFFER_OVERFLOW:
00048          errno = ENAMETOOLONG;
00049          break;
00050       case ERROR_ACCESS_DENIED:
00051          errno = EACCES;
00052          break;
00053       default :
00054           errno = EIO;             /* meaningless, will make ret=NULL later */
00055          break;
00056       }
00057   }
00058   return ((st != 0xFFFFFFFF) &&
00059                 !(st & FILE_ATTRIBUTE_DIRECTORY));
00060 }
00061 #else
00062 #define READABLE(fn, st) \
00063   (access (fn, R_OK) == 0 && stat (fn, &(st)) == 0 && !S_ISDIR (st.st_mode))
00064 #endif
00065 
00066 /* POSIX invented the brain-damage of not necessarily truncating
00067    filename components; the system's behavior is defined by the value of
00068    the symbol _POSIX_NO_TRUNC, but you can't change it dynamically!
00069    
00070    Generic const return warning.  See extend-fname.c.  */
00071 
00072 string
00073 kpse_readable_file P1C(const_string, name)
00074 {
00075   string ret;
00076 
00077 #ifdef WIN32
00078   unsigned int st = 0;
00079 #else /* ! WIN32 */
00080   struct stat st;
00081 #endif
00082 
00083   kpse_normalize_path((string)name);
00084   if (READABLE (name, st)) {
00085       ret = (string) name;
00086 #ifdef ENAMETOOLONG
00087   } else if (errno == ENAMETOOLONG) {
00088       ret = kpse_truncate_filename (name);
00089 
00090       /* Perhaps some other error will occur with the truncated name, so
00091          let's call access again.  */
00092       if (!READABLE (ret, st)) { /* Failed.  */
00093           if (ret != name) free (ret);
00094           ret = NULL;
00095       }
00096 #endif /* ENAMETOOLONG */
00097   } else { /* Some other error.  */
00098       if (errno == EACCES) { /* Maybe warn them if permissions are bad.  */
00099           if (!kpse_tex_hush ("readable")) {
00100               perror (name);
00101           }
00102       }
00103       ret = NULL;
00104   }
00105   return ret;
00106 }