2 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * We're most likely overcautious here, but let's reserve for broken WinCE
34 * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
35 * builds are compiled with -DUNICODE [as well as -D_UNICODE].
37 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
38 # define FindFirstFile FindFirstFileW
40 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
41 # define FindNextFile FindNextFileW
48 struct LP_dir_context_st {
51 char entry_name[NAME_MAX + 1];
54 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
56 if (ctx == NULL || directory == NULL) {
63 const char *extdir = directory;
64 char *extdirbuf = NULL;
65 size_t dirlen = strlen(directory);
72 *ctx = malloc(sizeof(**ctx));
77 memset(*ctx, 0, sizeof(**ctx));
79 if (directory[dirlen - 1] != '*') {
80 extdirbuf = (char *)malloc(dirlen + 3);
81 if (extdirbuf == NULL) {
87 if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
88 extdir = strcat(strcpy(extdirbuf, directory), "/*");
90 extdir = strcat(strcpy(extdirbuf, directory), "*");
93 if (sizeof(TCHAR) != sizeof(char)) {
95 /* len_0 denotes string length *with* trailing 0 */
96 size_t index = 0, len_0 = strlen(extdir) + 1;
98 wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
100 if (extdirbuf != NULL) {
108 #ifdef LP_MULTIBYTE_AVAILABLE
109 if (!MultiByteToWideChar
110 (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
112 for (index = 0; index < len_0; index++)
113 wdir[index] = (TCHAR)extdir[index];
115 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
119 (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
121 if (extdirbuf != NULL) {
125 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
132 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
136 if (sizeof(TCHAR) != sizeof(char)) {
137 TCHAR *wdir = (*ctx)->ctx.cFileName;
138 size_t index, len_0 = 0;
140 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
144 #ifdef LP_MULTIBYTE_AVAILABLE
145 if (!WideCharToMultiByte
146 (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
147 sizeof((*ctx)->entry_name), NULL, 0))
149 for (index = 0; index < len_0; index++)
150 (*ctx)->entry_name[index] = (char)wdir[index];
152 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
153 sizeof((*ctx)->entry_name) - 1);
155 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
157 return (*ctx)->entry_name;
160 int LP_find_file_end(LP_DIR_CTX **ctx)
162 if (ctx != NULL && *ctx != NULL) {
163 FindClose((*ctx)->handle);