From 64d460d259acf32a7ac3ca4bcb9b30ed66ffa467 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Tue, 6 Oct 2015 14:28:17 +0200
Subject: [PATCH] corr filename expansion with multiple extensions

---
 pub/lib/defs.hpp        |  1 -
 pub/lib/edif.cpp        |  2 +-
 pub/lib/toplevel.cpp    |  8 ++++----
 pub/trivia/file_ops.cpp | 39 +++++++++++++++++++++++++++------------
 4 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/pub/lib/defs.hpp b/pub/lib/defs.hpp
index 7d7e68e9..0584f337 100644
--- a/pub/lib/defs.hpp
+++ b/pub/lib/defs.hpp
@@ -22,4 +22,3 @@ using std::string;
 using std::vector;
 using std::cout;
 using std::cerr;
-using std::endl;
diff --git a/pub/lib/edif.cpp b/pub/lib/edif.cpp
index 5dfa09af..76047313 100644
--- a/pub/lib/edif.cpp
+++ b/pub/lib/edif.cpp
@@ -123,7 +123,7 @@ void NEdif::show_coord()
         for ( int i=0; i<f->nZ(); i++ ) {
             out += "  z" + S(i) + ": " + f->ZCo[i].str_compact();
         }
-        cout << out << endl;
+        cout << out << "\n";
     }
 }
 
diff --git a/pub/lib/toplevel.cpp b/pub/lib/toplevel.cpp
index 07b0c04d..c561e601 100644
--- a/pub/lib/toplevel.cpp
+++ b/pub/lib/toplevel.cpp
@@ -153,10 +153,10 @@ void CFrida::interactive()
             cmdline = NMacro::readln( NOlm::sel_str() + " > " );
             execute_cmd( cmdline );
         } catch( string& ex ) {
-            cerr << "'" << cmdline << "' failed: " << ex << endl;
+            cerr << "'" << cmdline << "' failed: " << ex << "\n";
             NMacro::clear();
         } catch( const char* ex ) {
-            cerr << "BUG: '" << cmdline << "' failed with unforeseen message: " << ex << endl;
+            cerr << "BUG: '" << cmdline << "' failed with unforeseen message: " << ex << "\n";
             NMacro::clear();
         } catch( ... ) {
             cerr << "BUG: '" << cmdline << "' failed with unforeseen exception\n";
@@ -185,9 +185,9 @@ void CFrida::execute_file( const string fnam )
             }
         }
     } catch( string& ex ) {
-        cerr << "Error in script " << fnam << ", line " << lineno << ": " << ex << endl;
+        cerr << "Error in script " << fnam << ", line " << lineno << ": " << ex << "\n";
     } catch( const char* ex ) {
-        cerr << "BUG: char* error in script " << fnam << ", line " << lineno << ": " << ex << endl;
+        cerr << "BUG: char* error in script " << fnam << ", line " << lineno << ": " << ex << "\n";
     } catch( ... ) {
         cerr << "BUG: catched invalid exception\n";
     }
diff --git a/pub/trivia/file_ops.cpp b/pub/trivia/file_ops.cpp
index 754dc9a3..92889530 100644
--- a/pub/trivia/file_ops.cpp
+++ b/pub/trivia/file_ops.cpp
@@ -67,17 +67,20 @@ string triv::system_read( string cmd, bool debug )
 
 //! Test whether file exists.
 
-bool triv::file_exists( const string& fname )
+bool triv::file_exists( const string& path )
 {
-    struct stat fileInfo;
-    int iStat = stat( fname.c_str(), &fileInfo );
-    if ( iStat == 0 )  // we got attributes, so the file obviously exists
-        return true;
-    else
-        return false;  // either file doesn't exist or we lack permissions
+    struct stat fileStat;
+    if ( stat(path.c_str(), &fileStat) )
+    {
+        return 0;
+    }
+    if ( !S_ISREG(fileStat.st_mode) )
+    {
+        return 0;
+    }
+    return 1;
 }
 
-
 //! Analyses a file name: divides "dir/short.ext" into "dir", "short", "ext".
 
 void triv::fname_divide( const string& fname, string *fdir, string *fshort, string *fext )
@@ -102,8 +105,19 @@ void triv::fname_divide( const string& fname, string *fdir, string *fshort, stri
 string triv::wordexp_unique( const string& s )
 {
     wordexp_t p;
-    if( wordexp( s.c_str(), &p, 0 ) )
-        throw "cannot expand " + s;
+    int err = wordexp( s.c_str(), &p, 0 );
+    switch(err) {
+    case 0:
+        break;
+    case WRDE_BADCHAR:
+        throw "illegal character in '" + s +"'";
+    case WRDE_BADVAL:
+        throw "undefined shell variable in '" + s +"'";
+    case WRDE_SYNTAX:
+        throw "shell syntax error in '" + s +"'";
+    default:
+        throw "completely unexpected error in '" + s + "'";
+    }
     if( p.we_wordc!=1 )
         throw "expansion of " + s + " not unique";
     string ret = p.we_wordv[0];
@@ -123,7 +137,8 @@ vector<string> triv::wordexp_multi( const string& s )
         throw "expansion of " + s + " is empty";
     vector<string> ret;
     for ( size_t i=0; i<p.we_wordc; ++i )
-        ret.push_back( p.we_wordv[i] );
+        if( file_exists( p.we_wordv[i] ) )
+            ret.push_back( p.we_wordv[i] );
     wordfree( &p );
     return ret;
 }
@@ -164,7 +179,7 @@ vector<string> triv::glob_file_list( const string& patterns, const string& exten
     for ( string pat: vPattern ) {
         string fmain, fext;
         fname_divide( pat, nullptr, &fmain, &fext );
-        if ( fext=="" && extensions!="" ) {
+        if ( fext=="" && vExtension.size() ) {
             for( string ext: vExtension )
                 extended_patterns += pat + "." + ext + " ";
         } else {
-- 
GitLab