~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to main/php_scandir.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 of the PHP license,       |
 
8
   | that is bundled with this package in the file LICENSE, and is        |
 
9
   | available through the world-wide-web at the following url:           |
 
10
   | http://www.php.net/license/3_0.txt.                                  |
 
11
   | If you did not receive a copy of the PHP license and are unable to   |
 
12
   | obtain it through the world-wide-web, please send a note to          |
 
13
   | license@php.net so we can mail you a copy immediately.               |
 
14
   +----------------------------------------------------------------------+
 
15
   | Author: Shane Caraveo <shane@caraveo.com>                            |
 
16
   |         Ilia Alshanetsky <ilia@prohost.org>                          |
 
17
   +----------------------------------------------------------------------+
 
18
 */
 
19
 
 
20
/* $Id: php_scandir.c,v 1.9.2.2 2005/03/11 07:02:32 hyanantha Exp $ */
 
21
 
 
22
#ifdef PHP_WIN32
 
23
#include "config.w32.h"
 
24
#else
 
25
#include <php_config.h>
 
26
#endif
 
27
 
 
28
#include "php_scandir.h"
 
29
 
 
30
#ifdef HAVE_SYS_TYPES_H
 
31
#include <sys/types.h>
 
32
#endif
 
33
 
 
34
#ifdef HAVE_DIRENT_H
 
35
#include <dirent.h>
 
36
#endif
 
37
 
 
38
#ifndef HAVE_SCANDIR
 
39
 
 
40
#ifdef PHP_WIN32
 
41
#include "win32/readdir.h"
 
42
#endif  
 
43
 
 
44
#include <stdlib.h>
 
45
 
 
46
#ifndef NETWARE
 
47
#include <search.h>
 
48
#endif
 
49
 
 
50
#endif /* HAVE_SCANDIR */
 
51
 
 
52
#ifndef HAVE_ALPHASORT
 
53
 
 
54
#ifdef HAVE_STRING_H
 
55
#include <string.h>
 
56
#endif
 
57
 
 
58
int php_alphasort(const struct dirent **a, const struct dirent **b)
 
59
{
 
60
        return strcoll((*a)->d_name,(*b)->d_name);
 
61
}
 
62
#endif /* HAVE_ALPHASORT */
 
63
 
 
64
#ifndef HAVE_SCANDIR
 
65
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))
 
66
{
 
67
        DIR *dirp = NULL;
 
68
        struct dirent **vector = NULL;
 
69
        struct dirent *dp = NULL;
 
70
        int vector_size = 0;
 
71
        int nfiles = 0;
 
72
 
 
73
        if (namelist == NULL) {
 
74
                return -1;
 
75
        }
 
76
 
 
77
        if (!(dirp = opendir(dirname))) {
 
78
                return -1;
 
79
        }
 
80
 
 
81
        while ((dp = readdir(dirp)) != NULL) {
 
82
                int dsize = 0;
 
83
                struct dirent *newdp = NULL;
 
84
 
 
85
                if (selector && (*selector)(dp) == 0) {
 
86
                        continue;
 
87
                }
 
88
 
 
89
                if (nfiles == vector_size) {
 
90
                        struct dirent **newv;
 
91
                        if (vector_size == 0) {
 
92
                                vector_size = 10;
 
93
                        } else { 
 
94
                                vector_size *= 2;
 
95
                        }
 
96
 
 
97
                        newv = (struct dirent **) realloc (vector, vector_size * sizeof (struct dirent *));
 
98
                        if (!newv) {
 
99
                                return -1;
 
100
                        }
 
101
                        vector = newv;
 
102
                }
 
103
 
 
104
                dsize = sizeof (struct dirent) + ((strlen(dp->d_name) + 1) * sizeof(char));
 
105
                newdp = (struct dirent *) malloc(dsize);
 
106
 
 
107
                if (newdp == NULL) {
 
108
                        goto fail;
 
109
                }
 
110
 
 
111
                vector[nfiles++] = (struct dirent *) memcpy(newdp, dp, dsize);
 
112
        }
 
113
 
 
114
        closedir(dirp);
 
115
 
 
116
        *namelist = vector;
 
117
 
 
118
        if (compare) {
 
119
                qsort (*namelist, nfiles, sizeof(struct dirent *), compare);
 
120
        }
 
121
 
 
122
        return nfiles;
 
123
 
 
124
fail:
 
125
        while (nfiles-- > 0) {
 
126
                free(vector[nfiles]);
 
127
        }
 
128
        free(vector);
 
129
        return -1;      
 
130
}
 
131
#endif
 
132
 
 
133
/*
 
134
 * Local variables:
 
135
 * tab-width: 4
 
136
 * c-basic-offset: 4
 
137
 * End:
 
138
 * vim600: sw=4 ts=4 fdm=marker
 
139
 * vim<600: sw=4 ts=4
 
140
 */