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
58 struct LP_dir_context_st {
61 char entry_name[NAME_MAX + 1];
64 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
66 if (ctx == NULL || directory == NULL) {
73 const char *extdir = directory;
74 char *extdirbuf = NULL;
75 size_t dirlen = strlen(directory);
82 *ctx = malloc(sizeof(**ctx));
87 memset(*ctx, 0, sizeof(**ctx));
89 if (directory[dirlen - 1] != '*') {
90 extdirbuf = (char *)malloc(dirlen + 3);
91 if (extdirbuf == NULL) {
97 if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
98 extdir = strcat(strcpy(extdirbuf, directory), "/*");
100 extdir = strcat(strcpy(extdirbuf, directory), "*");
103 if (sizeof(TCHAR) != sizeof(char)) {
105 /* len_0 denotes string length *with* trailing 0 */
106 size_t index = 0, len_0 = strlen(extdir) + 1;
108 wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
110 if (extdirbuf != NULL) {
118 #ifdef LP_MULTIBYTE_AVAILABLE
119 if (!MultiByteToWideChar
120 (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
122 for (index = 0; index < len_0; index++)
123 wdir[index] = (TCHAR)extdir[index];
125 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
129 (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
131 if (extdirbuf != NULL) {
135 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
142 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
146 if (sizeof(TCHAR) != sizeof(char)) {
147 TCHAR *wdir = (*ctx)->ctx.cFileName;
148 size_t index, len_0 = 0;
150 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
154 #ifdef LP_MULTIBYTE_AVAILABLE
155 if (!WideCharToMultiByte
156 (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
157 sizeof((*ctx)->entry_name), NULL, 0))
159 for (index = 0; index < len_0; index++)
160 (*ctx)->entry_name[index] = (char)wdir[index];
162 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
163 sizeof((*ctx)->entry_name) - 1);
165 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
167 return (*ctx)->entry_name;
170 int LP_find_file_end(LP_DIR_CTX **ctx)
172 if (ctx != NULL && *ctx != NULL) {
173 FindClose((*ctx)->handle);