summaryrefslogtreecommitdiffstats
path: root/recursive.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:30:18 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:30:18 +0000
commite2010b31ef4137540c264f2fb69f8f2660a6f7fc (patch)
treef6dc13301edd0ff87720cbb52123d6b0832b293b /recursive.cc
parentAdding upstream version 1.2~pre2. (diff)
downloadzutils-e2010b31ef4137540c264f2fb69f8f2660a6f7fc.tar.xz
zutils-e2010b31ef4137540c264f2fb69f8f2660a6f7fc.zip
Adding upstream version 1.2~pre3.upstream/1.2_pre3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r--recursive.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/recursive.cc b/recursive.cc
new file mode 100644
index 0000000..09aa095
--- /dev/null
+++ b/recursive.cc
@@ -0,0 +1,61 @@
+/* Zutils - Utilities dealing with compressed files
+ Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+bool next_filename( std::list< std::string > & filenames,
+ std::string & input_filename, bool & error,
+ const bool recursive, const bool ignore_stdin = false,
+ const bool no_messages = false )
+ {
+ while( !filenames.empty() )
+ {
+ input_filename = filenames.front();
+ filenames.pop_front();
+ if( input_filename.empty() || input_filename == "-" )
+ {
+ if( ignore_stdin ) continue;
+ input_filename.clear(); return true;
+ }
+ if( recursive )
+ {
+ struct stat st;
+ if( stat( input_filename.c_str(), &st ) == 0 && S_ISDIR( st.st_mode ) )
+ {
+ DIR * const dirp = opendir( input_filename.c_str() );
+ if( !dirp )
+ {
+ if( !no_messages )
+ show_error2( "Can't open directory", input_filename.c_str() );
+ error = true; continue;
+ }
+ std::list< std::string > tmp_list;
+ while( true )
+ {
+ const struct dirent * const entryp = readdir( dirp );
+ if( !entryp ) { closedir( dirp ); break; }
+ std::string tmp_name( entryp->d_name );
+ if( tmp_name != "." && tmp_name != ".." )
+ tmp_list.push_back( input_filename + "/" + tmp_name );
+ }
+ filenames.splice( filenames.begin(), tmp_list );
+ continue;
+ }
+ }
+ return true;
+ }
+ input_filename.clear();
+ return false;
+ }