1 /* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */
3 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "directory.h"
31 #if defined(OPENSSL_WINDOWS)
41 struct OPENSSL_dir_context_st {
44 char entry_name[NAME_MAX + 1];
47 const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) {
48 if (ctx == NULL || directory == NULL) {
55 *ctx = malloc(sizeof(OPENSSL_DIR_CTX));
60 memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX));
62 if (sizeof(TCHAR) != sizeof(char)) {
64 /* len_0 denotes string length *with* trailing 0 */
65 size_t index = 0, len_0 = strlen(directory) + 1;
67 wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR));
75 if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir,
77 for (index = 0; index < len_0; index++) {
78 wdir[index] = (TCHAR)directory[index];
82 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
86 (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
89 if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
96 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
101 if (sizeof(TCHAR) != sizeof(char)) {
102 TCHAR *wdir = (*ctx)->ctx.cFileName;
103 size_t index, len_0 = 0;
105 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) {
110 if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0,
111 (*ctx)->entry_name, sizeof((*ctx)->entry_name),
113 for (index = 0; index < len_0; index++) {
114 (*ctx)->entry_name[index] = (char)wdir[index];
118 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
119 sizeof((*ctx)->entry_name) - 1);
122 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
124 return (*ctx)->entry_name;
127 int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) {
128 if (ctx != NULL && *ctx != NULL) {
129 FindClose((*ctx)->handle);
138 #endif /* OPENSSL_WINDOWS */