2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * We're most likely overcautious here, but let's reserve for broken WinCE
44 * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
45 * builds are compiled with -DUNICODE [as well as -D_UNICODE].
47 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
48 # define FindFirstFile FindFirstFileW
50 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
51 # define FindNextFile FindNextFileW
59 # define CP_DEFAULT CP_UTF8
61 # define CP_DEFAULT CP_ACP
64 struct LP_dir_context_st {
67 char entry_name[NAME_MAX + 1];
70 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
72 if (ctx == NULL || directory == NULL) {
79 const char *extdir = directory;
80 char *extdirbuf = NULL;
81 size_t dirlen = strlen(directory);
88 *ctx = malloc(sizeof(**ctx));
93 memset(*ctx, 0, sizeof(**ctx));
95 if (directory[dirlen - 1] != '*') {
96 extdirbuf = (char *)malloc(dirlen + 3);
97 if (extdirbuf == NULL) {
103 if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
104 extdir = strcat(strcpy(extdirbuf, directory), "/*");
106 extdir = strcat(strcpy(extdirbuf, directory), "*");
109 if (sizeof(TCHAR) != sizeof(char)) {
111 /* len_0 denotes string length *with* trailing 0 */
112 size_t index = 0, len_0 = strlen(extdir) + 1;
113 #ifdef LP_MULTIBYTE_AVAILABLE
119 if ((sz = MultiByteToWideChar((cp = CP_UTF8), 0, extdir, len_0,
121 GetLastError() != ERROR_NO_UNICODE_TRANSLATION)
124 sz = MultiByteToWideChar((cp = CP_ACP), 0, extdir, len_0,
129 wdir = _alloca(sz * sizeof(TCHAR));
130 if (!MultiByteToWideChar(cp, 0, extdir, len_0, wdir, sz)) {
131 if (extdirbuf != NULL) {
142 wdir = _alloca(len_0 * sizeof(TCHAR));
143 for (index = 0; index < len_0; index++)
144 wdir[index] = (TCHAR)extdir[index];
147 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
149 (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
151 if (extdirbuf != NULL) {
155 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
162 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
166 if (sizeof(TCHAR) != sizeof(char)) {
167 TCHAR *wdir = (*ctx)->ctx.cFileName;
168 size_t index, len_0 = 0;
170 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
174 #ifdef LP_MULTIBYTE_AVAILABLE
175 if (!WideCharToMultiByte(CP_DEFAULT, 0, (WCHAR *)wdir, len_0,
177 sizeof((*ctx)->entry_name), NULL, 0))
179 for (index = 0; index < len_0; index++)
180 (*ctx)->entry_name[index] = (char)wdir[index];
182 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
183 sizeof((*ctx)->entry_name) - 1);
185 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
187 return (*ctx)->entry_name;
190 int LP_find_file_end(LP_DIR_CTX **ctx)
192 if (ctx != NULL && *ctx != NULL) {
193 FindClose((*ctx)->handle);