"Fossies" - the Fresh Open Source Software Archive

Member "zutils-1.10/recursive.cc" (5 Jan 2021, 4298 Bytes) of package /linux/privat/zutils-1.10.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "recursive.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes reports: 1.10-rc1_vs_1.10 or 1.9_vs_1.10.

    1 /* Zutils - Utilities dealing with compressed files
    2    Copyright (C) 2009-2021 Antonio Diaz Diaz.
    3 
    4    This program is free software: you can redistribute it and/or modify
    5    it under the terms of the GNU General Public License as published by
    6    the Free Software Foundation, either version 2 of the License, or
    7    (at your option) any later version.
    8 
    9    This program is distributed in the hope that it will be useful,
   10    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12    GNU General Public License for more details.
   13 
   14    You should have received a copy of the GNU General Public License
   15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 */
   17 
   18 /* Returns true if full_name is a regular file with an enabled extension
   19    or (a link to) a directory. */
   20 bool test_full_name( const std::string & full_name, const struct stat * stp,
   21                      const bool follow )
   22   {
   23   struct stat st, st2;
   24   if( follow && stat( full_name.c_str(), &st ) != 0 ) return false;
   25   if( !follow && lstat( full_name.c_str(), &st ) != 0 ) return false;
   26   if( S_ISREG( st.st_mode ) )           // regular file
   27     return enabled_format( extension_format( extension_index( full_name ) ) );
   28   if( !S_ISDIR( st.st_mode ) ) return false;
   29 
   30   std::string prev_dir( full_name );
   31   bool loop = ( stp && st.st_ino == stp->st_ino && st.st_dev == stp->st_dev );
   32   if( !loop )
   33     for( unsigned i = prev_dir.size(); i > 1; )
   34       {
   35       while( i > 0 && prev_dir[i-1] != '/' ) --i;
   36       if( i == 0 ) break;
   37       if( i > 1 ) --i;      // remove trailing slash except at root dir
   38       prev_dir.resize( i );
   39       if( stat( prev_dir.c_str(), &st2 ) != 0 || !S_ISDIR( st2.st_mode ) ||
   40           ( st.st_ino == st2.st_ino && st.st_dev == st2.st_dev ) )
   41         { loop = true; break; }
   42       }
   43   if( loop )            // full_name already visited or above tree
   44     show_file_error( full_name.c_str(), "warning: Recursive directory loop." );
   45   return !loop;         // (link to) directory
   46   }
   47 
   48 
   49 /* Returns in input_filename the next filename, or "." for stdin.
   50    ("." was chosen because it is not a valid filename).
   51    Sets 'error' to true if a directory fails to open. */
   52 bool next_filename( std::list< std::string > & filenames,
   53                     std::string & input_filename, bool & error,
   54                     const int recursive, const bool ignore_stdin = false,
   55                     const bool no_messages = false )
   56   {
   57   while( !filenames.empty() )
   58     {
   59     input_filename = filenames.front();
   60     filenames.pop_front();
   61     if( input_filename == "-" )
   62       {
   63       if( ignore_stdin ) continue;
   64       input_filename = "."; return true;
   65       }
   66     struct stat st;
   67     if( stat( input_filename.c_str(), &st ) == 0 && S_ISDIR( st.st_mode ) )
   68       {
   69       if( recursive )
   70         {
   71         DIR * const dirp = opendir( input_filename.c_str() );
   72         if( !dirp )
   73           {
   74           if( !no_messages )
   75             show_file_error( input_filename.c_str(), "Can't open directory", errno );
   76           error = true; continue;
   77           }
   78         for( unsigned i = input_filename.size();
   79              i > 1 && input_filename[i-1] == '/'; --i )
   80           input_filename.resize( i - 1 );   // remove trailing slashes
   81         struct stat stdot, *stdotp = 0;
   82         if( input_filename[0] != '/' )      // relative path
   83           {
   84           if( input_filename == "." ) input_filename.clear();
   85           if( stat( ".", &stdot ) == 0 && S_ISDIR( stdot.st_mode ) )
   86             stdotp = &stdot;
   87           }
   88         if( input_filename.size() && input_filename != "/" )
   89           input_filename += '/';
   90         std::list< std::string > tmp_list;
   91         while( true )
   92           {
   93           const struct dirent * const entryp = readdir( dirp );
   94           if( !entryp ) { closedir( dirp ); break; }
   95           const std::string tmp_name( entryp->d_name );
   96           if( tmp_name == "." || tmp_name == ".." ) continue;
   97           const std::string full_name( input_filename + tmp_name );
   98           if( test_full_name( full_name, stdotp, recursive == 2 ) )
   99             tmp_list.push_back( full_name );
  100           }
  101         filenames.splice( filenames.begin(), tmp_list );
  102         }
  103       continue;
  104       }
  105     return true;
  106     }
  107   input_filename.clear();
  108   return false;
  109   }