Commit 85a14794c812859d9fe30a5a7e9fd0e3471e7882

Authored by Jürgen Knödlseder
1 parent 8650677b

Expand environment variables before checking existence of file and directory.

Showing 1 changed file with 6 additions and 4 deletions
src/support/GTools.cpp
... ... @@ -855,7 +855,8 @@ double gammalib::plaw_energy_flux(const double& emin, const double& emax,
855 855 * @param[in] filename File name.
856 856 *
857 857 * Checks if a file exists. If a directory with the same name if found, false
858   - * is returned.
  858 + * is returned. The function expands any environment variable prior to
  859 + * checking.
859 860 ***************************************************************************/
860 861 bool gammalib::file_exists(const std::string& filename)
861 862 {
... ... @@ -866,7 +867,7 @@ bool gammalib::file_exists(const std::string& filename)
866 867 struct stat info;
867 868  
868 869 // Get file information structure
869   - int ret = stat(filename.c_str(), &info);
  870 + int ret = stat(gammalib::expand_env(filename).c_str(), &info);
870 871  
871 872 // Check if file is a regular file
872 873 if (ret == 0 && S_ISREG(info.st_mode)) {
... ... @@ -883,7 +884,8 @@ bool gammalib::file_exists(const std::string& filename)
883 884 *
884 885 * @param[in] dirname Directory name.
885 886 *
886   - * Checks if a directory exists.
  887 + * Checks if a directory exists. The function expands any environment
  888 + * variable prior to checking.
887 889 ***************************************************************************/
888 890 bool gammalib::dir_exists(const std::string& dirname)
889 891 {
... ... @@ -894,7 +896,7 @@ bool gammalib::dir_exists(const std::string& dirname)
894 896 struct stat info;
895 897  
896 898 // Get file information structure
897   - int ret = stat(dirname.c_str(), &info);
  899 + int ret = stat(gammalib::expand_env(dirname).c_str(), &info);
898 900  
899 901 // Check if we have a directory
900 902 if (ret == 0 && S_ISDIR(info.st_mode)) {
... ...