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
17 * We're most likely overcautious here, but let's reserve for broken WinCE
18 * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
19 * builds are compiled with -DUNICODE [as well as -D_UNICODE].
21 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
22 # define FindFirstFile FindFirstFileW
24 #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
25 # define FindNextFile FindNextFileW
32 struct LP_dir_context_st {
35 char entry_name[NAME_MAX + 1];
38 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
40 if (ctx == NULL || directory == NULL) {
47 const char *extdir = directory;
48 char *extdirbuf = NULL;
49 size_t dirlen = strlen(directory);
56 *ctx = malloc(sizeof(**ctx));
61 memset(*ctx, 0, sizeof(**ctx));
63 if (directory[dirlen - 1] != '*') {
64 extdirbuf = (char *)malloc(dirlen + 3);
65 if (extdirbuf == NULL) {
71 if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
72 extdir = strcat(strcpy(extdirbuf, directory), "/*");
74 extdir = strcat(strcpy(extdirbuf, directory), "*");
77 if (sizeof(TCHAR) != sizeof(char)) {
79 /* len_0 denotes string length *with* trailing 0 */
80 size_t index = 0, len_0 = strlen(extdir) + 1;
82 wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
84 if (extdirbuf != NULL) {
92 #ifdef LP_MULTIBYTE_AVAILABLE
93 if (!MultiByteToWideChar
94 (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
96 for (index = 0; index < len_0; index++)
97 wdir[index] = (TCHAR)extdir[index];
99 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
103 (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
105 if (extdirbuf != NULL) {
109 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
116 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
120 if (sizeof(TCHAR) != sizeof(char)) {
121 TCHAR *wdir = (*ctx)->ctx.cFileName;
122 size_t index, len_0 = 0;
124 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
128 #ifdef LP_MULTIBYTE_AVAILABLE
129 if (!WideCharToMultiByte
130 (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
131 sizeof((*ctx)->entry_name), NULL, 0))
133 for (index = 0; index < len_0; index++)
134 (*ctx)->entry_name[index] = (char)wdir[index];
136 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
137 sizeof((*ctx)->entry_name) - 1);
139 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
141 return (*ctx)->entry_name;
144 int LP_find_file_end(LP_DIR_CTX **ctx)
146 if (ctx != NULL && *ctx != NULL) {
147 FindClose((*ctx)->handle);