/usr/share/octave/packages/io-2.4.10/rfsearch.m is in octave-io 2.4.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | ## Copyright (C) 2013-2017 Philip Nienhuis <prnienhuis at users.sf.net>
##
## 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 Octave; see the file COPYING. If not, see
## <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} @var{filename} = rsearchfile (@var{dname}, @var{fname})
## @deftypefnx {Function File} @var{filename} = rsearchfile (@var{dname}, @var{fname}, @var{maxdepth})
## Recursively search for file or filename pattern FNAME starting in directory
## DNAME and return the first match.
##
## @var{dname} and @var{fname} must be character strings and should conform
## to the directory name and filename requirements of your operating system.
## Optional argument @var{maxdepth} can be specified to limit the maximum search
## depth; the default value is 1 (search only in @var{dname} and subdirs of
## @var{dname}). Setting maxdepth to 0 limits the search to @var{dname}.
## Be careful with setting @var{maxdepth} to values > 3 or 4 as this can
## provoke excessive search times in densely populated directory trees.
## Keep in mind that rfsearch is a recursive function itself.
##
## Output argument @var{filename} returns the relative file path of the
## first match, relative to @var{DNAME}, or an empty character string if
## no match was found.
##
## Examples:
##
## @example
## filename = rfsearch ("/home/guest/octave", "test.fil")
## Look for file test.fil and start the search in /home/guest/octave
## @end example
##
## @example
## filename = rfsearch ("/home", "test.fil", 2)
## Look for file test.fil, start the search in /home, and if needed
## search subdirs of subdirs of /home
## @end example
##
## @seealso {dir, glob}
##
## @end deftypefn
## Author: Philip Nienhuis <prnienhuis@users.sf.net>
## Created: 2013-08-20
function [ fpath ] = rfsearch (dname, fname, mxdpth=1, depth=0)
## Input validation
if (nargin < 2)
print_usage ()
elseif ((! ischar (dname)) || (! ischar (fname)))
error ("rsearchfile: character arguments expected for DNAME and FNAME\n");
elseif (! isnumeric (mxdpth))
error ("Numeric value >= 0 expected for MAXDEPTH\n");
elseif (mxdpth < 0)
warning ("rsearchfile: negative value for MAXDEPTH (%d) set to 0\n", mxdpth);
mxdpth = 0;
elseif (! isnumeric (depth))
print_usage ("too many or illegal arguments");
endif
## If present strip trailing filesep of dname (doesn't hurt though).
## Preserve root /
if (length (dname) > 1 && strcmp (dname(min(2, end)), filesep))
dname(end:end) = '';
endif
sbdir = '';
fpath = dir ([dname filesep fname '*']);
if (isempty (fpath) && depth < mxdpth)
## Bump search depth
++depth;
## Get list of subdirs in current level
dirlist = dir (dname);
if (! isempty (dirlist))
dirlist = dirlist(find ([dirlist.isdir]));
ii = 0;
if (strcmp (dirlist(1).name, '.'))
## Not a root dir; discard entries '.' & '..'
ii = 2;
endif
fpath = '';
## Search all subdirs in current level
while (++ii <= numel (dirlist) && isempty (fpath))
sbdir = [filesep dirlist(ii).name];
fpath = dir ([dname sbdir filesep fname '*']);
if (isempty (fpath) && depth < mxdpth)
## Try a level deeper, if allowed. Be sure to convey current depth
## as 'find_in_subdir' is called recursively here
fpath = rfsearch ([dname sbdir], fname, mxdpth, depth);
endif
endwhile
endif
endif
## Final parts
if (isempty (fpath))
fpath = '';
else
if (isstruct (fpath))
fpath = fpath.name;
endif
## Combine and strip leading filesep
fpath = [sbdir filesep fpath](2:end);
endif
endfunction ## rfsearch
|