Skip to content

Commit b00d9a5

Browse files
author
Hartmut Holzgraefe
committed
added fnmatch() and glob() functions
could someone please check if i got the virtual dir stuff right?
1 parent a974fbd commit b00d9a5

6 files changed

Lines changed: 83 additions & 1 deletion

File tree

ext/standard/basic_functions.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,12 @@ function_entry basic_functions[] = {
640640
PHP_FALIAS(realpath, warn_not_available, NULL)
641641
#endif
642642

643+
#ifdef HAVE_FNMATCH
644+
PHP_FE(fnmatch, NULL)
645+
#else
646+
PHP_FALIAS(fnmatch, warn_not_available, NULL)
647+
#endif
648+
643649
/* functions from fsock.c */
644650
PHP_FE(fsockopen, third_and_fourth_args_force_ref)
645651
PHP_FE(pfsockopen, third_and_fourth_args_force_ref)
@@ -673,7 +679,11 @@ function_entry basic_functions[] = {
673679
PHP_FE(rewinddir, NULL)
674680
PHP_STATIC_FE("readdir", php_if_readdir, NULL)
675681
PHP_FALIAS(dir, getdir, NULL)
676-
682+
#ifdef HAVE_GLOB
683+
PHP_FE(glob, NULL)
684+
#else
685+
PHP_FALIAS(glob, warn_not_available, NULL)
686+
#endif
677687
/* functions from filestat.c */
678688
PHP_FE(fileatime, NULL)
679689
PHP_FE(filectime, NULL)

ext/standard/config.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ AC_ARG_WITH(system-regex,
229229
fi
230230
])
231231

232+
AC_CHECK_FUNCS(fnmatch glob)
233+
232234
if test "$PHP_SAPI" = "cgi"; then
233235
AC_DEFINE(ENABLE_CHROOT_FUNC, 1, [Whether to enable chroot() function])
234236
fi

ext/standard/dir.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include "win32/readdir.h"
4040
#endif
4141

42+
#ifdef HAVE_GLOB
43+
#include <glob.h>
44+
#endif
45+
4246
typedef struct {
4347
int default_dir;
4448
} php_dir_globals;
@@ -326,6 +330,7 @@ PHP_FUNCTION(rewinddir)
326330
rewinddir(dirp->dir);
327331
}
328332
/* }}} */
333+
329334
/* {{{ proto string readdir([resource dir_handle])
330335
Read directory entry from dir_handle */
331336

@@ -346,6 +351,41 @@ PHP_NAMED_FUNCTION(php_if_readdir)
346351

347352
/* }}} */
348353

354+
#ifdef HAVE_GLOB
355+
/* {{{ proto array glob(string pattern [, int flags])
356+
*/
357+
PHP_FUNCTION(glob)
358+
{
359+
char *pattern = NULL;
360+
int argc = ZEND_NUM_ARGS();
361+
int pattern_len;
362+
long flags;
363+
glob_t globbuf;
364+
zval *new_val;
365+
int n;
366+
char path[MAXPATHLEN];
367+
char *ret=NULL;
368+
369+
if (zend_parse_parameters(argc TSRMLS_CC, "s|l", &pattern, &pattern_len, &flags) == FAILURE)
370+
return;
371+
372+
globbuf.gl_offs = 0;
373+
if(glob(pattern, 0, NULL, &globbuf)) {
374+
RETURN_FALSE;
375+
}
376+
377+
array_init(return_value);
378+
for(n=0;n<globbuf.gl_pathc;n++) {
379+
MAKE_STD_ZVAL(new_val);
380+
ZVAL_STRING(new_val, globbuf.gl_pathv[n], 1);
381+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val,
382+
sizeof(zval *), NULL);
383+
ret = VCWD_GETCWD(path, MAXPATHLEN);
384+
}
385+
globfree(&globbuf);
386+
}
387+
/* }}} */
388+
#endif
349389

350390
/*
351391
* Local variables:

ext/standard/file.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ int file_globals_id;
100100
php_file_globals file_globals;
101101
#endif
102102

103+
#ifdef HAVE_FNMATCH
104+
#include <fnmatch.h>
105+
#endif
106+
103107
/* }}} */
104108
/* {{{ ZTS-stuff / Globals / Prototypes */
105109

@@ -1999,6 +2003,30 @@ php_meta_tags_token php_next_meta_token(php_meta_tags_data *md TSRMLS_DC)
19992003

20002004
/* }}} */
20012005

2006+
#ifdef HAVE_FNMATCH
2007+
/* {{{ proto bool fnmatch(string pattern, string filename [, int flags])
2008+
Match filename against pattern */
2009+
PHP_FUNCTION(fnmatch)
2010+
{
2011+
char *pattern = NULL;
2012+
char *filename = NULL;
2013+
int argc = ZEND_NUM_ARGS();
2014+
int pattern_len;
2015+
int filename_len;
2016+
long flags=0;
2017+
2018+
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l",
2019+
&pattern, &pattern_len,
2020+
&filename, &filename_len,
2021+
&flags)
2022+
== FAILURE)
2023+
return;
2024+
2025+
RETURN_BOOL( ! fnmatch( pattern, filename, flags ));
2026+
}
2027+
/* }}} */
2028+
#endif
2029+
20022030
/*
20032031
* Local variables:
20042032
* tab-width: 4

ext/standard/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ PHP_FUNCTION(fd_isset);
6666
PHP_FUNCTION(select);
6767
#if (!defined(PHP_WIN32) && !defined(__BEOS__) && HAVE_REALPATH) || defined(ZTS)
6868
PHP_FUNCTION(realpath);
69+
PHP_FUNCTION(fnmatch);
6970
#endif
7071
PHP_NAMED_FUNCTION(php_if_ftruncate);
7172
PHP_NAMED_FUNCTION(php_if_fstat);

ext/standard/php_dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ PHP_FUNCTION(getcwd);
3434
PHP_FUNCTION(rewinddir);
3535
PHP_NAMED_FUNCTION(php_if_readdir);
3636
PHP_FUNCTION(getdir);
37+
PHP_FUNCTION(glob);
3738

3839
#endif /* PHP_DIR_H */

0 commit comments

Comments
 (0)