1/* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */ 2/* 3 * Copyright (c) 2004, Richard Levitte <richard@levitte.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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. 26 */ 27 28#include "directory.h" 29 30 31#if defined(OPENSSL_WINDOWS) 32 33#include <windows.h> 34#include <tchar.h> 35#include <errno.h> 36 37#ifndef NAME_MAX 38#define NAME_MAX 255 39#endif 40 41struct OPENSSL_dir_context_st { 42 WIN32_FIND_DATA ctx; 43 HANDLE handle; 44 char entry_name[NAME_MAX + 1]; 45}; 46 47const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) { 48 if (ctx == NULL || directory == NULL) { 49 errno = EINVAL; 50 return 0; 51 } 52 53 errno = 0; 54 if (*ctx == NULL) { 55 *ctx = malloc(sizeof(OPENSSL_DIR_CTX)); 56 if (*ctx == NULL) { 57 errno = ENOMEM; 58 return 0; 59 } 60 memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX)); 61 62 if (sizeof(TCHAR) != sizeof(char)) { 63 TCHAR *wdir = NULL; 64 /* len_0 denotes string length *with* trailing 0 */ 65 size_t index = 0, len_0 = strlen(directory) + 1; 66 67 wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR)); 68 if (wdir == NULL) { 69 free(*ctx); 70 *ctx = NULL; 71 errno = ENOMEM; 72 return 0; 73 } 74 75 if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir, 76 len_0)) { 77 for (index = 0; index < len_0; index++) { 78 wdir[index] = (TCHAR)directory[index]; 79 } 80 } 81 82 (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx); 83 84 free(wdir); 85 } else { 86 (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx); 87 } 88 89 if ((*ctx)->handle == INVALID_HANDLE_VALUE) { 90 free(*ctx); 91 *ctx = NULL; 92 errno = EINVAL; 93 return 0; 94 } 95 } else { 96 if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) { 97 return 0; 98 } 99 } 100 101 if (sizeof(TCHAR) != sizeof(char)) { 102 TCHAR *wdir = (*ctx)->ctx.cFileName; 103 size_t index, len_0 = 0; 104 105 while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) { 106 len_0++; 107 } 108 len_0++; 109 110 if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0, 111 (*ctx)->entry_name, sizeof((*ctx)->entry_name), 112 NULL, 0)) { 113 for (index = 0; index < len_0; index++) { 114 (*ctx)->entry_name[index] = (char)wdir[index]; 115 } 116 } 117 } else { 118 strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName, 119 sizeof((*ctx)->entry_name) - 1); 120 } 121 122 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0'; 123 124 return (*ctx)->entry_name; 125} 126 127int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) { 128 if (ctx != NULL && *ctx != NULL) { 129 FindClose((*ctx)->handle); 130 free(*ctx); 131 *ctx = NULL; 132 return 1; 133 } 134 errno = EINVAL; 135 return 0; 136} 137 138#endif /* OPENSSL_WINDOWS */ 139