php-src/main/php_scandir.c

135 lines
3.3 KiB
C
Raw Normal View History

2015-01-03 09:22:58 +00:00
/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
+----------------------------------------------------------------------+
2017-01-02 15:30:12 +00:00
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Shane Caraveo <shane@caraveo.com> |
| Ilia Alshanetsky <ilia@prohost.org> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
2006-12-03 14:37:32 +00:00
#include "php.h"
#include "php_scandir.h"
#ifdef HAVE_SYS_TYPES_H
2003-01-28 16:05:34 +00:00
#include <sys/types.h>
#endif
2003-01-28 16:05:34 +00:00
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
2003-02-19 18:45:51 +00:00
#ifndef HAVE_SCANDIR
2003-01-28 16:05:34 +00:00
#ifdef PHP_WIN32
2006-12-01 20:17:50 +00:00
#include "win32/param.h"
2003-01-28 16:05:34 +00:00
#include "win32/readdir.h"
2015-01-03 09:22:58 +00:00
#endif
2003-01-28 16:05:34 +00:00
#include <stdlib.h>
#include <search.h>
#endif /* HAVE_SCANDIR */
#ifndef HAVE_ALPHASORT
#ifdef HAVE_STRING_H
2003-01-28 16:05:34 +00:00
#include <string.h>
#endif
2003-01-28 16:04:10 +00:00
PHPAPI int php_alphasort(const struct dirent **a, const struct dirent **b)
{
return strcoll((*a)->d_name,(*b)->d_name);
}
#endif /* HAVE_ALPHASORT */
#ifndef HAVE_SCANDIR
PHPAPI int php_scandir(const char *dirname, struct dirent **namelist[], int (*selector) (const struct dirent *entry), int (*compare) (const struct dirent **a, const struct dirent **b))
{
DIR *dirp = NULL;
struct dirent **vector = NULL;
int vector_size = 0;
int nfiles = 0;
2006-11-30 16:10:38 +00:00
char entry[sizeof(struct dirent)+MAXPATHLEN];
struct dirent *dp = (struct dirent *)&entry;
if (namelist == NULL) {
return -1;
}
if (!(dirp = opendir(dirname))) {
return -1;
}
while (!php_readdir_r(dirp, (struct dirent *)entry, &dp) && dp) {
int dsize = 0;
struct dirent *newdp = NULL;
if (selector && (*selector)(dp) == 0) {
continue;
}
if (nfiles == vector_size) {
struct dirent **newv;
if (vector_size == 0) {
vector_size = 10;
2015-01-03 09:22:58 +00:00
} else {
vector_size *= 2;
}
newv = (struct dirent **) realloc (vector, vector_size * sizeof (struct dirent *));
if (!newv) {
return -1;
}
vector = newv;
}
2014-10-27 17:42:45 +00:00
dsize = sizeof (struct dirent) + (((int)strlen(dp->d_name) + 1) * sizeof(char));
newdp = (struct dirent *) malloc(dsize);
if (newdp == NULL) {
goto fail;
}
vector[nfiles++] = (struct dirent *) memcpy(newdp, dp, dsize);
}
closedir(dirp);
*namelist = vector;
if (compare) {
qsort (*namelist, nfiles, sizeof(struct dirent *), (int (*) (const void *, const void *)) compare);
}
return nfiles;
fail:
while (nfiles-- > 0) {
free(vector[nfiles]);
}
free(vector);
2015-01-03 09:22:58 +00:00
return -1;
}
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/