| 1 |
/* -*- Mode: C; c-file-style: "gnu" -*- */ |
| 2 |
/* |
| 3 |
Copyright (c) 2000 Petter Reinholdtsen |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person |
| 6 |
obtaining a copy of this software and associated documentation |
| 7 |
files (the "Software"), to deal in the Software without |
| 8 |
restriction, including without limitation the rights to use, copy, |
| 9 |
modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 |
of the Software, and to permit persons to whom the Software is |
| 11 |
furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be |
| 14 |
included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* |
| 27 |
* scandir.c -- if scandir() is missing, make a replacement |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
#ifndef HAVE_SCANDIR |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#ifdef HAVE_CONFIG_H |
| 38 |
#include "config.h" |
| 39 |
#endif |
| 40 |
#include "arch.h" |
| 41 |
|
| 42 |
#ifdef HAVE_DIRENT_H |
| 43 |
# include <dirent.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
#ifdef HAVE_WINDOWS_H |
| 48 |
|
| 49 |
#include <windows.h> |
| 50 |
|
| 51 |
/* |
| 52 |
* MS-Windows version of scandir (uses FindFirst...) |
| 53 |
*/ |
| 54 |
int |
| 55 |
scandir (const char *dir, struct dirent ***namelist, |
| 56 |
int (*select)(const struct dirent *), |
| 57 |
int (*compar)(const struct dirent **, const struct dirent **)) |
| 58 |
{ |
| 59 |
WIN32_FIND_DATA file_data; |
| 60 |
HANDLE handle; |
| 61 |
int count, pos; |
| 62 |
struct dirent **names; |
| 63 |
char *pattern; |
| 64 |
|
| 65 |
/* 3 for "*.*", 1 for "\", 1 for zero termination */ |
| 66 |
pattern = (char*)malloc(strlen(dir) + 3 +1 +1); |
| 67 |
strcpy(pattern, dir); |
| 68 |
if (pattern[ strlen(pattern) - 1] != '\\') |
| 69 |
strcat(pattern, "\\"); |
| 70 |
strcat(pattern, "*.*"); |
| 71 |
|
| 72 |
/* 1st pass thru is just to count them */ |
| 73 |
handle = FindFirstFile(pattern, &file_data); |
| 74 |
if (handle == INVALID_HANDLE_VALUE) |
| 75 |
{ |
| 76 |
free(pattern); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
count = 0; |
| 81 |
while (1) |
| 82 |
{ |
| 83 |
count++; |
| 84 |
if (!FindNextFile(handle, &file_data)) |
| 85 |
break; |
| 86 |
} |
| 87 |
FindClose(handle); |
| 88 |
|
| 89 |
/* Now we know how many, we can alloc & make 2nd pass to copy them */ |
| 90 |
names = (struct dirent**)malloc(sizeof(struct dirent*) * count); |
| 91 |
memset(names, 0, sizeof(*names)); |
| 92 |
handle = FindFirstFile(pattern, &file_data); |
| 93 |
if (handle == INVALID_HANDLE_VALUE) |
| 94 |
{ |
| 95 |
free(pattern); |
| 96 |
free(names); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
/* Now let caller filter them if requested */ |
| 101 |
pos = 0; |
| 102 |
while (1) |
| 103 |
{ |
| 104 |
int rtn; |
| 105 |
struct dirent current; |
| 106 |
|
| 107 |
strcpy(current.d_name, file_data.cFileName); |
| 108 |
|
| 109 |
if (!select || select(¤t)) |
| 110 |
{ |
| 111 |
struct dirent *copyentry = malloc(sizeof(struct dirent)); |
| 112 |
strcpy(copyentry->d_name, current.d_name); |
| 113 |
names[pos] = copyentry; |
| 114 |
pos++; |
| 115 |
} |
| 116 |
|
| 117 |
rtn = FindNextFile(handle, &file_data); |
| 118 |
if (!rtn || rtn==ERROR_NO_MORE_FILES) |
| 119 |
break; |
| 120 |
} |
| 121 |
|
| 122 |
free(pattern); |
| 123 |
/* Now sort them */ |
| 124 |
if (compar) |
| 125 |
qsort(names, pos, sizeof(names[0]), compar); |
| 126 |
*namelist = names; |
| 127 |
return pos; |
| 128 |
} |
| 129 |
#else |
| 130 |
int |
| 131 |
scandir(const char *dir, struct dirent ***namelist, |
| 132 |
int (*select)(const struct dirent *), |
| 133 |
int (*compar)(const struct dirent **, const struct dirent **)) |
| 134 |
{ |
| 135 |
DIR *d = opendir(dir); |
| 136 |
struct dirent *current; |
| 137 |
struct dirent **names; |
| 138 |
int count = 0; |
| 139 |
int pos = 0; |
| 140 |
int result = -1; |
| 141 |
|
| 142 |
if (NULL == d) |
| 143 |
return -1; |
| 144 |
|
| 145 |
while (NULL != readdir(d)) count++; |
| 146 |
|
| 147 |
names = malloc(sizeof(struct dirent *) * count); |
| 148 |
|
| 149 |
closedir(d); |
| 150 |
d = opendir(dir); |
| 151 |
if (NULL == d) |
| 152 |
return -1; |
| 153 |
|
| 154 |
while (NULL != (current = readdir(d))) { |
| 155 |
if ( NULL == select || select(current) ) { |
| 156 |
struct dirent *copyentry = malloc(current->d_reclen); |
| 157 |
|
| 158 |
memcpy(copyentry, current, current->d_reclen); |
| 159 |
|
| 160 |
names[pos] = copyentry; |
| 161 |
pos++; |
| 162 |
} |
| 163 |
} |
| 164 |
result = closedir(d); |
| 165 |
|
| 166 |
if (pos != count) |
| 167 |
names = realloc(names, sizeof(struct dirent *)*pos); |
| 168 |
|
| 169 |
*namelist = names; |
| 170 |
|
| 171 |
return pos; |
| 172 |
} |
| 173 |
#endif |
| 174 |
#endif /* HAVE_SCANDIR */ |