Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ext/standard/tests/file/glob_bracket_negation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
glob() negates a bracket expression that starts with "!" or "^"
--FILE--
<?php
$dir = __DIR__ . '/glob_bracket_negation';
@mkdir($dir);
foreach (['a', 'b', 'c', '^', '!'] as $name) {
touch("$dir/$name");
}

foreach (['[!a]', '[^a]', '[!a-b]', '[^a-b]', '[!]a]', '[^]a]', '[a^]'] as $pattern) {
echo $pattern, ': ', implode(' ', array_map('basename', glob("$dir/$pattern"))), "\n";
}
?>
--CLEAN--
<?php
$dir = __DIR__ . '/glob_bracket_negation';
foreach (['a', 'b', 'c', '^', '!'] as $name) {
@unlink("$dir/$name");
}
@rmdir($dir);
?>
--EXPECT--
[!a]: ! ^ b c
[^a]: ! ^ b c
[!a-b]: ! ^ c
[^a-b]: ! ^ c
[!]a]: ! ^ b c
[^]a]: ! ^ b c
[a^]: ^ a
7 changes: 7 additions & 0 deletions main/php_glob.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@

#include "charclass.h"

#define CIRCUMFLEX '^'
#define DOLLAR '$'
#define DOT '.'
#define EOS '\0'
Expand Down Expand Up @@ -566,6 +567,12 @@ static int glob0(const Char *pattern, php_glob_t *pglob, struct glob_lim *limitp
switch (c) {
case LBRACKET:
c = *qpatnext;
/*
* Not in OpenBSD's glob(), keep it when syncing: like fnmatch(),
* treat a leading '^' as '!'.
*/
if (c == CIRCUMFLEX)
c = NOT;
if (c == NOT)
++qpatnext;
if (*qpatnext == EOS ||
Expand Down
Loading