bin/glob

filter: Statementstmt 66.0% 70 / 106 Branchbran 48.3% 30 / 62 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 77.7% 14 / 18 Podpod n/a totaltotal 52.2% 114 / 218 CC 39 SCAR 34.8
CC
39
Cov
53%
my_home
110.0
expand_tilde
56.0
run
12.0
CRAP 32.4
« bin/factor ↑ Summary bin/units »
1 #!/usr/bin/perl
2  
3 =begin metadata
4  
5 Name: glob
6 Description: find pathnames matching a pattern
7 Author: Marc Mengel, mengel@fnal.gov
8 Author: brian d foy, briandfoy@pobox.com
9 License: perl
10  
11 =end metadata
12  
13 =cut
14  
15 package PerlPowerTools::glob;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = '2.1';
21  
22 1 ▶use constant EX_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant EX_NO_MATCHES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant EX_FAILURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant EX_ERROR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant IS_WINDOWS => ( $^O eq 'MSWin32' );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use File::Glob qw(csh_glob GLOB_CSH);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶__PACKAGE__->run(@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 1 1
32  
33 1 my %Unknown_users;
34 1 my $wildcards;
35 1 ▶BEGIN { $wildcards = qr/[*?\[]/ }
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 12 ▶sub run {
Subroutine run: called CC=10 SCAR=24.9
38 12 my( $class, @args ) = @_;
39  
40 12 my( $code, $message ) = do {
41 12 ▶ if( @args == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 0) { } 1 11 12
elsif ($class->globbrace(@args) == 0) { } 1 10 11
42 1 ( EX_ERROR, undef );
43 }
44 elsif( $class->globbrace(@args) == 0 ) {
45 1 ( EX_FAILURE, $class->missing_brace_message );
46 }
47 else {
48 10 my $separator = "\n";
49 10 ▶ if( $args[0] eq '-0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "-0") 0 10 10
50 0 shift @args;
51 0 $separator = "\0";
52 }
53  
54 10 my @ARGV_expanded = @args;
55 10 if( IS_WINDOWS ) {
56 @ARGV_expanded = map { expand_tilde($_) } @ARGV_expanded;
57 }
58  
59 10 my @matches = csh_glob( "@ARGV_expanded", GLOB_CSH );
60  
61 10 ▶ if( @matches ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches) { } 9 1 10
elsif (() = keys %Unknown_users) { } 0 1 1
62 9 $class->output_list( \@matches, $separator );
63 9 ( EX_SUCCESS );
64 }
65 elsif( () = keys %Unknown_users ) {
66 0 ( EX_NO_MATCHES, $class->no_match_message );
67 }
68 else {
69 1 my( $message, $code ) = do {
70 1 my $pattern = "@args";
71 1 my $unknown = () = keys %Unknown_users;
72  
73 1 ▶ if( ! IS_WINDOWS && $pattern =~ /(?:\A|\s)~([\w-]+?)\b/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pattern =~ /(?:\A|\s)~([\w-]+?)\b/) { } 0 1 1
elsif ($pattern =~ /$wildcards/) { } 1 0 1
74 0 ▶ getpwnam($1) ? undef : "Unknown user $1.";
Branch: 1 branch
BranchTrueFalseTotal
getpwnam $1 ? : 0 0 0
75 }
76 elsif( $pattern =~ $wildcards ) {
77 1 $class->no_match_message;
78 }
79 0 else { (undef, EX_FAILURE) }
80 };
81 1 ▶ $code = EX_NO_MATCHES unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 1 0 1
82  
83 1 ( $code, $message );
84 }
85 }
86 };
87  
88 12 $class->exit( $code, $message );
89 }
90  
91 0 ▶sub exit {
Subroutine exit: not called CC=3 SCAR=24.8
92 0 my( $class, $code, $message ) = @_;
93  
94 0 ▶ print STDERR $message if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 0 0 0
95 0 ▶ exit( defined $code ? $code : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 0 0 0
96 }
97  
98 0 ▶sub expand_tilde {
Subroutine expand_tilde: not called CC=7 SCAR=40.3
99 0 return $_[0] unless IS_WINDOWS;
100 0 ▶ return $_[0] unless $_[0] =~ $wildcards;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /$wildcards/ 0 0 0
101 0 local $_ = $_[0];
102  
103 0 my $home = my_home();
104 0 my $dir = dirname($home);
105 0 ▶ $dir = '/Users' unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 0 0
106  
107 0 ▶ return $_ unless m/ \A ~ (\w+)? /x;
Branch: 1 branch
BranchTrueFalseTotal
unless / \A ~ (\w+)? /x 0 0 0
108 0 my $user = $1;
109  
110 0 ▶ if( $user && ! $Unknown_users{$user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and not $Unknown_users{$user}) { } 0 0 0
Condition0%0/3
$user and not $Unknown_users{$user}
!ll&&!rl&&r
000
MC/DC0%0/2
$user and not $Unknown_users{$user}
$user not $Unknown_users{$user}
Truth table0%0/3
A and not(B)
A= $userB= not $Unknown_users{$user}
ABresult
0X0
100
111
111 0 my $net_user = `net user "$user" 2>&1`;
112 0 ▶ if( $net_user =~ /could not be found|The syntax of this command/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_user =~ /could not be found|The syntax of this command/) 0 0 0
113 0 print STDERR "Unknown user $user.\n";
114 0 $Unknown_users{$user}++;
115 0 return;
116 }
117 s/ \A ~ (\w+) /$dir\\$1/x
118 0 }
119 else {
120 0 s/ \A ~ /$home/x;
121 }
122  
123 0 return $_;
124 }
125  
126 # https://github.com/aatrens-juniper/OpenBSD-src/blob/master/bin/csh/glob.c
127 11 ▶sub globbrace {
Subroutine globbrace: called CC=12 SCAR=24.9
128 11 my( $class, @args ) = @_;
129  
130 11 foreach my $s ( @args ) {
131 11 my $start = index $s, '{';
132 11 ▶ return 1 if $start == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start == -1 6 5 11
133  
134 5 my $counter = 0;
135 5 ▶ STRING: for( my $i = 0; $i < length($s); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $s) 24 5 29
136 24 ▶ if( '[' eq substr($s, $i, 1) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('[' eq substr($s, $i, 1)) { } 1 23 24
elsif ('{' eq substr($s, $i, 1)) { } 5 18 23
elsif ('}' eq substr($s, $i, 1)) { } 5 13 18
137 1 ▶ SQUARE: for( $i++; $i <= length($s); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= length $s) 2 0 2
138 2 ▶ last SQUARE if ']' eq substr( $s, $i, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if "]" eq substr($s, $i, 1) 1 1 2
139 1 ▶ return 0 if $i == length $s;
Branch: 1 branch
BranchTrueFalseTotal
if $i == length $s 0 1 1
140 }
141 }
142 elsif( '{' eq substr( $s, $i, 1 ) ) {
143 5 $counter++
144 }
145 elsif( '}' eq substr( $s, $i, 1 ) ) {
146 5 ▶ next if $counter == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $counter == 0 1 4 5
147 4 $counter--
148 }
149 }
150  
151 5 ▶ return 0 if $counter != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $counter != 0 1 4 5
152 }
153  
154 4 return 1;
155 }
156  
157  
158 1 ▶sub missing_brace_message { q(Missing '}'.) }
Subroutine missing_brace_message: called CC=1 SCAR=0.0
159  
160 # Stolen from File::HomeDir::Windows;
161 0 ▶sub my_home {
Subroutine my_home: not called CC=10 SCAR=47.0
162 # A lot of unix people and unix-derived tools rely on
163 # the ability to overload HOME. We will support it too
164 # so that they can replace raw HOME calls with File::HomeDir.
165 0 ▶ if (exists $ENV{HOME} and defined $ENV{HOME} and length $ENV{HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"HOME"} and defined $ENV{"HOME"} and length $ENV{"HOME"}) 0 0 0
Condition0%0/6
exists $ENV{"HOME"} and defined $ENV{"HOME"} and length $ENV{"HOME"}
!ll&&!rl&&r
000
exists $ENV{"HOME"} and defined $ENV{"HOME"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $ENV{"HOME"} and defined $ENV{"HOME"} and length $ENV{"HOME"}
exists $ENV{"HOME"} defined $ENV{"HOME"} length $ENV{"HOME"}
Truth table0%0/4
A and B and C
A= exists $ENV{"HOME"}B= defined $ENV{"HOME"}C= length $ENV{"HOME"}
ABCresult
0XX0
10X0
1100
1111
166 0 return $ENV{HOME};
167 }
168  
169 # Do we have a user profile?
170 0 ▶ if (exists $ENV{USERPROFILE} and $ENV{USERPROFILE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"USERPROFILE"} and $ENV{"USERPROFILE"}) 0 0 0
Condition0%0/3
exists $ENV{"USERPROFILE"} and $ENV{"USERPROFILE"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"USERPROFILE"} and $ENV{"USERPROFILE"}
exists $ENV{"USERPROFILE"} $ENV{"USERPROFILE"}
Truth table0%0/3
A and B
A= exists $ENV{"USERPROFILE"}B= $ENV{"USERPROFILE"}
ABresult
0X0
100
111
171 0 return $ENV{USERPROFILE};
172 }
173  
174 # Some Windows use something like $ENV{HOME}
175 0 ▶ if (exists $ENV{HOMEDRIVE} and exists $ENV{HOMEPATH} and $ENV{HOMEDRIVE} and $ENV{HOMEPATH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"HOMEDRIVE"} and exists $ENV{"HOMEPATH"} and $ENV{"HOMEDRIVE"} and $ENV{"HOMEPATH"}) 0 0 0
Condition0%0/9
exists $ENV{"HOMEDRIVE"} and exists $ENV{"HOMEPATH"} and $ENV{"HOMEDRIVE"} and $ENV{"HOMEPATH"}
!ll&&!rl&&r
000
exists $ENV{"HOMEDRIVE"} and exists $ENV{"HOMEPATH"} and $ENV{"HOMEDRIVE"}
!ll&&!rl&&r
000
exists $ENV{"HOMEDRIVE"} and exists $ENV{"HOMEPATH"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $ENV{"HOMEDRIVE"} and exists $ENV{"HOMEPATH"} and $ENV{"HOMEDRIVE"} and $ENV{"HOMEPATH"}
exists $ENV{"HOMEDRIVE"} exists $ENV{"HOMEPATH"} $ENV{"HOMEDRIVE"} $ENV{"HOMEPATH"}
Truth table0%0/5
A and B and C and D
A= exists $ENV{"HOMEDRIVE"}B= exists $ENV{"HOMEPATH"}C= $ENV{"HOMEDRIVE"}D= $ENV{"HOMEPATH"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
176 0 return File::Spec->catpath($ENV{HOMEDRIVE}, $ENV{HOMEPATH}, '',);
177 }
178  
179 0 return;
180 }
181  
182 1 ▶sub no_match_message { 'glob: No match.' }
Subroutine no_match_message: called CC=1 SCAR=0.0
183  
184 0 ▶sub output_list {
Subroutine output_list: not called CC=2 SCAR=17.9
185 0 my( $class, $array, $separator ) = @_;
186 0 ▶ $separator = "\n" unless defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
187  
188 0 print STDOUT join $separator, @$array;
189 0 print "\n";
190 }
191  
192 =encoding utf8
193  
194 =head1 NAME
195  
196 glob - output pathnames matching a pattern
197  
198 =head1 SYNOPSIS
199  
200 On the command-line:
201  
202 glob 'eenie{meenie,mynie,moe}*.[ch]'
203  
204 =head1 DESCRIPTION
205  
206 When this program was originally created, *perl* did not have a builtin
207 C<glob> feature and would rely on the *csh* to do the work for it. With
208 Perl v5.6 in March 2000, the L<File::Glob> module has done that work
209 without interacting with *csh*.
210  
211 =head2 Pattern Matching Syntax for Filename Expansion
212  
213 The expressions that are passed as arguments to B<glob> must adhere to
214 csh/tcsh pattern-matching syntax for wildcard filename expansion (also
215 known as I<globbing>). Unquoted words containing an asterisk (C<*>),
216 question-mark (C<?>), square-brackets (C<[...]>), or curly-braces (C<{...}>), or
217 beginning with a tilde (~), are expanded into an alphabetically sorted
218 list of filenames, as follows:
219  
220 =over 5
221  
222 =item C<*>
223  
224 Match any (zero or more) characters.
225  
226 =item C<?>
227  
228 Match any single character.
229  
230 =item [...]
231  
232 Match any single character in the given character class. The character
233 class is the enclosed list(s) or range(s). A list is a string of
234 characters. A range is two characters separated by a dash (-), and
235 includes all the characters in between the two characters given
236 (inclusive). If a dash (C<->) is intended to be part of the character
237 class it must be the first character given.
238  
239 =item {str1,str2,...}
240  
241 Expand the given "word-set" to each string (or filename-matching
242 pattern) in the comma-separated list. Unlike the pattern-matching
243 expressions above, the expansion of this construct is not sorted. For
244 instance, C<{foo,bar}> expands to C<foo bar> (not C<bar foo>). As
245 special cases, unmatched C<{> and C<}>, and the "empty set" (the string
246 {}) are treated as ordinary characters instead of pattern-matching
247 meta-characters. A backslash (C<\)> may be used to escape an opening or
248 closing curly brace, or the backslash character itself. Note that
249 word-sets I<may> be nested!
250  
251 =item C<~>
252  
253 The home directory of the invoking user as indicated by the value of
254 the variable C<$HOME>.
255  
256 =item ~username
257  
258 The home directory of the user whose login name is 'username',
259 as indicated by the password entry for the named user.
260  
261 =back
262  
263 Only the patterns *, ? and [...] imply pattern matching; an error
264 results if no filename matches a pattern that contains them. When
265 a period or "dot" (.) is the first character in a filename or
266 pathname component, it must be matched explicitly. The filename
267 component separator character (e.g., / or slash) must also
268 be matched explicitly.
269  
270 =head1 OPTIONS
271  
272 When the first argument is B<-0> (a minus sign followed by the number
273 zero), then a NUL character ("\0") is used to separate the expanded
274 words and/or filenames when printing them to standard output.
275 Otherwise a newline is used as the word/filename output separator.
276  
277 =head1 RETURNS
278  
279 When B<glob> is invoked as a script from the command-line, the exit-status
280 returned will be 0 if any files were matched or word-sets were expanded;
281 1 if no files/word-sets were matched/expanded; and 2 if some other kind of
282 error occurred.
283  
284 =head1 DIAGNOSTICS
285  
286 If no filenames are matched and pattern-matching characters were used
287 (C<*>, C<?>, or C<[...]>), then an error message of "No Match" is issued. If a
288 user's home directory is specified using tilde-expansion (e.g., C<~username>)
289 but the corresponding username or their home directory cannot be found,
290 then the error message "Unknown user: username" is issued.
291  
292 =head1 COPYRIGHT
293  
294 Copyright (c) 1997-2025 Marc Mengel. All rights reserved.
295  
296 This library is free software; you can redistribute it and/or modify it under
297 the same terms as Perl itself.
298  
299 =head1 AUTHOR
300  
301 Marc Mengel E<lt>F<mengel@fnal.gov>E<gt>
302  
303 =head1 REVISIONS
304  
305 =over 4
306  
307 =item brian f foy E<lt>F<briandfoy@pobox.com>E<gt> - v2.1 February 2025
308  
309 Reimplement this as a thin layer over L<File::Glob::csh_glob>. This
310 program was written before that was a core module, but had several
311 edge cases where it would crash.
312  
313 =item Brad Appleton E<lt>F<bradapp@enteract.com>E<gt> - v1.2 March 1999
314  
315 Modified to use qr// (and some other minor speedups), to explode
316 subexpressions in curly braces (a la csh -- rather than using just
317 plain alternation), and made callable as a standalone script.
318  
319 =back
320  
321 =cut
322  
« bin/factor ↑ Summary bin/units »